2011-03-09 16 views
3

MKMapViewのuserLocation注釈をカスタマイズして、公式マップアプリのような見出しの矢印を描こうとしています。MKMapViewカスタマイズUserLocation注釈

私はviewForAnnotationでこれを行うにしようとしています:

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 

    if ([annotation isKindOfClass:[MKUserLocation class]]) { 

    } 
} 

が、私はUserLocationのビューを取得し、それをカスタマイズするために次に何をすべきかを見つけることができません。

多くのおかげで...

答えて

5

(少なくともSDK 4.2での)デフォルトのユーザ位置マーカーのためのクラスがMKUserLocationViewですが、アプリから拒絶反応を危険にさらすことなく、変更するインスタンスを作成することはできませんので、これはパブリックではありません格納。代わりに独自のMKAnnotationView(またはMKAnnotationViewのサブクラス)を作成する必要があります。

+0

確かに持っていいだろうここの技術を実証するコードの塊! –

6

MKUserLocationViewのカスタマイズ可能なレプリカであるSVPulsingAnnotationViewを作成しました。

enter image description here

あなたは、他のMKAnnotationViewのようにそれをインスタンス化:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { 
    if([annotation isKindOfClass:[MyAnnotationClass class]]) { 
     static NSString *identifier = @"currentLocation"; 
     SVPulsingAnnotationView *pulsingView = (SVPulsingAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; 

     if(pulsingView == nil) { 
      pulsingView = [[SVPulsingAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; 
      pulsingView.annotationColor = [UIColor colorWithRed:0.678431 green:0 blue:0 alpha:1]; 
     } 

     pulsingView.canShowCallout = YES; 
     return pulsingView; 
    } 

    return nil; 
} 
0

輸入のUIKit 輸入MapKit 輸入CoreLocation

クラスsecondViewController:のUIViewController、CLLocationManagerDelegate、MKMapViewDelegate {

@IBOutlet var mapview: MKMapView! 

// VARのlocationmanager = CLLocationManager()

let locationManager = CLLocationManager() 
var currentLocation: CLLocation! 

/*パブリックFUNCのlocationManager(_マネージャ:CLLocationManager、didUpdateLocations場所:[CLLocation]){

let location = locations[0] 

    let span = MKCoordinateSpanMake(0.1, 0.1) 

    let my = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) 

    print(my) 
    let region = MKCoordinateRegionMake(my, span) 

    mapview.setRegion(region, animated: true) 

    self.mapview.showsUserLocation = true 
}*/ 

override func viewDidLoad() { 
    super.viewDidLoad() 

/* mapview.mapType = MKMapType.standard 
    locationmanager.delegate = self 
    locationmanager.desiredAccuracy = kCLLocationAccuracyBest 
    locationmanager.requestWhenInUseAuthorization() 
/* isAuthorizedtoGetUserLocation() 
    if CLLocationManager.locationServicesEnabled() { 
     locationmanager.delegate = self 
     locationmanager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters 
    }*/ 

    locationmanager.startUpdatingLocation()*/ 
関連する問題