如何在Google地图中将所有标记与路径相关联?

daG*_*vis 8 javascript directory google-maps path latitude-longitude

我是谷歌地图(API)的新手,我需要获得以下结果:

路径

目前,我知道如何渲染地图并在其上放置标记(基于经度和纬度).

var map;

map = new google.maps.Map(document.getElementById('map_canvas'), {
    zoom: 7,
    center: new google.maps.LatLng(response[0].latitude, response[0].longitude),
    mapTypeId: google.maps.MapTypeId.ROADMAP
});

for (var i = 0; i < response.length; ++i) {

    new google.maps.Marker({
        'map' : map,
        'position' : new google.maps.LatLng(response[i].latitude, response[i].longitude),
        'title' : response[i].address
    });

}
Run Code Online (Sandbox Code Playgroud)

变量response结构如下:

[
Object
address: "Krišj??a Barona iela 25, Riga, LV-1011, Latvia"
latitude: "24.1245290"
longitude: "56.9528510"
__proto__: Object
, 
Object
address: "R?gas iela 1, Tukums, Tukuma novads, LV-3101, Latvia"
latitude: "23.1630590"
longitude: "56.9663880"
__proto__: Object
]
Run Code Online (Sandbox Code Playgroud)

可能有很多标记.我正在寻找一种方法来连接标记与预览图像中的路径.

伙计们,我不知道应该搜索什么,我需要你的帮助.谢谢你的建议!

kat*_*ugh 12

Google教程的一个例子:

  var flightPlanCoordinates = [
    new google.maps.LatLng(37.772323, -122.214897),
    new google.maps.LatLng(21.291982, -157.821856),
    new google.maps.LatLng(-18.142599, 178.431),
    new google.maps.LatLng(-27.46758, 153.027892)
  ];
  var flightPath = new google.maps.Polyline({
    path: flightPlanCoordinates,
    strokeColor: "#FF0000",
    strokeOpacity: 1.0,
    strokeWeight: 2
  });
Run Code Online (Sandbox Code Playgroud)

另请参阅参考条目Polylines.

如果您不知道如何将响应对象映射到对象数组LatLng,这是一个示例:

var flightPath = responseArray.map(function (item) {
    return new google.maps.LatLng(item.latitude, item.longitude);
});
Run Code Online (Sandbox Code Playgroud)

  • 最后你应该添加这个,flightPath.setMap(map); (2认同)