如何向google方向api发送请求以获取swift中的路径

时间:2016-07-17 12:47:29

标签: ios swift google-maps google-maps-api-3 google-direction

我很快乐 我正在创建一个应用程序,它将显示用户的位置并将标记放置到该位置。用户移动后。将删除标记并创建新标记。现在。我想在点A和点B上制作标记到应用程序中并在地图上显示路线。它应使用地图上最近的道路。 我已经研究了谷歌地图文档但我需要帮助我无法理解如何在2点之间建立路线?

如果你帮助我,我会很高兴,非常感谢你。

1 个答案:

答案 0 :(得分:0)

根据此blog,您首先需要添加属性let locationManager = CLLocationManager(),该属性将添加并实例化名为CLLocationManager的{​​{1}}属性。

接下来,找到locationManager并将这两行添加到底部,这将使viewDidLoad()成为MapViewController的委托,并请求访问该用户的位置。

locationManager

从这个相关的thread,你必须在locationManager.delegate = self locationManager.requestWhenInUseAuthorization() 中实例化CLLocationManager类,如下所示:

viewDidLoad()

然后在// Ask for Authorisation from the User. self.locationManager.requestAlwaysAuthorization() // For use in foreground self.locationManager.requestWhenInUseAuthorization() if CLLocationManager.locationServicesEnabled() { locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters locationManager.startUpdatingLocation() } 方法中,您可以获得用户的当前位置坐标:

CLLocationManagerDelegate

然后到add a marker,创建一个包含func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { var locValue:CLLocationCoordinate2D = manager.location.coordinate print("locations = \(locValue.latitude) \(locValue.longitude)") } GMSMarker的{​​{1}}对象,并设置其position。以下示例演示如何将标记添加到现有title对象。标记在坐标map处创建,并在单击时在信息窗口中显示字符串“Hello world”。

GMSMapView

最后,在2点之间建立一条路线,你可以查看这些链接:

  

现在在10,10方法中提供该城市名称或您想要的内容   像这样的起源:

let  position = CLLocationCoordinate2DMake(10, 10)
let marker = GMSMarker(position: position)
marker.title = "Hello World"
marker.map = mapView
     

UIAlertControllerStyle.Alert)

createRoute
     

(状态,成功) - >无效                   如果成功{                       self.configureMapAndMarkersForRoute()                       self.drawRoute()                       self.displayRouteInfo()                   }                   其他{                       的println(状态)                   }               })           }

@IBAction func createRoute(sender: AnyObject) {

    let addressAlert = UIAlertController(title: "Create Route", message: "Connect locations with a route:", preferredStyle:
  

首先,获取路线中的所有点坐标然后添加   这些点在路径中的纬度和经度将绘制路径   根据那个

    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: {

希望这有帮助!