問(wèn)題描述
我在 mapView
上有一個(gè)用戶位置(藍(lán)點(diǎn))和注釋.選擇注釋后,我將文本設(shè)置為 distLabel
-到點(diǎn) %4.0f m. 的距離.".用戶移動(dòng)時(shí)如何更新該文本標(biāo)簽?
I have an user location (blue dot) and annotations on mapView
. When annotation is selected, I setting text to distLabel
- "Distance to point %4.0f m.". How can I update that text label when user moves?
didSelectAnnotationView:
didSelectAnnotationView:
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
CLLocation *pinLocation =
[[CLLocation alloc] initWithLatitude:
[(MyAnnotation*)[view annotation] coordinate].latitude
longitude:
[(MyAnnotation*)[view annotation] coordinate].longitude];
CLLocation *userLocation =
[[CLLocation alloc] initWithLatitude:
self.mapView.userLocation.coordinate.latitude
longitude:
self.mapView.userLocation.coordinate.longitude];
CLLocationDistance distance = [pinLocation distanceFromLocation:userLocation];
[distLabel setText: [NSString stringWithFormat:@"Distance to point %4.0f m.",
distance]];
}
我知道有一個(gè)函數(shù) didUpdateToLocation
,但是如何將它與 didSelectAnnotationView
一起使用?
I know there is a function didUpdateToLocation
, but how can I use it with didSelectAnnotationView
?
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
//Did update to location
}
推薦答案
地圖視圖有一個(gè) selectedAnnotations
屬性,您可以在 didUpdateToLocation
方法中使用該屬性來(lái)判斷哪個(gè)注解獲取距離.
The map view has a selectedAnnotations
property that you can use in the didUpdateToLocation
method to tell which annotation to get the distance from.
(順便說(shuō)一句,如果您使用地圖視圖的 userLocation
,您可能希望使用地圖視圖的 didUpdateUserLocation
委托方法而不是 didUpdateToLocation
這是一個(gè) CLLocationManager
委托方法.)
(By the way, if you are using the map view's userLocation
, you might want to use the map view's didUpdateUserLocation
delegate method instead of didUpdateToLocation
which is a CLLocationManager
delegate method.)
在委托方法中,您可以檢查當(dāng)前是否有任何選擇的注解,如果有,則顯示到該注解的距離(否則說(shuō)未選擇注解").
In the delegate method, you can check if there is any currently selected annotation and, if so, show the distance to that annotation (otherwise say "no annotation selected").
您可能希望編寫(xiě)一個(gè)可以從 didSelectAnnotationView
和 didUpdateUserLocation
調(diào)用的通用方法,以減少代碼重復(fù).
You might want to write a common method that can be called from both didSelectAnnotationView
and didUpdateUserLocation
to reduce code duplication.
例如:
-(void)updateDistanceToAnnotation:(id<MKAnnotation>)annotation
{
if (annotation == nil)
{
distLabel.text = @"No annotation selected";
return;
}
if (mapView.userLocation.location == nil)
{
distLabel.text = @"User location is unknown";
return;
}
CLLocation *pinLocation = [[CLLocation alloc]
initWithLatitude:annotation.coordinate.latitude
longitude:annotation.coordinate.longitude];
CLLocation *userLocation = [[CLLocation alloc]
initWithLatitude:mapView.userLocation.coordinate.latitude
longitude:mapView.userLocation.coordinate.longitude];
CLLocationDistance distance = [pinLocation distanceFromLocation:userLocation];
[distLabel setText: [NSString stringWithFormat:@"Distance to point %4.0f m.", distance]];
}
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
if (mapView.selectedAnnotations.count == 0)
//no annotation is currently selected
[self updateDistanceToAnnotation:nil];
else
//first object in array is currently selected annotation
[self updateDistanceToAnnotation:[mapView.selectedAnnotations objectAtIndex:0]];
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
[self updateDistanceToAnnotation:view.annotation];
}
這篇關(guān)于用戶移動(dòng)時(shí)如何計(jì)算用戶位置到注釋的距離的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!