Swift iOS Google Map,协调路径 [英] Swift iOS google Map, path to coordinate

查看:136
本文介绍了Swift iOS Google Map,协调路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用中创建一个函数,该函数将引导用户使用我创建的标记. 这是我正在使用的代码,效果很好,它可以为用户提供当前位置并将其显示在地图上.但是,如何获得前往标记的路线呢?

I am trying to create a function in my app that will guide the user to a marker I have created. This is the code I am using, it works great, It gets the users current location and show it on the map. But how can I get a directions to a marker?

任何遮阳篷都会有帮助

class Karta: UIViewController, CLLocationManagerDelegate {
    @IBOutlet var mapView: GMSMapView!

    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        //allow app to track user
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()

        //set out a marker on the map
        var marker = GMSMarker()
        marker.position = CLLocationCoordinate2DMake(56.675907, 12.858798)
        marker.appearAnimation = kGMSMarkerAnimationPop
        marker.icon = UIImage(named: "flag_icon")
        marker.map = mapView
     }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "Types Segue" {
            let navigationController = segue.destinationViewController as UINavigationController
        }
    }

    func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {

        //If map is being used
        if status == .AuthorizedWhenInUse {
            var myLocation = mapView
            locationManager.startUpdatingLocation()
            mapView.myLocationEnabled = true
            mapView.settings.myLocationButton = true
        }
    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        if let location = locations.first as? CLLocation {
            mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
          locationManager.stopUpdatingLocation()
        }
    }
}

推荐答案

与Apple的MapKit不同,iOS版Google Maps SDK本身并不包含执行路线计算的方法.

Unlike Apple's MapKit, the Google Maps SDK for iOS does not natively include a way to perform route calculations.

相反,您需要使用Google Directions API: https://developers.google. com/maps/documentation/directions/.它是仅HTTP的API,到目前为止Google尚未提供任何SDK,但是您可以轻松地自己编写包装器,或者选择Github上可用的众多选项之一:

Instead, you need to use the Google Directions API: https://developers.google.com/maps/documentation/directions/. It is an HTTP-only API, and Google does not provide any SDK as of today, but you can easily write your own wrapper yourself, or choose one of the many options available on Github:

  • https://github.com/sudeepjaiswal/GoogleDirections
  • https://github.com/sebk/GoogleDirections
  • https://github.com/iamamused/iOS-DirectionKit
  • https://github.com/marciniwanicki/OCGoogleDirectionsAPI
  • and many others...

这篇关于Swift iOS Google Map,协调路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆