【问题标题】:swift forward geolocating and accessing the placemark longitude and longitude快速前进地理定位和访问地标经度和经度
【发布时间】:2015-11-12 21:29:16
【问题描述】:

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

有什么想法吗?

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

【问题讨论】:

    标签: swift geocoding


    【解决方案1】:
    // 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-13
      相关资源
      最近更新 更多