在Swift4中的GoogleMaps上显示路径 [英] Show Path on GoogleMaps in Swift4

查看:68
本文介绍了在Swift4中的GoogleMaps上显示路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我想绘制两个点之间的时间,并且我的两个位置都来自不同的控制器

My question is I want to draw the time between two points and my both location are coming from different controllers

for my first Location :- 
  extension HomeViewController: PickupLocationDelegate {
func didSelectLocation(place: GooglePlaceModel?) {
    guard let place = place else {return}
    enter_Location_TF.text = place.name
    customDirectionViewTwo.isHidden = false
    direction_Button.isHidden = true
    location_Button.setImage(UIImage(named: "directionPoint")?.withRenderingMode(.alwaysOriginal), for: .normal)

    pickupMarkers.forEach { (marker) in
        marker.map = nil
    }
    let position = CLLocationCoordinate2D(latitude: place.latitude , longitude: place.longitude)
    print(position)
    let path = GMSMutablePath()
    path.add(CLLocationCoordinate2D(latitude: place.latitude, longitude: place.longitude))
    let marker = GMSMarker()
    marker.position = position
    marker.map = mapView
    mapView.camera = GMSCameraPosition.camera(withTarget: position, zoom: 14)
    pickupMarkers.append(marker)

}
}// My second location:-
 extension HomeViewController: DropLocationDelegate {
func didSelectDrop(location: GooglePlaceModel?) {
    guard let location = location else {return}
    dropLocationLbl?.text = location.name


    let position = CLLocationCoordinate2D(latitude: location.latitude , longitude: location.longitude)
    print(position)
    let marker = GMSMarker(position: position)
    marker.icon = #imageLiteral(resourceName: "icon-drop-location")
    marker.map = mapView

    mapView.camera = GMSCameraPosition.camera(withTarget: position, zoom: 14)
    pickupMarkers.append(marker)
    let path = GMSMutablePath()
    path.add(CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude))
    pickupMarkers.forEach { (marker) in
        path.add(marker.position)
    }
    let bounds = GMSCoordinateBounds(path: path)
    mapView?.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 50.0))
}
}

标记显示正确,但路径未显示.我用了 GMSMutable路径以绘制位置之间的界线,但这不能正常工作.有帮助吗?

marker are showing correctly but the path is not showing. I used the GMSMutable path to draw the line between location but this is not working properly. Any help?

推荐答案

前提条件

您需要在此链接后获取Google Directions api密钥 如何获取Google Directions API密钥,并且您还需要添加此行

You need to get a Google directions api Key following this link How to get a Google Directions API key and you also need to add this line

GMSPlacesClient.provideAPIKey("Your API KEY")

在您的AppDelegate didFinishLaunchingWithOptions方法中

现在是我们的问题

要找到路径,您需要使用类似这样的方法,通过googleapis.directions请求,传递两个CLLocationCoordinate2D,然后在结束时,您将获得一个CLLocationCoordinate2D数组,它们是路径的航路点

To find a path you need to use a method like this one, using googleapis.directions request, passing two CLLocationCoordinate2D then in the closure you will get an array of CLLocationCoordinate2D which are the waypoints of your path

public func getWaypointsAsArrayOfCoordinates(startLocation: CLLocationCoordinate2D, endLocation: CLLocationCoordinate2D, mode:String? = "walking", lang:String? = "en", finishedClosure:@escaping (([CLLocationCoordinate2D])->Void)){

        var resultedArray:[CLLocationCoordinate2D] = []

        let urlWithParams = "https://maps.googleapis.com/maps/api/directions/json" + self.customEncodedParameters(parametersDict: ["origin":"\(startLocation.latitude),\(startLocation.longitude)", "destination": "\(endLocation.latitude),\(endLocation.longitude)", "mode": mode!, "key":googleDirectionsApiKey, "language" : lang!])

        var urlRequest = URLRequest(url: URL(string: urlWithParams)!)
        urlRequest.httpMethod = "GET"

        URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in

            if let _ = error {
            } else {
                do {
                    if  let jsonData = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary {
                        let status = jsonData["status"] as! String
                        if(status == "OK") {

                            for routeDict in jsonData["routes"] as! Array<Dictionary<String, AnyObject>>
                            {
                                let legs = routeDict["legs"] as! Array<Dictionary<String, AnyObject>>
                                for leg in legs
                                {
                                    let steps = leg["steps"] as! Array<Dictionary<String, AnyObject>>
                                    for (index,step) in steps.enumerated(){
                                        let start = step["start_location"] as! Dictionary<String,Any>
                                        let end = step["end_location"] as! Dictionary<String,Any>
                                        resultedArray.append(CLLocationCoordinate2D(latitude: start["lat"] as! CLLocationDegrees, longitude: start["lng"] as! CLLocationDegrees))
                                        if(index == steps.count - 1) {
                                            resultedArray.append(CLLocationCoordinate2D(latitude: end["lat"] as! CLLocationDegrees, longitude: end["lng"] as! CLLocationDegrees))
                                        }
                                    }
                                }
                            }
                            finishedClosure(resultedArray)
                        }
                        else {
                            print("not found")
                            finishedClosure([])
                        }
                    }
                } catch {
                    print(error)
                    finishedClosure([])
                }
            }

            }.resume()

    }

编辑(添加了缺少的功能)

private func customEncodedParameters(parametersDict:[String:String]) ->String
    {
        let charactersAllowed = CharacterSet.urlQueryAllowed
        var returnStr = ""
        for key in parametersDict.keys {
            if(returnStr.count == 0)
            {
                returnStr += "?"
                returnStr += key.addingPercentEncoding(withAllowedCharacters: charactersAllowed)!
                returnStr += "="
                returnStr += parametersDict[key]!.addingPercentEncoding(withAllowedCharacters: charactersAllowed)!
            }else{
                returnStr += "&"
                returnStr += key.addingPercentEncoding(withAllowedCharacters: charactersAllowed)!
                returnStr += "="
                returnStr += parametersDict[key]!.addingPercentEncoding(withAllowedCharacters: charactersAllowed)!
            }
        }
        return returnStr
    }

然后您可以像这样在代码中使用它

extension HomeViewController: DropLocationDelegate {
func didSelectDrop(location: GooglePlaceModel?) {
    guard let location = location else {return}
    dropLocationLbl?.text = location.name


    let position = CLLocationCoordinate2D(latitude: location.latitude , longitude: location.longitude)
    print(position)
    let marker = GMSMarker(position: position)
    marker.icon = #imageLiteral(resourceName: "icon-drop-location")
    marker.map = mapView

    mapView.camera = GMSCameraPosition.camera(withTarget: position, zoom: 14)
    pickupMarkers.append(marker)
    self.getWaypointsAsArrayOfCoordinates(startLocation: pickupMarkers.first.position , endLocation: pickupMarkers.last.position) { [weak self] (arrayOfCoordinates) in

        DispatchQueue.main.async {
            let path = GMSMutablePath()

            for coordinate in arrayOfCoordinates {
                path.add(coordinate)
            }

            let polyline = GMSPolyline(path: path)
            polyline.strokeWidth = 2
            polyline.strokeColor = UIColor.red
            polyline.map = self?.mapView

            let bounds = GMSCoordinateBounds(path: path)
            self?.mapView?.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 50.0))
        }
    }
}
}

这篇关于在Swift4中的GoogleMaps上显示路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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