2013-09-24 73 views
-1

我有一個城市名稱,但不是該城市的經緯度值。我將如何獲取城市名稱中的經緯度值,然後將其保存到iOS中的變量中?我已經看到了地理反向編碼,但我所看到的例子與我所尋找的相反(他們從緯度/經度發現城市名稱)。如何從城市名稱中查找經緯度

+5

如果**反向地理編碼**與您所尋找的*相反*,那麼您認爲您所尋找的是什麼? – Joe

+0

我試圖在正確的方向上推動你。它被稱爲** [geocoding](https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLGeocoder_class/Reference/Reference.html#//apple_ref/occ/instm/CLGeocoder/geocodeAddressDictionary:completionHandler :)**和Jay Gajjar接近。 – Joe

+0

@ user2588945你是否檢查過文檔,嘗試過類似'geocodeAddressDictionary:completionHandler:'的例子,並且沒有工作? – Alladinian

回答

5

創建一個地理編碼器,並通過在字符串名稱:

一定要CoreLocation添加到您的項目和#import <CoreLocation/CoreLocation.h>,或者,如果你正在使用的Xcode 5,模塊的支持將自動對正確的框架,鏈接與@import CoreLocation;

NSString *city = @"New York, New York"; 
CLGeocoder *geocoder = [CLGeocoder new]; 
[geocoder geocodeAddressString:city completionHandler:^(NSArray *placemarks, NSError *error) { 
    if (error) { 
     NSLog(@"Error: %@", [error localizedDescription]); 
     return; // Bail! 
    } 

    if ([placemarks count] > 0) { 
     CLPlacemark *placemark = [placemarks lastObject]; // firstObject is iOS7 only. 
     NSLog(@"Location is: %@", placemark.location); 

    } 
}]; 

其給出

位置是:< + 40.72377900,-73.99128900> +/-100.00米(速度-1.00 mps/course -1.00)@ 9/24/13,2:34:18 PM英國夏令時間

我只是記錄地標,所以你得到更多,但你可以看到經緯度,長在那裏的紐約。

+1

然後你沒有運行iOS 7。改用'lastObject'來代替。 – Abizern

相關問題