我从数据库中获得了纬度,经度和地址。如何在谷歌地图上显示每个商店的价值:
$qry = mysqli_query($link,"select * from store");
while($row = mysqli_fetch_array($qry)){
$lat = $row['lat'];
$long = $row['lng'];
$adresse = $row['Adresse'];
}
如何在谷歌地图上一个接一个地显示每个商店的地址。在我的代码中,它只显示一个map.how来显示每个地图?
答案 0 :(得分:0)
//JS function
window.onload=initMap;
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
});
show_markers();
}
/*
Considering you have php array with locations
in format ('address'=>add1,'lat'=>lat1,'lng'=>lng1)
$qry = mysqli_query($link,"select * from store");
$marker_data = mysqli_fetch_array($qry);
*/
function show_markers(){
var locations = <?php echo json_encode($marker_data);?>;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i]['lat'], locations[i]['lng']),
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i]['address']);
infowindow.open(map, marker);
}
})(marker, i));
}
}
reffer map相关代码:https://developers.google.com/maps/documentation/javascript/adding-a-google-map