How to put a infowindow on polyline in Google Maps v3?

前端 未结 3 1214
情歌与酒
情歌与酒 2021-01-12 05:51

I want to know how to show infowindow on polyline in using Google Maps Api V3? and to appear in the middle of the polyline ?!

3条回答
  •  情书的邮戳
    2021-01-12 06:30

    First you should got center/middle of polyline and this what works for me

      private fun centerPos(points: MutableList): LatLng {
        val middleDistance = SphericalUtil.computeLength(points).div(2)
        return extrapolate(points, points.first(), middleDistance.toFloat()) ?: points[0]
    }
    private fun extrapolate(path: List, 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 = 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
    }
    

    then You can add infoWindow with normal way with your platform at it is differ from each platform

提交回复
热议问题