我有这个代码从数据库中获取细节并将它们放在php数组中,稍后将其传递给javascript以生成Google地图:
<?php
$query='select name,longitude,latitude from maps';
$result=mysql_query($query) or die();
$num_rows=mysql_num_rows($result);
if($num_rows > 0){
for($i=0;$i<$num_rows;$i++){
$row=mysql_fetch_row($result);
$location[]= $row[0].', '.$row[2].', '.$row[1].','.($i+1);
}
//this creates the following array
array(4) {
[0]=> string(44) "Travel agent Marko, 42.5624525, 27.5263493,1"
[1]=> string(44) "Travel agent Marko, 42.5632957, 27.5267364,2"
[2]=> string(54) "Travel agent 'Planet Travel', 42.5609811, 27.5263934,3"
[3]=> string(56) "Travel agent 'Planet Travel' 2, 42.5642942, 27.5266121,4"
}
?>
<script type="text/javascript">
var loc = <?php echo json_encode($location); ?>;
var locations = [
[loc]
];
var map = new google.maps.Map(document.getElementById('googlemap'), {
zoom: 10,
center: new google.maps.LatLng(42.5624525, 27.5263493),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
alert(locations.length);
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
</script>
<?php
}
?>
我的问题是为什么标记不会出现在地图上?没有返回错误,但也没有标记。 我是否必须在php循环中移动javascript代码? 如果我提醒locations.length我得到1作为locations.length。为什么js将位置数组计为1个位置?提前谢谢!
答案 0 :(得分:3)
改变这个:
UITextField
到此:
var loc = <?php echo json_encode($location); ?>;
var locations = [
[loc]
];
循环应该是:
var locations = <?php echo json_encode($location); ?>;