Custom Annotation view for userlocation not moving the mapview

前端 未结 1 1013
一生所求
一生所求 2020-11-27 18:48

Can we have custom annotation view for the users current location in iOS?

I need to remove the blue dot (with circles) with my own custom view (say some ping pin).

相关标签:
1条回答
  • 2020-11-27 19:12

    Yes, you can have a custom view for the user's location.

    Unfortunately, it's harder to implement than it should be because even though the documentation for the viewForAnnotation delegate method claims that you can just supply your own view if the annotation class is MKUserLocation, the custom view does not then continue to move with the user's location. In fact, when a custom view is returned for MKUserLocation, the map view stops updating the user location entirely (the map view's didUpdateUserLocation delegate method no longer fires). I believe this is a bug.

    A workaround is to use CLLocationManager and a custom annotation...


    Make sure showsUserLocation is NO or unchecked on the map view.

    Declare properties for a CLLocationManager and a custom annotation using a custom class that implements the MKAnnotation protocol (or you could just use the generic MKPointAnnotation class).

    In viewDidLoad or some other appropriate place, create the CLLocationManager, set its delegate and call startUpdatingLocation.

    In the location manager's didUpdateToLocation delegate method (not the map view's didUpdateUserLocation delegate method), create or update your custom annotation:

    -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {
        if (myUserLocAnnot == nil)
        {
            self.myUserLocAnnot = [[[MyUserLocClass alloc] init] autorelease];
              //remove the autorelease if using ARC
            myUserLocAnnot.title = @"You are here";
            myUserLocAnnot.coordinate = newLocation.coordinate;
            [mapView addAnnotation:myUserLocAnnot];
        }
        else
        {
            myUserLocAnnot.coordinate = newLocation.coordinate;
        }
    }
    

    Finally, in the map view's viewForAnnotation delegate method, you would return a custom annotation view if the annotation is your custom user location annotation.

    0 讨论(0)
提交回复
热议问题