2017-03-03 85 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

相关问题