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 ...
在iOS开发中,`MKMapView`是苹果地图框架的一部分,用于在地图上显示地理位置和信息。本篇文章将深入探讨如何在`MKMapView`上绘制GPS轨迹路径,并支持类似Google Maps的缩放功能。 首先,我们需要了解`MKMapView`的...
【FlyoverKit】是一个开源项目,专为 iOS 开发者设计,它允许你在 `MKMapView` 上实现引人入胜的360°俯视效果。`MKMapView` 是苹果地图框架的一部分,用于展示地图、定位和导航。通过集成 FlyoverKit,开发者可以将...
MTDirectionsKit, 在MKMapView之上,直接显示方向 为什么 MTDirectionsKit?为什么苹果没有为你提供直接显示在MKMapView之上的方法,强迫你在中 switch,只是为了引导你的用户在某个地方。 我们也是这样,所以我们...
在iOS开发中,`MKMapView`是苹果提供的地图视图组件,用于展示地图信息和位置。`CalloutView`则是`MKMapView`中一个重要的特性,通常用来显示用户点击地图上的Annotation(标注)时出现的提示信息。默认情况下,`...
在iOS开发中,`MKMapView`是苹果提供的一个强大的地图视图组件,它允许开发者集成Apple Maps到自己的应用中,展示地理位置信息,并进行各种交互操作。本篇文章将深入讲解如何在`MKMapView`上实现长按屏幕添加大头针...
在iOS开发中,`MKMapView` 是苹果提供的地图视图组件,用于展示地图并进行相关交互。本主题聚焦于如何在`MKMapView`上同时显示多个自定义的气泡(callouts),并通过手势来实现这一功能。这在地图应用中非常常见,...
MKMapView在中国显示地图和卫星图存在位置上的偏差,因此很多时侯在定位跟踪的时候常常是卫星定位而标准地图显示,因此常发现位置点不居中显示,当然这个可以使用开启用户位置跟踪模式来进行定位。本DEMO也正是利用...
系统地图MKMapView的定位及导航 UISearchController模糊查询点击每个UITableViewCell 在地图上显示大头针 ,点击大头针的右边到这里去,调用系统导航,模糊查询数据源来自百度api 写的有点简单,有点乱
在iOS开发中,地图服务是常见且重要的功能之一,苹果提供的`MKMapView`类使得集成地图变得简单。然而,开发者可能会遇到一些棘手的问题,比如内存泄漏。标题为“MKMapView_Memory_Leak_Demo”的示例着重讨论了在使用...
在Swift开发中,使用ARKit(增强现实框架)与MKMapView(地图视图)结合,可以创建出一种独特的用户体验,即360°展示高架公路。这种技术将虚拟信息与现实世界无缝融合,使用户能够从不同角度观察和了解高架公路的...
10. **DPF.iOS.MKMapView.pas**:实现了MKMapView,这是苹果地图服务的一部分,允许在应用中嵌入地图并进行交互操作。 通过这些组件,开发者能够轻松地在Delphi环境下集成常见的iOS UI元素,如表格视图、导航控制器...
7. **DPF.iOS.MKMapView.pas**:对应于 iOS 的 MKMapView 控件,用于显示地图并实现相关功能,如定位、路线规划等。 8. **DPF.iOS.Common.pas**:这可能是包含通用函数和辅助类的源代码文件,供其他组件使用。 9. ...
标题提到的"GPS定位MKMapView,CoreLocation"涉及到两个主要的Apple框架:MapKit(MKMapView)和CoreLocation。接下来,我们将深入探讨这两个框架以及在iOS 8.0及更高版本中使用它们时可能遇到的问题和配置。 首先,...
`MKMapView`是苹果提供的一款强大的地图组件,用于在iOS应用中显示地图,并提供了丰富的自定义选项。本示例主要围绕`MKMapView`,通过`mkoverlay`和`mkpolyline`来讲解如何在地图上绘制路线图。 `MKMapView`是iOS ...
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 ...