如何在 Google 地图 v3 中的折线上放置信息窗口?

发布于 2024-12-17 23:11:56 字数 108 浏览 2 评论 0原文

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

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 ?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愛上了 2024-12-24 23:11:56

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

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

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

Firstly you will need to calculate the middle/center of the polyline. This has been discussed and answered here;
https://stackoverflow.com/a/9090409/787921

Then you will have to open the infowindow;

var infowindow = new google.maps.InfoWindow({
             content: "infowindow text content"});
infowindow.setPosition(midLatLng);
infowindow.open(map);
一个人的旅程 2024-12-24 23:11:56

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

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

find the middle point and set your custom view .

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)
}
清眉祭 2024-12-24 23:11:56

首先你应该得到polylinecenter/middle,这对我有用

  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每个平台都有所不同

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

  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
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文