2017-03-03 7 views
0

지도보기가 있으며 사용자가 누른 위치에 핀을 놓을 수있는 방법을 추가했습니다. 콜 아웃은 이미지에 표시된 위치의 주소를 보여줍니다. 지도 키트로 상점 이름/레스토랑 이름을 얻을 수 있습니까? (신속)

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 = 위치 표시를 할 경우. 이름)))), 나는 단지 레스토랑 이름 대신에 "5197 Yonge St"라는 주소를 얻을 수있다 : "Pho 88 Restaurant".

내가 어디에서 잘못했는지 누가 알 수 있습니까? 또는 단순히 달성 될 수 없는가? 감사!

+0

아마도 **지도 ** 위치가 그거일까요? ** 비즈니스 ** 위치가 아닌가요? – dfd

+0

코드가 다른 위치에서 완벽하게 작동하지만 다른 위치에서 시도하십시오. –

+0

@ArpitDongre 예 다른 장소에서 시도한 적이 있습니다. 장소 또는 플라자 (예 : 도심 노스 요크, 멜 이스트만 스퀘어)의 이름을 표시하지만 식당 이름이나 상점 이름이 없습니다 (회사 이름) –

답변

0

완벽한 답변을 드릴 수는 없지만 올바른 방향으로 안내해 드릴 수 있습니다. 내가 볼 수있는 한 placemarks에 대해 단일 항목 만 반환되지만 수는이고 MKLocalSearchRequest을 사용하면 더 자세한 목록을 얻을 수 있습니다. 도전 과제는 반환 된 값을 정확히 원하는 것과 일치시키는 방법 일 것입니다. 아마도 사용자가 짧은 목록에서 선택하도록 요청해야합니까? 또한, 당신이 찾고있는 시설의 유형을 지정해야한다고 생각합니다. MKLocalSearchRequest에는 레스토랑이 (가) 반환하면서 geoCoder 나에게 1-3 Some Street을 줄 수 있도록 - 확대 할 때 여기에 내가이 테스트 때 당신이

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