gpt4 book ai didi

ios - 使用 MKLocalSearch 在 map 上搜索位置

转载 作者:技术小花猫 更新时间:2023-10-29 11:10:36 29 4
gpt4 key购买 nike

我想使用 MKLocalSearch 在 map 中进行搜索。此功能在 iOS 6.1+ 中可用。有谁知道如何使用这个或者谁能举例说明如何使用 MKLocalSearch

MKLocalSearchResponse documentation

最佳答案

MKLocalSearch 的 API很容易理解。最基本的,你

  1. alloc-init MKLocalSearchRequest
  2. 将其 naturalLanguageQuery 设置为某个搜索词
  3. 使用搜索请求初始化一个MKLocalSearch对象
  4. 告诉本地搜索开始,将完成处理程序传递给它
  5. 对响应中的 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];

响应对象,一个 MKLocalSearchResponse , 包含 MKMapItem 的数组对象 (mapItems) 和一个名为 boundingRegionMKCoordinateRegion,这是一个包含所有结果的区域。您可以使用它来设置 map View 以显示所有结果:

[self.mapView setRegion:response.boundingRegion];

MKMapItem 对象数组不能放置在 map 上(它们用于发送到 map 应用程序)但每个对象都包含一个 placemark 属性,该属性 < em>可以添加到 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]);
}
}];

搜索都柏林在 map View 和日志上放置一个图钉:

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);
}

关于ios - 使用 MKLocalSearch 在 map 上搜索位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13798804/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com