如何在Google Maps v3中的折线上设置信息窗口?

时间:2011-11-23 18:17:31

标签: google-maps-api-3 google-polyline

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

3 个答案:

答案 0 :(得分:10)

首先,您需要计算折线的中间/中心。这已经在这里讨论和回答; https://stackoverflow.com/a/9090409/787921

然后你必须打开infowindow;

var infowindow = new google.maps.InfoWindow({
             content: "infowindow text content"});
infowindow.setPosition(midLatLng);
infowindow.open(map);

答案 1 :(得分: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)
}

答案 2 :(得分: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 ,因为您的平台不同于每个平台