我想MKLocalSearch
用于在地图中搜索。此功能在 iOS 6.1+ 中可用。有人知道如何使用它,或者任何人都可以举一个如何使用的例子MKLocalSearch
吗?
问问题
16022 次
3 回答
24
API forMKLocalSearch
相当容易理解。最基本的,你
alloc-init
一个MKLocalSearchRequest
- 将其设置
naturalLanguageQuery
为某个搜索词 - 使用搜索请求初始化
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]);
}
}];
您可以像这样为搜索指定一个区域:
// 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
用户放大的区域中获取区域。这将产生更好的结果:
[searchRequest setRegion:self.mapView.region];
响应对象 anMKLocalSearchResponse
包含一个对象数组MKMapItem
( mapItems
) 和一个MKCoordinateRegion
called boundingRegion
,它是一个包含所有结果的区域。您可以使用它来设置地图视图以显示所有结果:
[self.mapView setRegion:response.boundingRegion];
对象数组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]);
}
}];
搜索都柏林会在地图视图和日志上放置一个图钉:
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);
}
于 2014-04-29T14:51:41.197 回答
7
这是一个在给定位置周围 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);
}
}];
请注意,当搜索不成功时,它不会返回空列表,而是返回 domainMKErrorDomain
和 code错误4
。
于 2014-04-07T07:34:29.280 回答
0
这是Localsearch的教程
于 2013-04-09T06:59:15.370 回答