快速向前地理定位并访问地标经度和经度

标签 swift geocoding

我使用正向地理编码来获取事件的二维坐标。现在我试图获取事件地址的经度和纬度。我已成功创建一个包含坐标的地标,但我不知道如何将其从坐标更改为经度或纬度

有什么想法吗?

if let objects = objects {
    for object in objects {

        self.geocoder = CLGeocoder()

        //get address from object
        let COAddress = object.objectForKey("Address")as! String
        let COCity = object.objectForKey("City")as! String
        let COState = object.objectForKey("State")as! String
        let COZipCode = object.objectForKey("ZipCode")as! String
        let combinedAddress = "\(COAddress) \(COCity) \(COState) \(COZipCode)" //all parts of address
        print(combinedAddress)

        //make address a location

        self.geocoder.geocodeAddressString(combinedAddress, completionHandler: {(placemarks, error) -> Void in

            if(error != nil)
            {

                print("Error", error)
            }

            else if let placemark = placemarks?[0]
            {

                let placemark:CLPlacemark = placemarks![0]
                var coordinates:CLLocationCoordinate2D = placemark.location!.coordinate
                print("i got here", coordinates)

            }
        })

       let userLatitude = self.locationManager.location?.coordinate.latitude
       let userLongitude = self.locationManager.location?.coordinate.longitude
       let userLocation = CLLocation(latitude: userLatitude!, longitude: userLongitude!)

       // event location - THE ERROR IS BELOW
       let eventLatitude = placemarks.location?.cordinate.latitude
       let eventLongitude = placemarks.location?.coordinate.longitude
       let eventLocation = CLLocation(latitude: eventLatitude!, longitude: eventLongitude!)

       //Measuring my distance to my buddy's (in km)

       let distance = userLocation.distanceFromLocation(eventLocation) / 1000

         //Display the result in km
         print("The distance to event is ", distance)

         if (distance < 100) {

             print("yay")
         }
     }
}

最佳答案

// event location - THE ERROR IS BELOW
       let eventLatitude = placemarks.location?.cordinate.latitude
       let eventLongitude = placemarks.location?.coordinate.longitude
       let eventLocation = CLLocation(latitude: eventLatitude!, longitude: eventLongitude!)

我相信您需要访问 placemarks.first.locationplacemarks[0].location

关于快速向前地理定位并访问地标经度和经度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33681739/

相关文章:

swift - 需要澄清 Swift 中的 "@objc class"

geocoding - 如何使用 google maps api 跟踪在 google map 上共享位置的联系人的位置

google-maps - Google Javascript API地理编码限制

r - 使用for回路和georoute计算行驶距离时跳过错误

ios - 扩展 UIViewControllerAnimatedTransitioning 时出现段错误

iOS 8 Beta 6 - 在应用程序处于后台时处理 UILocalNotification

swift - 我可以根据子类对象的属性创建谓词吗?

ios - Swift 中的 ABAddressBook iOS 8 内存泄漏

google-maps - Google Geocoder 缺少类型地址组件

api - 地理编码服务-限制-Google VS Yahoo VS其他?