MKMapView标注位置更新问题

发布于 2024-09-30 00:01:54 字数 1218 浏览 5 评论 0原文

我需要通过实时刷新率跟踪用户当前位置 我有一个函数有两个解决方案。

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    
# ifdef Variant_1
    if(m_currentLocation)
        [m_Map removeAnnotation:m_currentLocation];
    else
        m_currentLocation = [MKPlacemark alloc];
    [m_currentLocation initWithCoordinate:newLocation.coordinate addressDictionary:nil];
    [m_Map addAnnotation:m_currentLocation];
    [m_Map setCenterCoordinate:m_currentLocation.coordinate animated:YES];

# else //Variant_2   
    
    if(m_currentLocation == nil)
     {
     m_currentLocation = [MKPlacemark alloc];
     [m_currentLocation initWithCoordinate:newLocation.coordinate addressDictionary:nil];
     [m_Map addAnnotation:m_currentLocation];
     
     }else
     {
     [m_currentLocation initWithCoordinate:newLocation.coordinate addressDictionary:nil];
     //[m_currentLocation setCoordinate:newLocation.coordinate];
     }
    [m_Map setCenterCoordinate:m_currentLocation.coordinate animated:YES];
# endif      
}

Variant_1 效果很好,但当您快速移动时,地图上的位置信号会闪烁。
Variant_2 不会闪烁,但不会移动位置,但会移动地图。
问题出在哪里?

I need to track user current location with realtime refreshrate
I have one function with two solutions for that.

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    
# ifdef Variant_1
    if(m_currentLocation)
        [m_Map removeAnnotation:m_currentLocation];
    else
        m_currentLocation = [MKPlacemark alloc];
    [m_currentLocation initWithCoordinate:newLocation.coordinate addressDictionary:nil];
    [m_Map addAnnotation:m_currentLocation];
    [m_Map setCenterCoordinate:m_currentLocation.coordinate animated:YES];

# else //Variant_2   
    
    if(m_currentLocation == nil)
     {
     m_currentLocation = [MKPlacemark alloc];
     [m_currentLocation initWithCoordinate:newLocation.coordinate addressDictionary:nil];
     [m_Map addAnnotation:m_currentLocation];
     
     }else
     {
     [m_currentLocation initWithCoordinate:newLocation.coordinate addressDictionary:nil];
     //[m_currentLocation setCoordinate:newLocation.coordinate];
     }
    [m_Map setCenterCoordinate:m_currentLocation.coordinate animated:YES];
# endif      
}

Variant_1 works good but when you move fast the location sing on the map blinks.
Variant_2 does not blink but does not move location sing however moves map.
Where is the problem?

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

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

发布评论

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

评论(1

扎心 2024-10-07 00:01:54

在Variant_1中,它可能会闪烁,因为您正在执行removeAnnotation,然后执行addAnnotation,而不是仅仅修改现有注释的坐标。

在 Variant_2 中,initWithCooperative 返回具有这些坐标的新 MKPlacemark 对象。它不会更新您正在调用该方法的对象的属性。

如果您改为运行 setCooperative 行会发生什么情况?

另一个问题是为什么不使用 MKMapView 的内置功能来显示当前用户位置?只需在开始时执行 m_Map.showsUserLocation = YES; 即可。如果您无论如何都使用 MKMapView,则不需要 CLLocationManager 来获取用户的当前位置。

我认为您仍然需要使用地图视图委托方法之一将地图置于用户当前位置的中心:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    [mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
}

In Variant_1, it probably blinks because you're doing a removeAnnotation and then an addAnnotation instead of just modifying the coordinates of the existing annotation.

In Variant_2, initWithCoordinate returns a new MKPlacemark object with those coordinates. It doesn't update the properties of the object you are calling the method on.

What happens if you run the setCoordinate line instead?

A separate question is why not use the MKMapView's built-in ability to show the current user location? Just do m_Map.showsUserLocation = YES; at the start. You don't need CLLocationManager to get the user's current location if you are using the MKMapView anyway.

I think you'll still need to center the map on the user's current location using one of the map view delegate methods:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    [mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文