简体   繁体   English

Google Maps JS API v2-简单多个标记示例

[英]Google Maps JS API v2 - Simple Multiple Marker Example

I'm fairly new to gmaps and Im using v2 because of the search function that I didnt find in v3. 我对使用v2的gmaps和Im相当陌生,因为我没有在v3中找到搜索功能。

I've got an array of data that I want to loop through and put the markers on the map =) 我有一个要遍历的数据数组,并将标记放在地图上=)

It seems really easy but I cant get it to work with v2 at all... 看起来确实很容易,但是我根本无法使其与v2一起使用...

Here is my what my array format and code looks like: 这是我的数组格式和代码的样子:

function createMarkers(myLatLng,html) {
  var marker = new GMarker(myLatLng, markerOptions);
  GEvent.addListener(marker, 'click', function() {
    marker.openInfoWindowHtml(html);
  });
  return marker;
}


var locations = [
  ["Bondi Beach",-33.890542,151.274856],
  ["Coogee Beach",-33.923036,151.259052],
  ["Cronulla Beach",-34.028249,151.157507],
  ["Manly Beach",-33.80010128657071,151.28747820854187],
  ["Maroubra Beach",-33.950198,151.259302]
];


for (var i = 0; i < location.length; i++) {
   var locations = locations[i];
   var myLatLng = new GLatLng(locations[1],locations[2]);
   var dynamicmarker = createMarkers(myLatLng);
   map.addOverlay(dynamicmarker);
}

The beachnames got position locations[0], the lat got position location[1] and lng got position location[2] and so on... 海滩名称获得位置位置[0],纬度获得位置位置[1],lng获得位置位置[2],依此类推...

I didnt use the names of the beaches as the "html"option, but I only get one marker on the screen. 我没有将海滩的名称用作“ html”选项,但我在屏幕上只得到一个标记。 Ive checked the for loop and it looks correct, v3 is so simple to get it to work. 我已经检查了for循环,它看起来正确,v3非常简单,可以正常工作。 But I need to have the search function that v2 has... 但是我需要具有v2具有的搜索功能...

Would be so grateful if someone could give me a tip or show me how to go thru the array and get those markers to show up! 如果有人可以给我小费或向我展示如何通过阵列并让那些标记显示出来,将非常感激!

You appear to have a few problems in the for loop. 您似乎在for循环中有一些问题。 First of all location.length should be locations.length . 首先location.length应该是locations.length Then you seem to be re-declaring a locations variable within the for loop. 然后,您似乎要在for循环中重新声明一个locations变量。 Remember that JavaScript does not have block scope. 请记住,JavaScript没有阻止范围。

You may want to try the following: 您可能需要尝试以下方法:

var i, myLatLng;

for (i = 0; i < locations.length; i++) {
   myLatLng = new GLatLng(locations[i][1], locations[i][2]);
   map.addOverlay(createMarkers(myLatLng));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM