【问题标题】:Swift ReverseGeocode CLPlacemark areasOfInterest almost always nil. Should I be using something else?Swift ReverseGeocode CLPlacemark areaOfInterest 几乎总是为零。我应该使用别的东西吗?
【发布时间】:2018-03-10 06:16:28
【问题描述】:

我正在使用 CLGeocoder 和 reverseGeocodeLocation 来获取我的 CLPlacemark。为什么名称大多以地址形式返回,而 areaOfInterest 以 nil 形式返回?例如……areasOfInterest 出现在真正重要的事情上,比如苹果总部和机场,这类事情,但沃尔玛、Publix blah blah 等商店是零。我应该寻找另一种方式吗?我是否期望获得比此方法提供的更多信息?我的意思是,Apple 在他们的地图上有这些兴趣点,我应该尝试其他方法来获取这些信息吗?

以下是我在我所在地区尝试过的一些地点 lat longs,它们没有恢复商店名称。这些来自谷歌,当放入苹果地图时,它是你在正确位置的顶部,但它也不相关......这让我觉得我应该做一些不同于返回商店名称的事情。描述或类别等其他信息也会很好。

注意:我只是想要信息,我不想将其放在地图或任何东西上。

沃尔玛:北纬 35.0944°,西经 85.3319°

水族馆:35.0558° N,85.3111° W

Publix:北纬 35.0651°,西经 85.3083°

我的代码的一小部分。所有作品都只是想让你知道我带回了什么以及如何带回来。

CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks, error)->Void in
                if placemarks != nil
                {
                    if error == nil && placemarks!.count >= 1 {
                        let thePlacemarks = placemarks![0] as CLPlacemark
                        print(placemarks)
                        print(thePlacemarks.areasOfInterest?.description)
                        print(thePlacemarks.administrativeArea?.description)
                        print(thePlacemarks.areasOfInterest?.description)
                        print(thePlacemarks.country?.description)
                        print(thePlacemarks.inlandWater?.description)
                        print(thePlacemarks.isoCountryCode?.description)
                        print(thePlacemarks.locality?.description)
                        print(thePlacemarks.location?.description)
                        print(thePlacemarks.name?.description)
                        print(thePlacemarks.ocean?.description)
                        print(thePlacemarks.subAdministrativeArea?.description)
                        print()
                    }
                }
            })

任何帮助都会很棒!

谢谢!

【问题讨论】:

    标签: xcode8 swift4 reverse-geocoding clplacemark


    【解决方案1】:

    所以...不一定理想,但通过使用 mapkit,我能够执行 MKLocalSearch 来获得我想要的东西。这只有效,因为我的代码中有一个我感兴趣的特定位置的数组。请参阅下面的代码。

    Import Mapkit
    let listArr = ["Walmart", "Publix","Game Stop"]
    

    然后在你的 ViewController 中的某个地方

    func searchArr() //This function will iterate through the array and see if any of the locations are within 30 meters
    {
        for element in listsArr
        {
              let request = MKLocalSearchRequest()
              request.naturalLanguageQuery = "\(element)"
              request.region = MKCoordinateRegionMakeWithDistance(currentCoordinates, 3200, 3200)
              MKLocalSearch(request: request).start { (response, error) in
    
                  guard error == nil else {print()return}
                  guard let response = response else {return}
                  guard response.mapItems.count > 0 else {return}
                  print(response.mapItems[0])
                  let coord1 = currentCoordinates
                  let coord2 = response.mapItems[0].placemark.coordinate
                  let distance = self.calculateDistance(fromlat: currentCoordinates.latitude, fromlon: currentCoordinates.longitude, tolat: response.mapItems[0].placemark.coordinate.latitude, tolon: response.mapItems[0].placemark.coordinate.longitude)
    
              if distance > 30
              {
                   print("the distance between the two points is: \(distance) meters")
    
              }            
          }
    }
    

    这是我发现的一个小函数,用于获取两个坐标之间的距离。

    func calculateDistance(fromlat : Double, fromlon : Double, tolat : Double, tolon : Double) -> Double {
    
        let  DEG_TO_RAD = 0.017453292519943295769236907684886
        let  EARTH_RADIUS_IN_METERS = 6372797.560856
    
        let latitudeArc : Double   = (fromlat - tolat) * DEG_TO_RAD
        let longitudeArc : Double  = (fromlon - tolon) * DEG_TO_RAD
        var latitudeH : Double   = sin(latitudeArc * 0.5)
        latitudeH  *= latitudeH
        var lontitudeH : Double  = sin(longitudeArc * 0.5)
        lontitudeH *= lontitudeH
        let tmp : Double  = cos(fromlat*DEG_TO_RAD) * cos(tolat*DEG_TO_RAD)
        return EARTH_RADIUS_IN_METERS * 2.0 * asin(sqrt(latitudeH + tmp*lontitudeH))
    
    
    }
    

    【讨论】:

    • 更正... if distance > 30 应该是
    猜你喜欢
    • 2011-05-31
    • 2021-04-06
    • 2012-02-24
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多