如何使用 GoogleMaps sdk 获取路线图的中心

Posted

技术标签:

【中文标题】如何使用 GoogleMaps sdk 获取路线图的中心【英文标题】:How to get centre of the route map using GoogleMaps sdk 【发布时间】:2017-05-01 05:46:27 【问题描述】:

我想在路线图的中心显示图像。 以下是用于起点和终点之间路线的代码:

        let camera1 = GMSCameraPosition.camera(withLatitude: 45.4654, longitude:9.1859, zoom: 0.0)
        self.mapView = GMSMapView.map(withFrame: cell.mapView.bounds, camera: camera1)

 Alamofire.request(url, method: .post, parameters: nil, encoding: JSONEncoding.default, headers: nil)
        .validate()
        .responseJSON  response in
            switch response.result 
            case .success:
                print(response.result.value!)
                print("Validation Successful")
                let dictResponse = response.result.value as! NSDictionary
                print(dictResponse)
                 let aryRoutes = dictResponse .value(forKey:"routes" ) as! NSArray
                print(aryRoutes)

                var aryOverViewPolyLines :NSArray = []
                aryOverViewPolyLines = aryRoutes .value(forKey: "overview_polyline") as! NSArray
                print(aryOverViewPolyLines)
                let strPoints = (aryOverViewPolyLines.value(forKey: "points") as! NSArray).object(at: 0)
                let polygon = GMSPolygon()
                polygon.path = GMSPath(fromEncodedPath: strPoints as! String)
                print(strPoints)
                let rectangle = GMSPolyline.init(path: polygon.path)
                rectangle.strokeWidth = 2.0
                rectangle.strokeColor = .white
                rectangle.map = self.mapView

                let mapBounds = GMSCoordinateBounds(path: polygon.path!)
                self.mapView.animate(with: GMSCameraUpdate.fit(mapBounds, withPadding: 150.0))



                case .failure(let error):
                print(response.result.value!)
                print(error)
            
    

关于如何解决这个问题的任何建议。

【问题讨论】:

【参考方案1】:

@mnabaa 的解决方案通过在折线上找到中间路径来工作。但是,有些路径的距离可能比其他路径长得多。因此,根据路线的不同,Mnabaa 的解决方案返回的点最终可能会比点 B 更接近点 A。下面的扩展考虑了这个问题:

extension GMSPolyline 
    var middlePoint: CLLocationCoordinate2D? 
        guard let path = path else 
            return nil
        
        var intermediatePoints: [CLLocationCoordinate2D] = []
        for coordinateIndex in 0 ..< path.count() - 1 
            let startCoordinate = path.coordinate(at: coordinateIndex)
            let endCoordinate = path.coordinate(at: coordinateIndex + 1)
            let startLocation = CLLocation(latitude: startCoordinate.latitude, longitude: startCoordinate.longitude)
            let endLocation = CLLocation(latitude: endCoordinate.latitude, longitude: endCoordinate.longitude)
            let pathDistance = endLocation.distance(from: startLocation)
            let intervalLatIncrement = (endLocation.coordinate.latitude - startLocation.coordinate.latitude) / pathDistance
            let intervalLngIncrement = (endLocation.coordinate.longitude - startLocation.coordinate.longitude) / pathDistance
            for intervalDistance in 0 ..< Int(pathDistance) 
                let intervalLat = startLocation.coordinate.latitude + (intervalLatIncrement * Double(intervalDistance))
                let intervalLng = startLocation.coordinate.longitude + (intervalLngIncrement * Double(intervalDistance))
                let circleCoordinate = CLLocationCoordinate2D(latitude: intervalLat, longitude: intervalLng)
                intermediatePoints.append(circleCoordinate)
            
        
        return intermediatePoints[intermediatePoints.count / 2]
    

它基于article by Dylan Maryk。

【讨论】:

【参考方案2】:
func mapView(_ mapView: GMSMapView, didTap overlay: GMSOverlay) 
    if let route = overlay as? GMSPolyline

        let coordinate = route.path!.coordinate(at: (route.path!.count()-1) / 2)
        route.customMapInfoMarker!.position = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)
        route.customMapInfoMarker.groundAnchor = CGPoint(x: 0.5, y: 0.5)
        route.customMapInfoMarker.map = mapView
        route.customMapInfoMarker.iconView = // YOUR CUSTOM IMAGEVIEW HERE
    

扩展 GMSPolyline

extension GMSPolyline

    fileprivate struct AssociatedKey 
        static var infoMarker = GMSMarker()
    
    var customMapInfoMarker: GMSMarker! 
        get 
            if let marker = objc_getAssociatedObject(self, &AssociatedKey.infoMarker) as? GMSMarker 
                return marker
            
            return GMSMarker()
        
        set (newValue)
            if let newValue = newValue 
                objc_setAssociatedObject(
                    self,
                    &AssociatedKey.infoMarker,
                    newValue,
                    objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            
        
    

【讨论】:

以上是关于如何使用 GoogleMaps sdk 获取路线图的中心的主要内容,如果未能解决你的问题,请参考以下文章

更改 Google 水印 GoogleMaps SDK

如何使用 GoogleMaps v2 获取当前坐标?

Swift - GoogleMaps SDK 获得单指或双指点击手势

加载优化模型失败 - GoogleMaps SDK IOS

让 GoogleMaps For Mobile 遵循预定义的路线

我可以将适用于 iOS 的 GoogleMaps SDK 与 -ObjC 以外的链接器标志链接吗