2014-02-25 30 views
2

我有這樣的疑問:請求mapkit註釋到服務器的位置和縮放級別

我有一個API服務器請求最近的位置(經緯度&長)和距離(公里)

我會點當用戶在地圖上做平底鍋的時候調用這個API ..所以我會計算在縮放級別函數中的距離參數..

我該如何獲得這個?

在這一刻我有這個MapKit委託方法:

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated 
{ 
    MKZoomScale currentZoomScale = mapView.bounds.size.width/mapView.visibleMapRect.size.width; 
NSLog(@"currentZoom:%f", currentZoomScale); 
    [self.dataReader sendServerRequestWithCoordinate:mapView.region.center andDistance:[self getDistanceByZoomLevel:currentZoomScale]; 

} 

- (float) getDistanceByZoomLevel:(MKZoomScale) zoomLevel { 
/// ?????? //// 
} 

回答

0

你並不需要計算基於「縮放級別」或「縮放比例」的距離。

地圖工具包和核心位置具有計算給定座標或地圖點距離的方法和函數。

假設您要使用當前可見地圖所覆蓋的對角線距離(從左上角到右下角)。

的角座標(CLLocationCoordinate2D多個)可以從地圖視圖的region屬性獲得,然後可以使用distanceFromLocation方法來計算兩個座標之間的距離。

可以從地圖視圖的visibleMapRect屬性獲取角點地圖點(MKMapPoint s),然後您可以使用MKMetersBetweenMapPoints函數獲取它們之間的距離。

下面是一個例子使用地圖兩點:

MKMapRect vmr = mapView.visibleMapRect; 

//vmr.origin is the top-left corner MKMapPoint 

MKMapPoint bottomRightMapPoint = 
    MKMapPointMake(vmr.origin.x + vmr.size.width, 
        vmr.origin.y + vmr.size.height); 

CLLocationDistance distanceMeters = 
    MKMetersBetweenMapPoints(vmr.origin, bottomRightMapPoint); 

當然,如果你想要公里而非米,1000.0分distanceMeters。另外,如果通過「距離」,你實際上想要半徑(距離中心點的距離),那麼也將distanceMeters除以2.0。

相關問題