javascript - 将值传递给 google.maps.LatLng

标签 javascript google-maps-api-3

我有一个数组,我想将其值传递给路径以绘制多边形

var coords = new Array("25.1, 66", "25.5, 65", "21.3, 67");

无论坐标数字如何,我如何将上面数组中的模式“x, y”传递给 google.maps.LatLng

  var triangleCoords = [ 
            new google.maps.LatLng(coords[0]),
             new google.maps.LatLng(coords[1]),
              new google.maps.LatLng(coords[2]),

  ];

  // Construct the polygon
  // Note that we don't specify an array or arrays, but instead just
  // a simple array of LatLngs in the paths property
  draw = new google.maps.Polygon({
    paths: triangleCoords,
    strokeColor: "#FF0000",
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: "#FF0000",
    fillOpacity: 0.35
  });

最佳答案

您可以使用 new google.maps.LatLng(coords[0].split(',')[0 而不是 new google.maps.LatLng(coords[0]) ],coords[0].split(',')[1]).

但是,由于这会进行两次分割,我可能会将其提取到一个函数中 - 像这样:

function createLatLng(coordString) {
     var a = coordString.split(',');
     return new google.maps.LatLng(a[0],a[1]);
}

var triangleCoords = [ 
       createLatLng(coords[0]),
      ....
];

为了获得额外的积分,您可以使用 for 循环:

   var triangleCoords = [];
   for (var i = 0; i < coords.length; i++) {
      triangleCoords[i] = createLatLng(coords[i]);
   }

关于javascript - 将值传递给 google.maps.LatLng,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8468037/

相关文章:

javascript - 从谷歌地图中获取第一个地址段

jquery - 使用markerclusterer v3获取谷歌地图范围内的标记列表

javascript - 谷歌地图不显示

javascript - 如何在 Javascript 中将中介器模式与多个中介器实例一起使用?

javascript - 如何通过单击一次从两个不同的 div(其中一个隐藏)复制内容,但每个 div 都在单独的行上?

javascript - 在javascript中将列表中的单词与句子中的单词相匹配的最佳方法是什么?

javascript - 如何对 2d for 循环进行排序并将结果输入斜边公式

javascript - 如何在每次页面加载后运行特定的 jquery 脚本?

javascript - 来自地址的标记人口

javascript - Google map V3 InvalidValueError Rails 5