Nimrod7
Nimrod7

Reputation: 1443

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]]) {

    }
 }

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

Many thanks...

Upvotes: 3

Views: 3314

Answers (3)

Hiren
Hiren

Reputation: 270

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()*/

Upvotes: 0

samvermette
samvermette

Reputation: 40437

I created SVPulsingAnnotationView, a customizable replica of MKUserLocationView.

enter image description here

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

Upvotes: 6

Anomie
Anomie

Reputation: 94834

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.

Upvotes: 5

Related Questions