使用Swift 3自定义视图中的Google Maps绘制折线 [英] Draw polyline using Google Maps in custom view with Swift 3

查看:151
本文介绍了使用Swift 3自定义视图中的Google Maps绘制折线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在使用自定义UIView上的Google地图的两个地点之间绘制路线,但无法正确实施。我的自定义视图是mapViewX。我已经使用pod安装了Google sdk,其中包括pod'GoogleMaps'和pod'GooglePlaces'。我将自定义视图类设置为'GMSMapView'。我的代码是:

I am trying to draw route between two places using Google Maps on a custom UIView but not able to get it correctly implemented. My custom view is mapViewX. I've installed google sdk using pods which includes pod 'GoogleMaps' and pod 'GooglePlaces'. I made custom-view Class as 'GMSMapView'. my code is :

    @IBOutlet weak var mapViewX: GMSMapView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let path = GMSMutablePath()
    path.add(CLLocationCoordinate2D(latitude: 37.778483, longitude: -122.513960))
    path.add(CLLocationCoordinate2D(latitude: 37.706753, longitude: -122.418677))
    let polyline = GMSPolyline(path: path)
    polyline.strokeColor = .black
    polyline.strokeWidth = 10.0
    polyline.map = mapViewX

}

请帮忙!

Please help!

推荐答案

在这里可以正常工作。确保你设置的坐标正确 GMSCameraPosition

It works fine here. Make sure you're setting correct coordinates of GMSCameraPosition.

编辑

要在两个坐标之间绘制路线,请使用 Google Maps Direction API

To draw the route between two coordinate, use Google Maps Direction API

类似于:

Something like :

    let origin = "\(37.778483),\(-122.513960)"
    let destination = "\(37.706753),\(-122.418677)"
    let url = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&mode=driving&key=[YOUR-API-KEY]"

    Alamofire.request(url).responseJSON { response in
        let json = JSON(data: response.data!)
        let routes = json["routes"].arrayValue

        for route in routes
        {
            let routeOverviewPolyline = route["overview_polyline"].dictionary
            let points = routeOverviewPolyline?["points"]?.stringValue
            let path = GMSPath.init(fromEncodedPath: points!)

            let polyline = GMSPolyline(path: path)
            polyline.strokeColor = .black
            polyline.strokeWidth = 10.0
            polyline.map = mapViewX

        }
    }

更多信息 - < a href =https://developers.google.com/maps/documentation/directions/intro =nofollow noreferrer> Directions API开发人员指南

这篇关于使用Swift 3自定义视图中的Google Maps绘制折线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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