我想使用MKLocalSearch
在地圖中搜索。該功能在iOS 6.1+中可用。有誰知道如何使用這個或任何人都可以舉例說明如何使用MKLocalSearch
?使用MKLocalSearch搜索地圖上的位置
MKLocalSearchResponse documentation
我想使用MKLocalSearch
在地圖中搜索。該功能在iOS 6.1+中可用。有誰知道如何使用這個或任何人都可以舉例說明如何使用MKLocalSearch
?使用MKLocalSearch搜索地圖上的位置
MKLocalSearchResponse documentation
MKLocalSearch
的API很容易理解。在最基本的,你
alloc-init
的MKLocalSearchRequest
naturalLanguageQuery
一些搜索詞MKLocalSearch
對象MKMapItem
對象的數組做什麼
搜索咖啡館:
// 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
用戶有:
// 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]);
}
}];
您可以這樣搜索指定區域放大。這將給出更好的結果:
[searchRequest setRegion:self.mapView.region];
響應對象中,MKLocalSearchResponse
,包含MKMapItem
對象(mapItems
)和MKCoordinateRegion
稱爲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);
}
這裏的教程的是,在圍繞一個給定的位置的步驟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);
}
}];
請注意,比搜索不成功時,它不會返回空列表,但域錯誤MKErrorDomain
和代碼4
。