2017-03-03 65 views
0

我有一個mapview,我添加了一個方法在用戶按下的位置放置一個pin。標註顯示圖像上顯示的位置地址。我可以使用mapkit獲得商店名稱/餐廳名稱嗎?(swift)

screenshot of my mapview with annotation pin and callout view.

而且我的代碼如下:

func onTapGestureRecognized(sender: UILongPressGestureRecognizer) { 

    self.mapView.removeAnnotations(mapView.annotations) 

    let location = tapRecognizer.location(in: mapView) 
    let coordinate = mapView.convert(location,toCoordinateFrom: mapView) 

    let getLat: CLLocationDegrees = coordinate.latitude 
    let getLon: CLLocationDegrees = coordinate.longitude 
    let theLocation: CLLocation = CLLocation(latitude: getLat, longitude: getLon) 

    let geoCoder = CLGeocoder() 

    geoCoder.reverseGeocodeLocation(theLocation, completionHandler: { (placemarks, error) -> Void in 

     // Place details 
     var placeMark: CLPlacemark! 
     placeMark = placemarks?[0] 
     var theLocationName = "" 
     var theStreetNumber = "" 
     var theStreet = "" 
     var theCity = "" 
     var theZip = "" 
     var theCountry = "" 

     // Address dictionary 
     print(placeMark.addressDictionary as Any) 

     // Location name 
     if let locationName = placeMark.name{ 
      theLocationName = locationName 
     } 

     if let streetNumber = placeMark.subThoroughfare{ 
      theStreetNumber = streetNumber 
     } 
     // Street address 
     if let street = placeMark.thoroughfare { 
      theStreet = street 
     } 

     // City 
     if let city = placeMark.locality { 
      theCity = city 
     } 

     // Zip code 
     if let zip = placeMark.postalCode{ 
      theZip = zip 
     } 

     // Country 
     if let country = placeMark.isoCountryCode{ 
      theCountry = country 
     } 

     let annotation = MKPointAnnotation() 
     annotation.title = theLocationName 
     annotation.subtitle = theStreetNumber + " " + theStreet + ", " + theCity + ", " + theCountry + ", " + theZip 
     if let location = placeMark.location { 
      annotation.coordinate = location.coordinate 
      // Display the annotation 
      self.mapView.showAnnotations([annotation], animated: true) 
     } 
    }) 

} 

正如你可以看到,當我試圖通過調用線(獲取地點名稱(((如果讓LOCATIONNAME =標。 name)))),我只能得到地址:「5197 Yonge St」,而不是餐廳名稱:「Pho 88 Restaurant」。

任何人都可以告訴我我做錯了嗎?或者它根本無法實現?謝謝!

+0

也許是因爲**地圖**的位置就是這樣嗎?不是**商業**地點? – dfd

+0

您的代碼可以與其他位置完美配合,嘗試換一個位置。 –

+0

@ArpitDongre是的,我已經嘗試過與其他地點,它有時會顯示一個地方或廣場的名稱(即城市中心北約克,梅爾伊士曼廣場),但從來沒有餐廳名稱或商店名稱(商業名稱) –

回答

0

我不能給你一個完整的答案,但我可能能指出你在正確的方向。據我所知,你只會得到一個單一的入口placemarks返回,但你可以使用MKLocalSearchRequest得到更完整的列表。挑戰將是如何將返回的值與您想要的值進行匹配 - 也許您必須要求用戶從簡短列表中進行選擇?另外,我認爲你需要指定你正在尋找哪種類型的機構。讓geoCoder會給我1-3 Some StreetMKLocalSearchRequest返回餐廳 - 這裏的東西,你可以包括上述

let request = MKLocalSearchRequest() 
request.naturalLanguageQuery = "restaurant" // or whatever you're searching for 
request.region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: getLat, longitude: getLon), span: self.mapView.region.span) 

let search = MKLocalSearch(request: request) 
search.start { response, error in 
    guard let response = response else { 
     print("There was an error searching for: \(request.naturalLanguageQuery) error: \(error)") 
       return 
    } 
    print("There are \(response.mapItems.count)") 
    for item in response.mapItems { 
     // You may be able to match the address to what the geoCode gives you 
     // or present the user with a list of options 
     print("\(item.name), \(item.placemark)") 
    } 
} 

您完成處理程序內。當我在測試這一點,地址並不總是在放大時投其所好,甚至在3 Some Street

相關問題