我目前正在执行如下任务:
MapView
和UIView
(包含2 textfields
,用户将在其上输入地址。)我找到了一些教程,告诉我如何使用路由,但主要是从当前位置绘制到某个目的地。
我的概念是从textfield
获取文本并将其转换为坐标并执行一些委托技术。
任何人都可以指导我如何解决它吗?
答案 0 :(得分:0)
您必须使用地理反向编码将您的城市名称转换为坐标。
geocoder.reverseGeocodeLocation(currentLocation, completionHandler: {
placemarks, error in
if error == nil && placemarks.count > 0 {
self.placeMark = placemarks.last as? CLPlacemark
self.adressLabel.text = "\(self.placeMark!.thoroughfare)\n\(self.placeMark!.postalCode) \(self.placeMark!.locality)\n\(self.placeMark!.country)"
self.manager.stopUpdatingLocation()
}
})
这是在路径坐标之间绘制线条的教程。 https://github.com/varshylmobile/MapManager
答案 1 :(得分:0)
let address : CLLocationCoordinate2D = self.getLocation(String(Anand Nagar, Rustambaug Road, Surat, India, 395006))
print(address.latitude)
print(address.longitude)
func getLocation(address: String) -> CLLocationCoordinate2D {
var center : CLLocationCoordinate2D = CLLocationCoordinate2D()
let esc_addr: String = address.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
let req: String = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=\(esc_addr)"
let responseData: NSData = NSData(contentsOfURL: NSURL(string: req)!)!
var _: NSError
var responseDictionary: [NSObject : AnyObject] = try! NSJSONSerialization.JSONObjectWithData(responseData, options: [] ) as! [NSObject : AnyObject]
var results: [AnyObject] = (responseDictionary["results"] as! [AnyObject])
if results.count > 0 {
var firstItem: [NSObject : AnyObject] = (results[0] as! [NSObject : AnyObject])
var geometry: [NSObject : AnyObject] = ((firstItem["geometry"] as! [NSObject : AnyObject]) )
var location: [NSObject : AnyObject] = ((geometry["location"] as! [NSObject : AnyObject]) )
print(location)
let lat = location["lat"]
let lng = location["lng"]
center.latitude = lat as! CLLocationDegrees
center.longitude = lng as! CLLocationDegrees
print(center)
return center
}
else {
center.latitude = 0
center.longitude = 0
return center
}
}
输出
21.2173191
72.8664716