わかりました、私自身の質問に答えるために:
前述したように、最善の方法は Google Maps API を使用することです。Google Maps API は多くの形式をサポートしていますが、いくつかの理由から JSON を使用することにしました。
Google マップに対して JSON クエリを実行し、クエリの座標を取得する手順は次のとおりです。すべての正しい検証が行われているわけではないことに注意してください。これは概念実証にすぎません。
1)iPhone用のJSONフレームワーク/ライブラリをダウンロードしてください。いくつかありますが、私はこれを選択しました.これは非常に優れており、アクティブなプロジェクトのようです.さらに、いくつかの商用アプリケーションがそれを使用しているようです. それをプロジェクトに追加します (手順はこちら)。
2) Google マップに住所を問い合わせるには、次のようなリクエスト URL を作成する必要があります:
http://maps.google.com/maps/geo?q=Paris+France
この URL は、クエリ "Paris+France" の JSON オブジェクトを返します。
3) コード:
//Method to handle the UISearchBar "Search",
- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar
{
//Perform the JSON query.
[self searchCoordinatesForAddress:[searchBar text]];
//Hide the keyboard.
[searchBar resignFirstResponder];
}
UISearchBar 検索を処理した後、Google マップにリクエストを送信する必要があります。
- (void) searchCoordinatesForAddress:(NSString *)inAddress
{
//Build the string to Query Google Maps.
NSMutableString *urlString = [NSMutableString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@?output=json",inAddress];
//Replace Spaces with a '+' character.
[urlString setString:[urlString stringByReplacingOccurrencesOfString:@" " withString:@"+"]];
//Create NSURL string from a formate URL string.
NSURL *url = [NSURL URLWithString:urlString];
//Setup and start an async download.
//Note that we should test for reachability!.
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection release];
[request release];
}
もちろん、GoogleMaps サーバーの応答を処理する必要があります (注: 多くの検証が欠落しています)。
//It's called when the results of [[NSURLConnection alloc] initWithRequest:request delegate:self] come back.
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
//The string received from google's servers
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//JSON Framework magic to obtain a dictionary from the jsonString.
NSDictionary *results = [jsonString JSONValue];
//Now we need to obtain our coordinates
NSArray *placemark = [results objectForKey:@"Placemark"];
NSArray *coordinates = [[placemark objectAtIndex:0] valueForKeyPath:@"Point.coordinates"];
//I put my coordinates in my array.
double longitude = [[coordinates objectAtIndex:0] doubleValue];
double latitude = [[coordinates objectAtIndex:1] doubleValue];
//Debug.
//NSLog(@"Latitude - Longitude: %f %f", latitude, longitude);
//I zoom my map to the area in question.
[self zoomMapAndCenterAtLatitude:latitude andLongitude:longitude];
[jsonString release];
}
最後に、マップをズームする機能です。これは今では些細なことです。
- (void) zoomMapAndCenterAtLatitude:(double) latitude andLongitude:(double) longitude
{
MKCoordinateRegion region;
region.center.latitude = latitude;
region.center.longitude = longitude;
//Set Zoom level using Span
MKCoordinateSpan span;
span.latitudeDelta = .005;
span.longitudeDelta = .005;
region.span = span;
//Move the map and zoom
[mapView setRegion:region animated:YES];
}
JSONの部分を理解するのは本当に面倒だったので、これが誰かに役立つことを願っています.ライブラリは私の意見ではあまり文書化されていませんが、それでも非常に優れています.
編集:
@Leo の質問により、1 つのメソッド名を「searchCoordinatesForAddress:」に変更しました。この方法は概念実証としては優れていると言わざるを得ませんが、大きな JSON ファイルをダウンロードする予定がある場合は、Google サーバーへのすべてのクエリを保持するために NSMutableData オブジェクトに追加する必要があります。(HTTP クエリはバラバラになることを覚えておいてください。)