Javascript 加载多个google地图实例
这是当前我将google地图实例加载到Javascript 加载多个google地图实例,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,这是当前我将google地图实例加载到mapdiv的方式 我想添加另一个id为map2的div,该div将保存另一个googlemaps实例,我该怎么做 <script type="text/javascript"> var map; function initialize() { var myLatlng = new google.maps.LatLng(33.33333,44.44444); var myOptions = { zoom: 1
map
div的方式
我想添加另一个id为map2
的div,该div将保存另一个googlemaps实例,我该怎么做
<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>
var映射;
函数初始化(){
var mylatng=new google.maps.LatLng(33.33333,44.44444);
变量myOptions={
缩放:14,
中心:myLatlng,
mapTypeId:google.maps.mapTypeId.ROADMAP
}
map=新的google.maps.map(document.getElementById(“map”),myOptions);
var marker=new google.maps.marker({
职位:myLatlng,
地图:地图,
标题:“如果这是您的确切位置,请按\“添加此位置\”
});
google.maps.event.addListener(标记'click',函数(){
map.setZoom(8);
});
}
如果您想通过javascript执行此操作
var map2 = document.createElement('div');
map2.id = "map2";
document.body.innerHTML += map2;
然后使用不同的div id调用initialize函数;,ie 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>
函数初始化(mapNum){
var映射;
var mylatng=new google.maps.LatLng(33.33333,44.44444);
变量myOptions={
缩放:14,
中心:myLatlng,
mapTypeId:google.maps.mapTypeId.ROADMAP
}
map=new google.maps.map(document.getElementById(“map”+mapNum),myOptions);
var marker=new google.maps.marker({
职位:myLatlng,
地图:地图,
标题:“如果这是您的确切位置,请按\“添加此位置\”
});
google.maps.event.addListener(标记'click',函数(){
map.setZoom(8);
});
}
初始化(1);
初始化(2);
如果您想通过javascript执行此操作
var map2 = document.createElement('div');
map2.id = "map2";
document.body.innerHTML += map2;
然后使用不同的div id调用initialize函数;,ie 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>
函数初始化(mapNum){
var映射;
var mylatng=new google.maps.LatLng(33.33333,44.44444);
变量myOptions={
缩放:14,
中心:myLatlng,
mapTypeId:google.maps.mapTypeId.ROADMAP
}
map=new google.maps.map(document.getElementById(“map”+mapNum),myOptions);
var marker=new google.maps.marker({
职位:myLatlng,
地图:地图,
标题:“如果这是您的确切位置,请按\“添加此位置\”
});
google.maps.event.addListener(标记'click',函数(){
map.setZoom(8);
});
}
初始化(1);
初始化(2);
@noiv11刚试过,第一个映射成功,第二个失败。@noiv11刚试过,第一个映射成功,第二个失败。