好的,回答我自己的问题:
如前所述,最好的办法是使用 Google Maps API,
它支持很多格式,但出于几个原因,我选择使用 JSON。
以下是对 Google 地图执行 JSON 查询并获取查询坐标的步骤。请注意,并非所有正确的验证都已完成,这只是一个概念证明。
1) 为 iPhone 下载一个 JSON 框架/库,有好几个,我选择了this one,它非常好,似乎是一个活跃的项目,加上几个商业应用程序似乎正在使用它。所以把它添加到你的项目中(指令here)。
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 的问题,将一个方法名称修改为“searchCoordinatesForAddress:”。我不得不说,这种方法作为概念证明很好,但是如果您打算下载大的 JSON 文件,则必须附加到 NSMutableData 对象以将所有查询保存到 google 服务器。 (请记住,HTTP 查询是零散的。)