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

用戶移動(dòng)時(shí)如何計(jì)算用戶位置到注釋的距離

How to calculate distance from user location to annotation when user moves(用戶移動(dòng)時(shí)如何計(jì)算用戶位置到注釋的距離)
本文介紹了用戶移動(dòng)時(shí)如何計(jì)算用戶位置到注釋的距離的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(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è)可以從 didSelectAnnotationViewdidUpdateUserLocation 調(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)!

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

相關(guān)文檔推薦

Stop a UITableView from automatically scrolling(阻止 UITableView 自動(dòng)滾動(dòng))
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 模擬器上運(yùn)行時(shí),使用 iOS 6.0 SDK 并為 iOS 5 Target 構(gòu)建會(huì)導(dǎo)致 UIScrollView setMinimumZ
Create partial-screen UIPageViewController programmatically(以編程方式創(chuàng)建部分屏幕 UIPageViewController)
how to make an ImageView zoomable with or without ScrollView.?(如何使用或不使用 ScrollView 使 ImageView 可縮放?)
UIImageView zoom and pinch in UIScrollView(UIImageView 在 UIScrollView 中縮放和捏合)
主站蜘蛛池模板: 在线观看日韩精品视频 | 91在线精品秘密一区二区 | 国产999精品久久久久久 | 国产精品精品3d动漫 | 日韩av一区二区在线观看 | 免费在线观看av网址 | 久久久久国产一区二区三区四区 | 日韩欧美网 | 久久国产精品99久久久久久丝袜 | 国产精品久久久久久久久久三级 | 91影院在线观看 | 亚洲三区在线观看 | 一区二区三区国产好 | 久久久久亚洲 | 欧美国产免费 | 91一区二区 | 成人av一区 | 欧美视频1区 | 国产成人精品免费视频大全最热 | 日本高清不卡视频 | 四虎在线观看 | 男人的天堂中文字幕 | 午夜视频一区二区三区 | 在线观看一区 | 久久三区 | 精品成人 | 不卡一二区 | 婷婷毛片 | www.日韩av.com | 中文字幕一区二区三区乱码在线 | 国内av在线 | 色婷婷国产精品 | 日韩av在线一区二区 | 欧美激情一区二区三区 | 操人视频在线观看 | 久草免费在线 | 亚洲精品9999 | 日本a级大片 | 亚洲国产精品人人爽夜夜爽 | 日韩成人在线观看 | 亚洲精品片 |