快速前进地理定位和访问地标经度和经度
Posted
技术标签:
【中文标题】快速前进地理定位和访问地标经度和经度【英文标题】: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")
【问题讨论】:
【参考方案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.location
或placemarks[0].location
【讨论】:
以上是关于快速前进地理定位和访问地标经度和经度的主要内容,如果未能解决你的问题,请参考以下文章