开发者

MKMapView customize UserLocation Annotation

I am looking to customize the MKMapView userLocation annotation, to draw a heading arrow like the official map app.

I am trying to do this in viewForAnnotation:

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

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

开发者_C百科    }
 }

but I am unable to find what to do next to get the view for UserLocation and customize it.

Many thanks...


I created SVPulsingAnnotationView, a customizable replica of MKUserLocationView.

MKMapView customize UserLocation Annotation

You instantiate it just like any other 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;
}


The class for the default user location marker (at least in SDK 4.2) is MKUserLocationView, but this is not public so you cannot create an instance to modify without risking rejection from the app store. You'll have to create your own MKAnnotationView (or subclass of MKAnnotationView) instead.


import UIKit import MapKit import CoreLocation

class secondViewController: UIViewController, CLLocationManagerDelegate,MKMapViewDelegate{

@IBOutlet var mapview: MKMapView!

// var locationmanager = CLLocationManager()

let locationManager = CLLocationManager()
var currentLocation: CLLocation!

/* public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [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()*/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜