好吧,回答我自己的问题:
正如之前提到的,最好的办法是使用 Google Maps API,
它支持很多格式,但出于多种原因我选择使用 JSON。
以下是对 Google 地图执行 JSON 查询并获取查询坐标的步骤。请注意,并未完成所有正确的验证,这只是概念证明。
1)下载iPhone的JSON框架/库,有几个,我选择了this one http://code.google.com/p/json-framework/,它非常好,似乎是一个活跃的项目,而且几个商业应用程序似乎正在使用它。因此,将其添加到您的项目中(说明here http://code.google.com/p/json-framework/wiki/InstallationInstructions ).
2) 要查询 Google 地图的地址,我们需要构建一个如下所示的请求 URL:http://maps.google.com/maps/geo?q=Paris+France http://maps.google.com/maps/geo?q=Paris+France
此 url 将返回查询“Paris+France”的 JSON 对象。
3) Code:
//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 部分很难弄清楚,在我看来,该库的记录不是很好,但它仍然非常好。
EDIT:
由于@Leo 问题,将一种方法名称修改为“searchCooperativesForAddress:”。我不得不说,这种方法作为概念证明很好,但如果您计划下载大 JSON 文件,则必须附加到 NSMutableData 对象以保存对 google 服务器的所有查询。 (请记住,HTTP 查询是分段进行的。)