現在の場所 (都市名またはおそらく住所) を調べる必要がある iPhone アプリケーションがあります。しかし、私はそれを行うことができませんでした。現在の緯度と経度がわかります。緯度と経度を指定すると、現在の場所の名前を返すことができる Web サービス (できれば無料) はありますか?
4 に答える
あなたが探している機能は、リバース ジオコーディングと呼ばれます。
この機能を提供する Web サービスの一覧を次に示します。
最も一般的なのは、 Geonames.org、Google Maps Services API、および複数の座標のバッチ処理用のBatchgeo.comです。
iPhone OS 3.0 以降を対象としていると仮定するとMKReverseGeocoder
、MapKit フレームワークの一部であるこれを使用できます。開発者向けドキュメントには、サンプル プロジェクトが含まれています。MKReverserGeocoder クラス リファレンス
MKReverseGeocoder は優れたクラスですが、次のエラーを返す場合があります: Error Domain=PBRequesterErrorDomain Code=6001 "Operation could not be completed. (PBRequesterErrorDomain error 6001.)
上記の luvieere の提案は、開始するのに最適な場所です。これは、Google HTTP リバース ジオコーダーを使用して例として行ったことです。これは、私が書いた GoogleReverseGeocoder クラスの実装です。使用方法の完全な説明は、こちらでご覧いただけます。
// Send Google A Request
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%lf,%lf&output=csv&sensor=false",self.locationToGeocode.coordinate.latitude,self.locationToGeocode.coordinate.longitude];
NSURL *urlFromString = [NSURL URLWithString:urlString];
NSStringEncoding encodingType = NSUTF8StringEncoding;
NSString *reverseGeoString = [NSString stringWithContentsOfURL:urlFromString encoding:encodingType error:nil];
// Parse Out Response
NSArray *listItems = [reverseGeoString componentsSeparatedByString:@","];
NSArray *tempAddressArray = [[listItems objectAtIndex:2] componentsSeparatedByString:@"\""];
NSArray *tempCountryArray = [[listItems objectAtIndex:[listItems count]-1] componentsSeparatedByString:@"\""];
// Did Google Find Address? 200 is yes
if ([[listItems objectAtIndex:0] intValue] == 200)
{
// Set Class Member Variables
[self setGoogleReturnDidFindAddress:YES];
[self setGoogleReturnAddress:[tempAddressArray objectAtIndex:[tempAddressArray count]-1]];
[self setGoogleReturnCountry:[tempCountryArray objectAtIndex:0]];
[self setGoogleReturnCode:[[listItems objectAtIndex:0] intValue]];
[self setGoogleReturnAccuracy:[[listItems objectAtIndex:1] intValue]];
} else if ([[listItems objectAtIndex:0] intValue] > 600)
{
[self setGoogleReturnDidFindAddress:NO];
}