MKMapView configures UserLocation annotation

I want to customize the MKLapView userLocation annotation to draw a title arrow, such as 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]]) {

    }
 }

but I can’t find what to do next to get a view for UserLocation and set it up.

Many thanks...

+3
source share
3 answers

The class for the user's default location marker (at least in the SDK 4.2) is MKUserLocationView, but it is not publicly available, so you cannot create an instance to change without the risk of deviating from the app store. You will need to create your own MKAnnotationView (or a subclass of MKAnnotationView).

+5
source

SVPulsingAnnotationView, MKUserLocationView.

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;
}
+6

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 location: [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
source

All Articles