2011-03-09 178 views
3

我正在尋找自定義MKMapView userLocation註釋,以繪製一個像官方地圖應用程序的標題箭頭。MKMapView自定義用戶位置註釋

我試圖做到這一點在viewForAnnotation:

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

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

    } 
} 

但我無法找到下一個要獲得用戶位置的視圖和自定義該怎麼辦。

非常感謝......

回答

5

默認用戶位置標記(至少在SDK 4.2)的類是MKUserLocationView,但這樣你就不能創建一個實例,而不從應用冒着拒絕修改,這是不公開商店。您必須改爲創建自己的MKAnnotationView(或MKAnnotationView的子類)。

+0

肯定會是不錯的一大量的代碼來演示這裏的技術! –

6

我創建了SVPulsingAnnotationViewMKUserLocationView的可定製副本。

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(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()*/ 
相關問題