[英]Google Map API: How to display map marker with array from PHP?
I'm trying to use an array in PHP
to be var locations
in javascript. 我试图在
PHP
使用数组作为javascript中的var locations
。 This is my array : 这是我的数组:
Array (
[0] => Array (
[0] => 001
[1] => A
[2] => 98
[3] => -8.0092722
[4] => 110.2965559
)
[1] => Array (
[0] => 003
[1] => C
[2] => 90
[3] => -7.9021438
[4] => 110.3735387
)
)
And i'm trying to pass that array using json_encode
like below : 我正在尝试使用
json_encode
传递该数组,如下所示:
var markers = <?php json_encode($setarray); ?>
The last one, this is my code to create marker : 最后一个,这是我创建标记的代码:
var infowindow = new google.maps.InfoWindow(), marker, i;
var bounds = new google.maps.LatLngBounds();
for (i = 0; i < markers.length; i++) {
pos = new google.maps.LatLng(markers[i][3], markers[i][4]);
bounds.extend(pos);
marker = new google.maps.Marker({
position: pos,
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(markers[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
map.fitBounds(bounds);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
I'm pretty sure my problem is when i try to pass the array to javascript, can you help me? 我很确定我的问题是当我尝试将数组传递给javascript时,您能帮我吗? Actually this is my first time i use javascript, and i'm not pretty familiar with it.
实际上,这是我第一次使用javascript,但我不太熟悉。 Thank you.
谢谢。
If you pass a php variable in javascript you have to echo the variable var jsonMarkers = <?php echo json_encode($setarray); ?>
如果在javascript中传递php变量,则必须回显变量var
jsonMarkers = <?php echo json_encode($setarray); ?>
jsonMarkers = <?php echo json_encode($setarray); ?>
Also to convert it to the desired JSON object you have to do this 还要将其转换为所需的JSON对象,您还必须执行此操作
var markers = JSON.parse(jsonMarkers);
You can then use like this 然后您可以像这样使用
markers[i][j];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.