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

时间:2017-05-01 05:46:28

标签: ios iphone google-maps swift3

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

{{1}}

如何解决此问题的任何建议。

2 个答案:

答案 0 :(得分:0)

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)
            }
        }
    }
}

答案 1 :(得分:0)

@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