久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

用戶移動時如何計算用戶位置到注釋的距離

How to calculate distance from user location to annotation when user moves(用戶移動時如何計算用戶位置到注釋的距離)
本文介紹了用戶移動時如何計算用戶位置到注釋的距離的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我在 mapView 上有一個用戶位置(藍點)和注釋.選擇注釋后,我將文本設置為 distLabel -到點 %4.0f m. 的距離.".用戶移動時如何更新該文本標簽?

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

我知道有一個函數 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
}

推薦答案

地圖視圖有一個 selectedAnnotations 屬性,您可以在 didUpdateToLocation 方法中使用該屬性來判斷哪個注解獲取距離.

The map view has a selectedAnnotations property that you can use in the didUpdateToLocation method to tell which annotation to get the distance from.

(順便說一句,如果您使用地圖視圖的 userLocation,您可能希望使用地圖視圖的 didUpdateUserLocation 委托方法而不是 didUpdateToLocation 這是一個 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.)

在委托方法中,您可以檢查當前是否有任何選擇的注解,如果有,則顯示到該注解的距離(否則說未選擇注解").

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").

您可能希望編寫一個可以從 didSelectAnnotationViewdidUpdateUserLocation 調用的通用方法,以減少代碼重復.

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

這篇關于用戶移動時如何計算用戶位置到注釋的距離的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Stop a UITableView from automatically scrolling(阻止 UITableView 自動滾動)
iOS UIScrollView Lazy Loading(iOS UIScrollView 延遲加載)
using iOS 6.0 SDK and building for iOS 5 Target causes UIScrollView setMinimumZoomScale to fail when running on iOS 5 simulator(在 iOS 5 模擬器上運行時,使用 iOS 6.0 SDK 并為 iOS 5 Target 構建會導致 UIScrollView setMinimumZ
Create partial-screen UIPageViewController programmatically(以編程方式創建部分屏幕 UIPageViewController)
how to make an ImageView zoomable with or without ScrollView.?(如何使用或不使用 ScrollView 使 ImageView 可縮放?)
UIImageView zoom and pinch in UIScrollView(UIImageView 在 UIScrollView 中縮放和捏合)
主站蜘蛛池模板: 国产精品精品视频一区二区三区 | 熟女毛片 | 中文字幕免费视频 | 国产精品自在线 | 日韩成人影院 | 美女久久久| 亚洲精彩免费视频 | 国产精久久久久久 | 三区在线观看 | 精品国产一区二区三区久久久蜜月 | 日韩av免费看 | 九九激情视频 | 中文字幕日韩三级 | 免费一区二区三区 | 国产欧美日韩在线观看 | 久久久久久亚洲 | 日本国产精品视频 | 精品久久国产视频 | 欧美日韩电影一区二区 | 久久国产精品一区二区三区 | 特级做a爱片免费69 精品国产鲁一鲁一区二区张丽 | 国产主播第一页 | 日韩一区二区三区视频在线观看 | 国产免费拔擦拔擦8x高清 | 久久久久久久一区 | 欧美视频三区 | 超碰在线97国产 | 九九九久久国产免费 | 一区二区三区免费 | 嫩草最新网址 | 精品国产乱码久久久久久久久 | 欧美日韩精品 | 中文字幕一区二区三区四区 | 久久久网| 日本一区二区三区在线观看 | 九九久久精品 | 美国一级片在线观看 | 亚洲欧美精品在线观看 | 欧美日韩中 | 成人免费毛片片v | 免费一看一级毛片 |