8

我想知道如何infowindowpolyline使用中显示Google Maps Api V3?并出现在折线的中间?!

4

3 回答 3

10

首先,您需要计算折线的中间/中心。此处已对此进行了讨论和回答; https://stackoverflow.com/a/9090409/787921

然后你必须打开信息窗口;

var infowindow = new google.maps.InfoWindow({
             content: "infowindow text content"});
infowindow.setPosition(midLatLng);
infowindow.open(map);
于 2012-02-02T00:19:17.953 回答
0

首先你应该得到折线的中心/中间,这对我有用

  private fun centerPos(points: MutableList<LatLng>): LatLng {
    val middleDistance = SphericalUtil.computeLength(points).div(2)
    return extrapolate(points, points.first(), middleDistance.toFloat()) ?: points[0]
}
private fun extrapolate(path: List<LatLng>, origin: LatLng, distance: Float): LatLng? {
    var extrapolated: LatLng? = null
    if (!PolyUtil.isLocationOnPath(
            origin,
            path,
            false,
            1.0
        )
    ) { // If the location is not on path non geodesic, 1 meter tolerance
        return null
    }
    var accDistance = 0f
    var foundStart = false
    val segment: MutableList<LatLng> = ArrayList()
    for (i in 0 until path.size - 1) {
        val segmentStart = path[i]
        val segmentEnd = path[i + 1]
        segment.clear()
        segment.add(segmentStart)
        segment.add(segmentEnd)
        var currentDistance = 0.0
        if (!foundStart) {
            if (PolyUtil.isLocationOnPath(origin, segment, false, 1.0)) {
                foundStart = true
                currentDistance = SphericalUtil.computeDistanceBetween(origin, segmentEnd)
                if (currentDistance > distance) {
                    val heading = SphericalUtil.computeHeading(origin, segmentEnd)
                    extrapolated = SphericalUtil.computeOffset(
                        origin,
                        (distance - accDistance).toDouble(),
                        heading
                    )
                    break
                }
            }
        } else {
            currentDistance = SphericalUtil.computeDistanceBetween(segmentStart, segmentEnd)
            if (currentDistance + accDistance > distance) {
                val heading = SphericalUtil.computeHeading(segmentStart, segmentEnd)
                extrapolated = SphericalUtil.computeOffset(
                    segmentStart,
                    (distance - accDistance).toDouble(),
                    heading
                )
                break
            }
        }
        accDistance += currentDistance.toFloat()
    }
    return extrapolated
}

然后您可以使用您的平台以正常方式添加infoWindow ,因为它与每个平台不同

于 2020-12-13T11:38:18.543 回答
0

找到中间点并设置您的自定义视图。

func showPath(polyStr :String){

    polyline?.map = nil
    mapView1.reloadInputViews()
    pathDraw = GMSPath(fromEncodedPath: polyStr)!
    polyline = GMSPolyline(path: pathDraw)

    polyline?.strokeWidth = 4.0
    polyline?.strokeColor = UIColor.init(red: 247/255.0, green: 55/255.0, blue: 76/255.0, alpha: 1.0)
    polyline?.map = mapView1


    let poinsCount = pathDraw.count()
    let midpoint = pathDraw.coordinate(at: poinsCount)

    DispatchQueue.main.async
    {
        self.addMarkerPin(corrdinate: midCordinate, distance: "10 min")
    }

}
func addMarkerPin(corrdinate:CLLocationCoordinate2D, distance: String)
{
    let marker = GMSMarker()
    marker.position = corrdinate

    PathTimeView = PathInfoView.loadFromNib() //here i am load Xib file, you can use your custom view 
    let DynamicView=PathTimeView
    DynamicView.timelbl.text = distance

    UIGraphicsBeginImageContextWithOptions(DynamicView.frame.size, false, UIScreen.main.scale)
    DynamicView.layer.render(in: UIGraphicsGetCurrentContext()!)
    let imageConverted: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()

    marker.icon = imageConverted
    marker.map = self.mapView1
    marker.infoWindowAnchor = CGPoint(x: -1900 , y: -2000)
}
于 2018-07-17T09:33:29.930 回答