MKMapView:如何将 MKUserLocationView (蓝点)带到前面?

发布于 2024-10-05 22:39:47 字数 360 浏览 0 评论 0原文

我正在添加自定义注释而不是引脚注释。 基本上,我在这些注释中画一个圆圈并将它们添加到地图中。 这些自定义注释模糊了用户位置视图的问题(蓝点) 如何将蓝点移到前面。

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

但是,这对我不起作用。蓝色的用户点仍然被我的自定义注释覆盖。 有什么想法可以让用户位置视图出现在前面吗? 干杯

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.
Any ideas how I can get the user location view to appear in front?
Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一场信仰旅途 2024-10-12 22:39:48

试试这个,它有效:

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

Try this, it works.:

MKAnnotationView *userLocationView = [self.mapView viewForAnnotation:self.mapView.userLocation];
[userLocationView.superview bringSubviewToFront:userLocationView];
樱桃奶球 2024-10-12 22:39:48

我是这样完成的:

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

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];
        }
    }
}
七月上 2024-10-12 22:39:48

通常您希望 userLocation 视图位于后面。至少这是带有图钉的地图视图的正常行为。

如果你真的想把它放在前面,你必须找出真正的 userLocation 视图。我相信 [mapView viewForAnnotation:userLocation] 不会为您提供真正的 userLocation 视图(您检查过它不是 nil 吗?)。我会向您的 MKMapViewDelegate 添加一个 -mapView:didAddAnnotationViews: ,如下所示:

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
    for (id view in views) {
        if (![view isKindOfClass:[YourCustomAnnotationView class]]) {
            self.userLocationView = 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;
        }
    }
}
霓裳挽歌倾城醉 2024-10-12 22:39:48

奥特温的回答对我不起作用。 MKUserLocationView 从来不在该数组中(至少在我的测试中)。

相反,直接从 MKMapView 实例获取值。像这样:


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

“userLocationAnnotationView”是我的视图控制器中的一个ivar:

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

当您运行此代码时,当前位置视图必须已经添加。 viewDidLoad 中的情况可能并非如此,原因有多种,包括:a) 还没有 GPS 更新 b) 当前位置不在屏幕上。我正在运行此代码:

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

祝你好运!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文