迅速向前地理定位和访问地标的经度和经度

时间:2015-11-12 21:29:16

标签: 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")
         }
     }
}

1 个答案:

答案 0 :(得分:0)

// 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