1

我是一个 javascript 菜鸟,所以请帮帮我!

我做了一个包含几个城市的数组:

var cities = [
    ['City 1', 59.326294118560725, 17.98754555000005],
    ['City 2', 57.70231523439743, 11.893682500000068],
    ['City 3', 62.39591678065696, 17.292328450000014],
    ['City 4', 59.89396160257896, 10.785116549999998],
    ['City 5', 57.10938174202619, 12.25817589999997]
    ];

我希望能够按名称选择两个或更多城市并在它们之间画一条线。

我找到了这段代码:

var route = new google.maps.Polyline({
  path: something
  strokeColor: "#FF0000",
  strokeOpacity: 0.8,
  strokeWeight: 2,
  map: map
});

如何修改它以便我可以从数组中选择一两个城市,并使用城市名称作为参考?

4

1 回答 1

2
var cities = [
    ['City 1', 59.326294118560725, 17.98754555000005],
    ['City 2', 57.70231523439743, 11.893682500000068],
    ['City 3', 62.39591678065696, 17.292328450000014],
    ['City 4', 59.89396160257896, 10.785116549999998],
    ['City 5', 57.10938174202619, 12.25817589999997]
    ];

var something = [];
for (var i=0; i<cities.length; i++) {
  if ((firstCity == cities[i][0]) || (secondCity == cities[i][0])) {
    // assume coordinates are Latitude, Longitude
    something.push(new google.maps.LatLng(cities[i][1],cities[i][2]));
  }
}
var route = new google.maps.Polyline({
  path: something,
  strokeColor: "#FF0000",
  strokeOpacity: 0.8,
  strokeWeight: 2,
  map: map
});

所有要点的工作示例

数组中 2 个城市的工作示例

于 2013-10-14T21:07:30.900 回答