开发者

MKMapView: how to bring MKUserLocationView (The blue dot) to front?

I am adding a custom annotation instead of the pin annotation. Basically, I draw a circle in these annotations and add them to the map. THe problem these custom annotations obscure the user location view ( the blue dot) How do I bring the blue dot to the front.

- (void)mapView:(MKMapView *)mapView 
didUpdateUserLocation:(MKUserLocation *)userLocation 
{
UIView *user = [mapView viewForAnnotation:userLocation];
[mapView bringSubviewToFront:user];
}

However, this is not working for me. The blue user dot is still being covered by my custom annotations. 开发者_C百科 Any ideas how I can get the user location view to appear in front? Cheers


Try this, it works.:

MKAnnotationView *userLocationView = [self.mapView viewForAnnotation:self.mapView.userLocation];
[userLocationView.superview bringSubviewToFront:userLocationView];


I accomplished it like this:

- (void) mapView:(MKMapView *)aMapView didAddAnnotationViews:(NSArray *)views {
    for (MKAnnotationView *view in views) {
        if ([[view annotation] isKindOfClass:[MKUserLocation class]]) {
            [[view superview] bringSubviewToFront:view];
        } else {
            [[view superview] sendSubviewToBack:view];
        }
    }
}


Normally you'd want to have the userLocation view in the back. At least that's the normal behavior for a mapView with pins.

If you really want to bring it to the front, you have to find out the real userLocation view. I believe [mapView viewForAnnotation:userLocation] doesn't give you the real userLocation view (have you checked it's not nil?). I would add a -mapView:didAddAnnotationViews: to your MKMapViewDelegate like this:

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
    for (id view in views) {
        if (![view isKindOfClass:[YourCustomAnnotationView class]]) {
            self.userLocationView = view;
        }
    }
}


Ortwin's answer did not work for me. The MKUserLocationView is never in that array (at least in my testing).

Instead, get the value from the MKMapView instance directly. Like so:


if (self.userLocationAnnotationView != nil && self.mapView.userLocation != nil) {
  self.userLocationAnnotationView = [self.mapView viewForAnnotation:self.mapView.userLocation];
}

"userLocationAnnotationView" is an ivar I have in my view controller:

MKAnnotationView *userLocationAnnotationView;
@property (nonatomic, retain) MKAnnotationView *userLocationAnnotationView;

When you run this code, the current location view must have already been added. This may not be the case in viewDidLoad for a variety of reasons including: a) no GPS updates yet b) current location is off screen. I'm running this code in:

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading

Good luck!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜