【问题标题】:Create non-looping Polylines in Google Maps在 Google 地图中创建非循环折线
【发布时间】:2016-11-15 09:19:00
【问题描述】:

我有一个坐标数组,我想在 Maps API v3 中为其创建折线。到目前为止,我已经完成了以下操作。

    for (i = 0; i < locations.length; i++) {  //locations is an array of Lat, Long
        var a = new google.maps.LatLng(locations[i][0], locations[i][1]);
        plans.push(a)
        bounds.extend(a);
    }

    var roadPath = new google.maps.Polyline({
        path: plans,
        geodesic: true,
        strokeColor: '#FF0000',
        strokeOpacity: 1.0,
        strokeWeight: 2
    });
    roadPath.setMap(map);

现在的问题是,在创建折线时,最后一个点和第一个点也如图所示连接起来

其中最左边的点是数组locations 中的第一个点,最右边的是最后一个坐标。如何让红线只贴在路上,而不是像乌鸦飞一样?

【问题讨论】:

标签: javascript google-maps


【解决方案1】:

而不是pushing all 点同时pass pair 点那么您的问题将得到解决。即尝试使用pair of points.like 绘制路径:第一次send origin and first 点,然后second pair is first point and second point 等等。

例如:假设您的点数组为:origin,first,seond,third,......,last,destination。然后将origin,first 发送为first pair,然后将first,second 发送为second pairsecond and third 发送为third pair, .....,last,destination as last pair 到谷歌方向服务或折线。

这里确保起点和终点不相同,如果它们相同则不要发送最后一对。

【讨论】:

  • 我已经在例子中解释过了。
  • 这篇文章在低质量队列中结束。我投了反对票,但很容易理解为什么。 @SuchitKumar,你 need 停止 misusing inline 格式化以突出 parts of your 帖子 - 我检查了你的其他答案,你经常这样做。请使用其他格式选项,例如**粗体**或_斜体_。这会激怒人们,并使您的帖子更难阅读,而不是更容易阅读。
【解决方案2】:

这可能是由于:
1. 重复的数据集。您可能已经将两组或更多组数据推送到折线路径中,因此当一个结束时,它会连接到原始坐标并形成循环。 2. 您将原始坐标添加到缓冲区的末尾。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多