提供我的谷歌地图的纬度和经度列表,用标记可视化它们

zah*_*oui 5 javascript google-maps-api-3

在我的项目中,我有一个包含纬度和经度列表的数据库,对应于几个地址,我想从这个数据库中取出它们并将它们传递给一个javascript代码,该代码将使用标记在地图中显示所有这些地址.到目前为止,我从数据库中获得了所有列表.通过使用它的纬度和经度,我能够在地图中可视化一个且只有一个地址.所以我现在正在努力解决多个地址问题.这是我到目前为止提出的代码:

      function initialize() {
          var myLatlng = new google.maps.LatLng(locations[0], locations[1]);
          var myOptions = {
              zoom: 4,
              center: myLatlng,
              mapTypeId: google.maps.MapTypeId.ROADMAP
          }
          map = new google.maps.Map(document.getElementById("map"), myOptions);
          var marker = new google.maps.Marker({
              position: myLatlng,
              map: map,
              title: "Fast marker"
          });
      }

      google.maps.event.addDomListener(window, 'load', initialize);
Run Code Online (Sandbox Code Playgroud)

location是我的纬度和经度列表,它是这样的:

locations[0]: latitude 1  
locations[1]: longitude 1  
locations[2]: latitude 2  
locations[3]: longitude 2    etc..
Run Code Online (Sandbox Code Playgroud)

现在我知道应该有某种循环,但我无法做到.

提前致谢!

PS:这是我的第一个问题,所以不要判断我的接受率!:)

Ste*_*ung 2

您只需要编写一个for循环,如下所示:

\n\n
function initialize() {\n  var myOptions = {\n    zoom: 4,\n    center: new google.maps.LatLng(locations[0], locations[1]),\n    mapTypeId: google.maps.MapTypeId.ROADMAP\n  }\n  map = new google.maps.Map(document.getElementById("map"), myOptions);\n  for (var i = 0, ln = locations.length; i < ln; i += 2) {\n    var marker = new google.maps.Marker({\n      position: new google.maps.LatLng(locations[i], locations[i + 1]);\n      map: map,\n      title: "Fast marker"\n    });\n  }\n}\n\ngoogle.maps.event.addDomListener(window, \'load\', initialize);\xe2\x80\x8b\n
Run Code Online (Sandbox Code Playgroud)\n