She*_*han 9 objective-c mapkit mkmapview ios mklocalsearch
我想MKLocalSearch用于在地图中搜索.此功能在iOS 6.1+中可用.有谁知道如何使用这个或任何人都可以举例说明如何使用MKLocalSearch?
nev*_*ing 23
API MKLocalSearch非常容易理解.最基本的,你
alloc-init 一个 MKLocalSearchRequestnaturalLanguageQuery为某个搜索词MKLocalSearch对象MKMapItem对响应中的对象
数组执行某些操作搜索咖啡馆:
// Create a search request with a string
MKLocalSearchRequest *searchRequest = [[MKLocalSearchRequest alloc] init];
[searchRequest setNaturalLanguageQuery:@"Cafe"];
// Create the local search to perform the search
MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:searchRequest];
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
if (!error) {
for (MKMapItem *mapItem in [response mapItems]) {
NSLog(@"Name: %@, Placemark title: %@", [mapItem name], [[mapItem placemark] title]);
}
} else {
NSLog(@"Search Request Error: %@", [error localizedDescription]);
}
}];
Run Code Online (Sandbox Code Playgroud)
您可以为此搜索指定区域:
// Search for Cafes in Paris
MKLocalSearchRequest *searchRequest = [[MKLocalSearchRequest alloc] init];
[searchRequest setNaturalLanguageQuery:@"Cafe"];
CLLocationCoordinate2D parisCenter = CLLocationCoordinate2DMake(48.8566667, 2.3509871);
MKCoordinateRegion parisRegion = MKCoordinateRegionMakeWithDistance(parisCenter, 15000, 15000);
[searchRequest setRegion:parisRegion];
Run Code Online (Sandbox Code Playgroud)
您还可以从MKMapView用户放大的区域中获取该区域.这将带来更好的结果:
[searchRequest setRegion:self.mapView.region];
Run Code Online (Sandbox Code Playgroud)
响应对象a MKLocalSearchResponse包含一个MKMapItem对象(mapItems)和一个MKCoordinateRegion被调用的数组boundingRegion,该区域包含所有结果.您可以使用它来设置地图视图以显示所有结果:
[self.mapView setRegion:response.boundingRegion];
Run Code Online (Sandbox Code Playgroud)
MKMapItem对象数组不能放在地图上(它们用于发送到地图应用程序),但每个对象都包含一个可以添加到地图的placemark属性:
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
if (!error) {
for (MKMapItem *mapItem in [response mapItems]) {
NSLog(@"Name: %@, MKAnnotation title: %@", [mapItem name], [[mapItem placemark] title]);
NSLog(@"Coordinate: %f %f", [[mapItem placemark] coordinate].latitude, [[mapItem placemark] coordinate].longitude);
// Should use a weak copy of self
[self.mapView addAnnotation:[mapItem placemark]];
}
} else {
NSLog(@"Search Request Error: %@", [error localizedDescription]);
}
}];
Run Code Online (Sandbox Code Playgroud)
搜索都柏林在地图视图和日志上放置一个图钉:
Name: Dublin, Co. Dublin, MKAnnotation title: Dublin, Co. Dublin, Ireland
Coordinate: 53.344104 -6.267494
Run Code Online (Sandbox Code Playgroud)
返回的对象中有大量额外的详细信息,尤其是在搜索业务时.以下是一些:
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
if (!error) {
NSLog(@"Results: %@", [response mapItems]);
MKMapItem *mapItem = [[response mapItems] objectAtIndex:0];
NSLog(@"Name:%@ Phone:%@ URL:%@", [mapItem name], [mapItem phoneNumber], [mapItem url]);
NSLog(@"Placemark: %@", [mapItem placemark]);
MKPlacemark *placemark = [mapItem placemark];
NSLog(@"Placemark Address: %@", [placemark addressDictionary]);
MKCoordinateRegion boundingRegion = [response boundingRegion];
NSLog(@"Bounds: %f %f", boundingRegion.span.latitudeDelta, boundingRegion.span.longitudeDelta);
}
Run Code Online (Sandbox Code Playgroud)
以下是在给定位置周围1公里范围内搜索咖啡馆的示例:
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(11.567898, 104.894430);
request.naturalLanguageQuery = @"cafe";
request.region = MKCoordinateRegionMakeWithDistance(location, 1000, 1000);
MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request];
[search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error){
for (MKMapItem *item in response.mapItems) {
NSLog(@"%@", item.name);
}
}];
Run Code Online (Sandbox Code Playgroud)
请注意,与搜索失败的情况相比,它不会返回空列表,而是返回域MKErrorDomain和代码错误4.
| 归档时间: |
|
| 查看次数: |
14362 次 |
| 最近记录: |