使用MKLocalSearch在地图上搜索位置 [英] Use MKLocalSearch to search for locations on a map
问题描述
我想使用MKLocalSearch
在地图中进行搜索.此功能在iOS 6.1+中可用.有人知道如何使用此方法吗?还是有人可以举一个如何使用MKLocalSearch
的示例吗?
I want to use MKLocalSearch
for searching in a Map. This functionality is available in iOS 6.1+. Does anybody know how to use this or can anybody give an example of how to use an MKLocalSearch
?
推荐答案
The API for 搜索咖啡馆:
您可以像这样指定搜索区域:
You can specify a region for the search like this: 您还可以从用户放大的 You can also take the region from an
响应对象,
The response object, an
The array of 搜索都柏林在地图视图和日志上放置图钉: Search for Dublin places a pin on the map view and logs:
在返回的对象中有很多额外的细节,尤其是在搜索企业时.这里有一些:
There are a load of extra details in the returned objects, especially if you search for businesses. Here are a few: 这篇关于使用MKLocalSearch在地图上搜索位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!MKLocalSearch
is fairly easy to understand. At its most basic, you
alloc-init
和 MKLocalSearchRequest
naturalLanguageQuery
设置为某个搜索词MKLocalSearch
对象MKMapItem
对象数组进行处理
alloc-init
an MKLocalSearchRequest
naturalLanguageQuery
to some search termMKLocalSearch
objectMKMapItem
objects in the response
// 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]);
}
}];
// 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];
MKMapView
中获取区域.这样会产生更好的结果:MKMapView
that the user has zoomed into. This will give better results:[searchRequest setRegion:self.mapView.region];
MKLocalSearchResponse
,包含一个 MKMapItem
对象(mapItems
)和称为boundingRegion
的MKCoordinateRegion
,该区域包含所有结果.您可以使用它来设置地图视图以显示所有结果:
MKLocalSearchResponse
, contains an array of MKMapItem
objects (mapItems
) and an MKCoordinateRegion
called boundingRegion
, which is a region that contains all the results. You can use it to set a map view to show all results:[self.mapView setRegion:response.boundingRegion];
MKMapItem
对象的数组不能放置在地图上(它们用于发送到Maps应用),但是每个对象都包含一个placemark
属性,可以将 添加到地图:
MKMapItem
objects can't be placed on a map (they're used for sending to the Maps app) but each contains a placemark
property which can be added to a map:[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]);
}
}];
Name: Dublin, Co. Dublin, MKAnnotation title: Dublin, Co. Dublin, Ireland
Coordinate: 53.344104 -6.267494
[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);
}