我在PHP中的数组中有多个LAT / LONG,但是可用于显示lat / long的代码是显示单个lat / long,我试图在for
循环中迭代数组,但它没有'工作如下所示:
<html>
<head>
<style type="text/css">
div#map {
position: relative;
}
div#crosshair {
position: absolute;
top: 192px;
height: 19px;
width: 19px;
left: 50%;
margin-left: -8px;
display: block;
background: url(crosshair.gif);
background-position: center center;
background-repeat: no-repeat;
}
</style>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var map;
var geocoder;
var centerChangedLast;
var reverseGeocodedLast;
var currentReverseGeocodeResponse;
function initialize() {
<?php for($i=0;$i<count($data);$i++){?>
var latlng = new google.maps.LatLng(<?php echo $data[$i]['lat'].','.$data[$i]['long']; ?>);
var myOptions = {
zoom: 40,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
geocoder = new google.maps.Geocoder();
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: "<?php echo $data[$i]['deviceID'];?>"
});
<?php }?>
}
</script>
</head>
<body onLoad="initialize()">
<div id="map" style="width:200px; height:200px">
<div id="map_canvas" style="width:100%; height:200px"></div>
<div id="crosshair"></div>
</div>
</body>
</html>
我怀疑javasctipt代码中必须有一些参数multiple
才能使其正常工作,但不确定...可能有人帮助解决这个问题。
答案 0 :(得分:1)
您正在每次迭代中创建新的map
。
将map
定义和geocoder
定义放在foreach
之外:
function initialize() {
var myOptions = {
zoom: 40,
center: latlng, // set some default latlng here, e.g $data[0]['lat'], $data[0]['lng']
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
geocoder = new google.maps.Geocoder();
<?php for($i=0;$i<count($data);$i++){?>
var latlng = new google.maps.LatLng(<?php echo $data[$i]['lat'].','.$data[$i]['long']; ?>);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: "<?php echo $data[$i]['deviceID'];?>"
});
<?php }?>
}