MKMapView标注位置更新问题
我需要通过实时刷新率跟踪用户当前位置 我有一个函数有两个解决方案。
- (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在Variant_1中,它可能会闪烁,因为您正在执行removeAnnotation,然后执行addAnnotation,而不是仅仅修改现有注释的坐标。
在 Variant_2 中,initWithCooperative 返回具有这些坐标的新 MKPlacemark 对象。它不会更新您正在调用该方法的对象的属性。
如果您改为运行 setCooperative 行会发生什么情况?
另一个问题是为什么不使用 MKMapView 的内置功能来显示当前用户位置?只需在开始时执行
m_Map.showsUserLocation = YES;
即可。如果您无论如何都使用 MKMapView,则不需要 CLLocationManager 来获取用户的当前位置。我认为您仍然需要使用地图视图委托方法之一将地图置于用户当前位置的中心:
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: