2013-06-05 68 views
2

我正嘗試使用MapKit構建一個簡單的應用程序,該應用程序將在應用程序啓動時自動搜索特定位置,並在地圖上放置(多個)引腳。我看到各種方式來加載位置(緯度/經度等),但沒有什麼可以讓我搜索特定的商戶名稱,餐廳名稱等。在iOS地圖上搜索

我期待與Apple Maps一起工作。但是,如果Google地圖比這樣做更好解決問題。

任何幫助,將不勝感激。謝謝

+0

請分享你已經嘗試什麼樣的代碼,所以我們可以幫助你。謝謝! – bcr

回答

6

iOS> = 6.1提供了MKLocalSearch,MKLocalSearchRequest來搜索自然語言的興趣點。示例

MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init]; 
request.region = regionToSearchIn; 
request.naturalLanguageQuery = @"restaurants"; // or business name 
MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:request]; 
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) { 
    // do something with the results/error 
}]; 
0

執行以下步驟以顯示最近的地方。

使用谷歌API找到最近的地方Google Places API

在谷歌API提供緯度和龍和地址信息。

然後將LAt和Long值存儲到數組,然後使用此經緯度值可以在Apple地圖(iOS 6+)和Google Map(iOS 5)中顯示註釋。

+0

從iOS6開始根據Google Places TOS,這是不允許的。它表示,如果您顯示Google商家信息結果,則必須使用Google地圖。 – ilmiacs

+0

有關MapKit的原始問題,不是第三方服務。 Sanjit的答案在下面是正確的答案。 – thefaj

1

我知道它的晚了,但希望這有助於!

[super viewDidLoad]; 
[self.searchDisplayController setDelegate:self]; 
[self.ibSearchBar setDelegate:self]; 

self.ibMapView.delegate=self; 

// Zoom the map to current location. 
[self.ibMapView setShowsUserLocation:YES]; 
[self.ibMapView setUserInteractionEnabled:YES]; 
[self.ibMapView setUserTrackingMode:MKUserTrackingModeFollow]; 



CLLocationManager *locationManager = [[CLLocationManager alloc] init]; 
locationManager.delegate=self; 

[locationManager startUpdatingLocation]; 
[self.ibMapView setRegion:MKCoordinateRegionMake(locationManager.location.coordinate, MKCoordinateSpanMake(0.2, 0.2))]; 

MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init]; 
request.region = self.ibMapView.region; 
request.naturalLanguageQuery = @"restaurant"; 

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
localSearch = [[MKLocalSearch alloc] initWithRequest:request]; 

[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error){ 

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 

    results = response; 
    if (response.mapItems.count == 0) 
     NSLog(@"No Matches"); 
    else 
     for (MKMapItem *item in response.mapItems) 
     { 
      NSLog(@"name = %@", item.name); 
      NSLog(@"Phone = %@", item.phoneNumber); 

      [_matchingItems addObject:item]; 
      MKPointAnnotation *annotation = 
      [[MKPointAnnotation alloc]init]; 
      annotation.coordinate = item.placemark.coordinate; 
      annotation.title = item.name; 
      [self.ibMapView addAnnotation:annotation]; 
     } 
}]; 

}