如何在iOS中的两个位置之间获取路线?
我想使用Google API在两个CLLocationCoordinates之间绘制路线.目前,我正在使用" https://maps.googleapis.com/maps/api/directions/json ?"与起源和目的地,这不完全是我想要做的.我想传递我的位置而不是位置名称,然后进行路由.
I want to draw route between two CLLocationCoordinates Using Google API. Currently i am using "https://maps.googleapis.com/maps/api/directions/json?" with origin and destination and it is not exactly what i want to do. I want to pass my location instead of location name and then make a route.
预先感谢.
我关注了这 Google Maps SDK教程.并且在该路线用户指南中,必须将两个位置名称都添加到alertView
中.但是我对代码进行了一些修改,以便您可以直接获取从使用者的当前位置到输入的目的地位置的路线.
I followed THIS tutorial for Google Maps SDK. And In that tutorial for route user have to add both location name into alertView
. But I made some modification into the code so you can directly get the route from use's current location to entered destination location.
首先以这种方式从observeValueForKeyPath
方法的用户当前位置获取位置名称:
First of all get the name of the location from user's current location in observeValueForKeyPath
method this way:
var currentLocationName = String()
override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
if !didFindMyLocation {
let myLocation: CLLocation = change[NSKeyValueChangeNewKey] as! CLLocation
let geoCoder = CLGeocoder()
let location = CLLocation(latitude: myLocation.coordinate.latitude, longitude: myLocation.coordinate.longitude)
geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
let placeArray = placemarks as? [CLPlacemark]
// Place details
var placeMark: CLPlacemark!
placeMark = placeArray?[0]
// Address dictionary
println(placeMark.addressDictionary)
// Location name
if let locationName = placeMark.addressDictionary["Name"] as? NSString {
println(locationName)
}
// Street address
if let street = placeMark.addressDictionary["Thoroughfare"] as? NSString {
println(street)
}
// City
if let city = placeMark.addressDictionary["City"] as? NSString {
//get city name.
self.currentLocationName = city as String
println(city)
}
// Zip code
if let zip = placeMark.addressDictionary["ZIP"] as? NSString {
println(zip)
}
// Country
if let country = placeMark.addressDictionary["Country"] as? NSString {
println(country)
}
})
viewMap.camera = GMSCameraPosition.cameraWithTarget(myLocation.coordinate, zoom: 10.0)
viewMap.settings.myLocationButton = true
didFindMyLocation = true
}
}
现在在createRoute
方法中给出城市名称或您想要的名称,如下所示:
Now in createRoute
method give that city name or what ever you want as origin like this:
@IBAction func createRoute(sender: AnyObject) {
let addressAlert = UIAlertController(title: "Create Route", message: "Connect locations with a route:", preferredStyle: UIAlertControllerStyle.Alert)
addressAlert.addTextFieldWithConfigurationHandler { (textField) -> Void in
//give a origin for route
textField.text = self.currentLocationName
textField.userInteractionEnabled = false
}
addressAlert.addTextFieldWithConfigurationHandler { (textField) -> Void in
textField.placeholder = "Destination?"
}
let createRouteAction = UIAlertAction(title: "Create Route", style: UIAlertActionStyle.Default) { (alertAction) -> Void in
let origin = (addressAlert.textFields![0] as! UITextField).text as String
let destination = (addressAlert.textFields![1] as! UITextField).text as String
self.mapTasks.getDirections(origin, destination: destination, waypoints: nil, travelMode: nil, completionHandler: { (status, success) -> Void in
if success {
self.configureMapAndMarkersForRoute()
self.drawRoute()
self.displayRouteInfo()
}
else {
println(status)
}
})
}
let closeAction = UIAlertAction(title: "Close", style: UIAlertActionStyle.Cancel) { (alertAction) -> Void in
}
addressAlert.addAction(createRouteAction)
addressAlert.addAction(closeAction)
presentViewController(addressAlert, animated: true, completion: nil)
}
希望有帮助.