我使用谷歌地图API v3,我有一个问题。
我需要使用多张地图显示它们,而且我不知道地图的数量。
这里是代码php代码:
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?key=AIzaSyA5DRxSgUpWIfkLYULGijHXFQdogRElRIg&sensor=false">
</script>
<?php
for ($i = 1; $i <= 4; $i++) {
echo "map n:" . $i . "<br/>";
?><script>
function initialize(ltd,lgd) {
var myOptions = {
center: new google.maps.LatLng(ltd, lgd ),
zoom: 15,
panControl: false,
zoomControl: false,
mapTypeControl: false,
scaleControl: false,
streetViewControl: false,
overviewMapControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas<?php echo $i ?>"),
myOptions);
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
});
var marker;
marker = new google.maps.Marker({
position: new google.maps.LatLng(ltd, lgd),
map: map
});
}
</script>
<?php echo "<body onload='initialize(36.835769,10.247693)'>"; ?>
<div id="map_canvas<?php echo $i ?>" style="margin-top:18px; width: 136px; height: 136px;"></div>
<?php
}
?>
感谢您的帮助:)
答案 0 :(得分:1)
你正在多次创建函数初始化,什么是无用的,每次都会覆盖该函数。
仅使用该函数一次,并将地图的id作为参数传递:
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?sensor=false">
</script>
<script>
function initialize(ltd,lgd,map) {
var myOptions = {
center: new google.maps.LatLng(ltd, lgd ),
zoom: 15,
panControl: false,
zoomControl: false,
mapTypeControl: false,
scaleControl: false,
streetViewControl: false,
overviewMapControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById(map),
myOptions);
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
});
var marker;
marker = new google.maps.Marker({
position: new google.maps.LatLng(ltd, lgd),
map: map
});
}
</script>
<?php
for ($i = 1; $i <= 4; $i++) {
echo "map n:" . $i . "<br/>";
?>
<div id="map_canvas<?php echo $i ?>"
style="margin-top:18px; width: 136px; height: 136px;"></div>
<script>
google.maps.event
.addDomListener(window,
'load',
function()
{
initialize(36.835769,
10.247693,
'map_canvas<?php echo $i;?>')});
</script>
<?php
}
?>