gpt4 book ai didi

javascript - 如何向 Google map 添加折线?

转载 作者:行者123 更新时间:2023-11-28 10:03:17 24 4
gpt4 key购买 nike

我正在使用 Google map API。我正在尝试添加折线和数据库中的坐标。谁能告诉我如何使用标记管理器添加折线?我想我的坐标太多了,而且变得困惑了。使用标记管理器可能会有所帮助。有什么建议吗?
我使用的代码是:

for ($i=0;$i<$truckCount;$i++)
{
$j=0;
$k=1;
do
{
$data = pg_fetch_row($result,$j);
$data1=pg_fetch_row($result,$k);
$j++;
$k++;
}while(condition)
echo"points[$i]=[new GLatLng($data[4], $data[5]),new GLatLng($data1[4], $data1[5])];";
echo"polyline= new GPolyline(points,'#0000FF', 6, 0.5);";
echo "setTimeout(function() {map.addOverlay(polyline);},2);";

}

我使用了 GLog.write(points),我发现只有前两个坐标被传递,因此没有绘制线条

最佳答案

您需要将新的 GPolyline 部分移到循环之外。因此,您将每个单独的坐标添加到数组中,然后最后使用整个数组来创建折线。

for(i=0;i<till data;i++)
{
$data=pg_fetch_row($result,$i);
points[$i]= new GLatLng($data[4],$data[5]); // Lat,Long coordinates are at 5th and 6th column
}

var polyline = new GPolyline(points,color,4,1);
map.addOverlay(polyline);

更新:或者,如果您想一次绘制一条直线的每一段,您可以尝试创建一个只有 2 个坐标的新数组:起点和单个段的终点。这次你需要循环直到 array.length-1 (不确定你需要如何修改你的 PHP 数组,所以我会用纯 Javascript 语法写一些东西 - 你需要修改回 PHP)

var coordinates = // your data array;
var points,polyline;

for(i=0;i < coordinates.length-1; i++) {
// recreate the array
points = [];

// add the first coordinate
points[0]= new GLatLng(coordinates[i].lat,coordinates[i].lon);

// add the 2nd coordinate
points[1]= new GLatLng(coordinates[i+1].lat,coordinates[i+1].lon);

polyline = new GPolyline(points,color,4,1);
map.addOverlay(polyline);

// at this point here you could add a setTimeout if still required
}

关于javascript - 如何向 Google map 添加折线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8754070/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com