【问题标题】:Can I get a store name/restaurant name with mapkit?(swift)我可以使用 mapkit 获得商店名称/餐厅名称吗?(swift)
【发布时间】:2017-03-03 04:24:37
【问题描述】:

我有一个地图视图,并且我添加了一种方法来在用户按下的位置放置一个图钉。如图所示,标注显示了该位置的地址。

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)
        }
    })

}

如您所见,当我尝试通过调用该行 (((( if let locationName = placeMark.name )))) 来获取位置名称时,我只能获取地址:“5197 Yonge St”,而不是餐厅名称:“Pho 88 餐厅”。

谁能告诉我哪里做错了?还是根本无法实现?谢谢!

【问题讨论】:

  • 可能是因为地图位置就是这样?不是商家位置?
  • 您的代码与其他位置完美配合,请尝试其他位置。
  • @ArpitDongre 是的,我尝试过其他位置,它有时会显示一个地方或广场的名称(即北约克市中心,梅尔伊士曼广场),但从来没有餐厅名称或商店名称(企业名称)
  • @dfd,嗯,如果是这个原因,你知道我怎样才能获得营业地点吗?

标签: ios swift mapkit core-location mkannotation


【解决方案1】:

我不能给你一个完整的答案,但我可以为你指出正确的方向。据我所知,placemarks 只会返回一个条目,但您可以使用MKLocalSearchRequest 获得更完整的列表。挑战将是如何将返回的值与您想要的准确匹配 - 也许您必须要求用户从一个短列表中进行选择?另外,我认为您需要指定要搜索的机构类型。您可以在上面的完成处理程序中包含以下内容

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)")
    }
}

当我对此进行测试时,地址并不总是匹配,即使在放大时也是如此 - 所以geoCoder 可能会给我1-3 Some StreetMKLocalSearchRequest 返回的餐厅是3 Some Street

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多