在自定义视图中使用

在自定义视图中使用

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

问题描述

我试图在自定义 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

}

请帮忙!

推荐答案

在这里工作正常.确保您设置了 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

类似:

    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

        }
    }

有关详细信息 - Directions API 开发人员指南

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

07-31 15:44