【问题标题】:Draw line between named entries in array on Google maps在谷歌地图上的数组中的命名条目之间画线
【发布时间】:2013-10-22 12:28:05
【问题描述】:

我是 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
});

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

【问题讨论】:

    标签: arrays google-maps google-maps-api-3 polyline google-polyline


    【解决方案1】:
    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
    });
    

    working example with all the points

    working example with 2 cities from the array

    【讨论】:

      猜你喜欢
      • 2012-11-06
      • 1970-01-01
      • 2012-06-10
      • 2021-11-12
      • 2015-03-29
      • 1970-01-01
      • 2011-01-11
      • 1970-01-01
      相关资源
      最近更新 更多