加载多个谷歌地图实例

时间:2011-03-22 21:28:59

标签: javascript google-maps google-maps-api-3

这是我将Google地图实例加载到map div。

的方式

我想添加另一个ID为map2的div,它将保存另一个Google地图实例,我该怎么做?

<script type="text/javascript"> 
  var map;
  function initialize() {
    var myLatlng = new google.maps.LatLng(33.33333,44.44444);
    var myOptions = {
      zoom: 14,
      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:"If this is your exact location, press \"Add this location\""
    });
    google.maps.event.addListener(marker, 'click', function() {
      map.setZoom(8);
    });
  }


</script> 
<div id="map"></div>

1 个答案:

答案 0 :(得分:3)

如果你想通过javascript

这样做
var map2 = document.createElement('div');
map2.id = "map2";

document.body.innerHTML += map2;

然后使用不同的div id调用初始化函数;即map2;

编辑:您确定没有错误地调用该功能吗?

<script type="text/javascript"> 

  function initialize(mapNum) {
    var map;
    var myLatlng = new google.maps.LatLng(33.33333,44.44444);
    var myOptions = {
      zoom: 14,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map"+mapNum), myOptions);


    var marker = new google.maps.Marker({
        position: myLatlng, 
        map: map, 
        title:"If this is your exact location, press \"Add this location\""
    });
    google.maps.event.addListener(marker, 'click', function() {
      map.setZoom(8);
    });
  }


initialize(1);
initialize(2);

</script> 
<div id="map1"></div>
<div id="map2"></div>