When you have address it's a string, how to get geocoding ?
In ios5 ago, use google service.
As was mentioned before, the best thing to do is to use the Google Maps API, it supports a lot of formats but for several reasons I chose to go with JSON.
So here are the steps to perform a JSON query to Google Maps and obtain the coordinate of the query. Note that not all the correct validations are done, this is only a Proof of concept.
1) Download a JSON framework/library for the iPhone, there are several, I chose to go with this one, it's very good and seems an active project, plus several comercial applications seem to be using it. So add it to your project ( instructions here ).
2) To query Google Maps for an address we need to build a request URL like this:http://maps.google.com/maps/geo?q=Paris+France
This url, will return a JSON object for the query "Paris+France".
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];
}
After we handle the UISearchBar search, we must make the request to Google Maps:
- (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];
}
We must of course then handle the response of the GoogleMaps server ( Note: a lot of validations missing)
//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];
}
Finally the function to zoom my map, which should by now be a trivial thing.
- (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];
}
Hope this helps someone because the JSON part was a real pain to figure out, the library is not very well documented in my opinion, still it's very good.
EDIT:
Modified one method name to "searchCoordinatesForAddress:" because of @Leo question. I have to say that this method is good as a proof of concept but if you plan to download big JSON files , you will have to append to a NSMutableData object to hold all the query to the google server. ( remember that HTTP queries come by pieces . )
In ios5 later
NSString *location = "some address, state, and zip";
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:location
completionHandler:^(NSArray* placemarks, NSError* error){
if (placemarks && placemarks.count > 0) {
CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
MKCoordinateRegion region = self.mapView.region;
region.center = placemark.region.center;
region.span.longitudeDelta /= 8.0;
region.span.latitudeDelta /= 8.0;
[self.mapView setRegion:region animated:YES];
[self.mapView addAnnotation:placemark];
}
}
];
ending,
What you're after is called geocoding or forward-geocoding. Reverse-geocoding is the process of converting a lat/long pair to street address.
iOS5 provides the CLGeocoder class for geocoding. MKPlacemark supports reverse-goecoding in iOS >= 3.0. The data involved of course is very large, so your app will generally need network access to the functionality.
A good place to start is Apple's Location Awareness Programming Guide. Also, there are LOTS of questions about this here on SO. http://stackoverflow.com/search?q=geocoding
Good luck!
相关推荐
TSClusterMapView, 用于iOS和OSX的动画集群 MKMapView TSClusterMapView - 带集群的MKMapView动画注释集群MKMapView子类基于 ADClusterMapView 。用法要运行示例项目,克隆 repo,然后从示例目录中运行 pod install ...
FlyoverKit:在 MKMapView 上实现 360° 俯视图.zip,360° flyover on a MKMapView
iPhone MKMAPView中根据gps坐标画路径,可以支持google map缩放
MTDirectionsKit, 在MKMapView之上,直接显示方向 为什么 MTDirectionsKit?为什么苹果没有为你提供直接显示在MKMapView之上的方法,强迫你在中 switch,只是为了引导你的用户在某个地方。 我们也是这样,所以我们...
MKMapView自定义calloutView,完全自定义布局。。
MKMapView在中国显示地图和卫星图存在位置上的偏差,因此很多时侯在定位跟踪的时候常常是卫星定位而标准地图显示,因此常发现位置点不居中显示,当然这个可以使用开启用户位置跟踪模式来进行定位。本DEMO也正是利用...
系统地图MKMapView的定位及导航 UISearchController模糊查询点击每个UITableViewCell 在地图上显示大头针 ,点击大头针的右边到这里去,调用系统导航,模糊查询数据源来自百度api 写的有点简单,有点乱
NULL 博文链接:https://chinaxxren.iteye.com/blog/1150309
在一个MKMapView上360° 展示一个高架公路
最近使用MKMapView,发现有内存泄露。经查发现属iOS系统 bug,详情我发表的文章:http://blog.csdn.net/rigel_xu/article/details/52146774
kingpin, 在MKMapView的地图注释集群库中, kingpin一种面向iOS的MKAnnotation集群库。 特性使用 2-d 树,以实现最大性能。无需子类化,使库易于与现有项目集成。安装 CocoaPods要在 Podfile 中获得稳定的发行版,请...
Working with Maps on iOS 9 with MapKit and the MKMapView Class Chapter 81. Working with MapKit Local Search in iOS 9 Chapter 82. Using MKDirections to get iOS 9 Map Directions and Routes Chapter 83. ...
位置伪造工具 FakeLocations ,FakeLocations 是一款用来伪造位置信息的工具。它可以在 MKMapView 和 CLLo...
Working with Maps on iOS 8 with MapKit and the MKMapView Class Chapter 77. Working with MapKit Local Search in iOS 8 Chapter 78. Using MKDirections to get iOS 8 Map Directions and Routes Chapter 79. ...
79. Working with Maps on iOS 10 with MapKit and the MKMapView Class 80. Working with MapKit Local Search in iOS 10 81. Using MKDirections to get iOS 10 Map Directions and Routes 82. An iOS 10 MapKit ...
自定义 callout! 通过手势 添加多个气泡
8.0以后的GPS定位问题,添加的一些配置信息
在 MKMapView 中实现地址选择和导航功能。具有以下特点: 1:支持ios5~7 2:自定义MKAnnotationView 3:支持第三方导航功能、包括高德、百度、苹果、谷歌,以及绘制路线,类似微信发生地址 4:支持滑动地图,选择...
MapViewPlus关于MapViewPlus为您提供了MapKit缺少的方法,它们是:imageForAnnotation和calloutViewForAnnotationView委托方法。 从imageForAnnotation返回任何UIImage(图像寿MapViewPlus关于MapViewPlus为您提供了...
RxMKMapView是MKMapView(MapKit) delegate的包装器,提供了React式委托代理以及可绑定的注释接口,以动态更改地图的“数据源”。 安装 RxMKMapView可通过。 要安装它,只需将以下行添加到您的Podfile中: pod ...