无论我给地理编码器的地址是什么([geocoder geocodeAddressString:completionHandler:),它总是只将一个对象放在地标数组中。
我有什么方法可以获得多个结果(例如在地图应用程序中),用户可以从中选择一个?
无论我给地理编码器的地址是什么([geocoder geocodeAddressString:completionHandler:),它总是只将一个对象放在地标数组中。
我有什么方法可以获得多个结果(例如在地图应用程序中),用户可以从中选择一个?
Apple 的本地地理编码服务由MapKit 框架提供。该框架中的重要对象是MKLocalSearch
,它可以对地址进行地理编码并返回多个结果。
MKLocalSearch 返回 10 个mapItems
类型的结果MKMapItem
。每个 MKMapItem 都包含一个MKPlacemark
对象,它是CLPlacemark
.
这是一个使用 MapKit 的示例MKLocalSearch
:
MKLocalSearchRequest* request = [[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery = @"Calgary Tower";
request.region = MKCoordinateRegionMakeWithDistance(loc, kSearchMapBoundingBoxDistanceInMetres, kSearchMapBoundingBoxDistanceInMetres);
MKLocalSearch* search = [[MKLocalSearch alloc] initWithRequest:request];
[search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
yourArray = response.mapItems; // array of MKMapItems
// .. do you other logic here
}];
我对数据包做了一些嗅探,似乎 CLGeocoder 没有连接到 Google 的地理编码服务,而是连接到 Apple 的。我还注意到我每次只从那里得到一个地标。
如果你想要更复杂的东西,你应该使用谷歌或其他地理编码。我使用SVGeocoder (https://github.com/samvermette/SVGeocoder),它具有与 CLGeocoder 非常相似的 API。