如何在iOS中的两个位置之间获取路线?

编程入门 行业动态 更新时间:2024-10-21 03:52:55
本文介绍了如何在iOS中的两个位置之间获取路线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用Google API在两个CLLocationCoordinates之间绘制路线.目前,我正在使用" maps.googleapis/maps/api/directions/json ?"与起源和目的地,这不完全是我想要做的.我想传递我的位置而不是位置名称,然后进行路由.

I want to draw route between two CLLocationCoordinates Using Google API. Currently i am using "maps.googleapis/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) }

希望有帮助.

更多推荐

如何在iOS中的两个位置之间获取路线?

本文发布于:2023-11-12 20:22:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582445.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路线   位置   两个   如何在   iOS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!