【发布时间】:2017-11-18 11:59:28
【问题描述】:
我尝试从 mysql db 中获取 lat 和 long 值并将这些值传递到谷歌地图上以在地图上显示位置标记
但目前我只获取最后一行数据,因此仅指示一个位置 如何在地图上显示所有位置??
<?php
include "config.php";
$result="select * from ds_duty_history";
$a=mysqli_query($conn,$result);
// $count_row = mysqli_num_rows($a);
while ($b = mysqli_fetch_array($a)) {
$long_d=$b['lng'];
$lat_d=$b['lat'];
$result = array(array('latitude'=>$lat_d,'longitude'=>$long_d));
}
?>
<!doctype html>
<html>
<head>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=YOURKEY&callback=initMap">
</script>
<script type="text/javascript">
var map;
function initialize() {
// Set static latitude, longitude value
var latlng = new google.maps.LatLng(<?php echo $lat_d; ?>, <?php echo $long_d; ?>);
// Set map options
var myOptions = {
zoom: 16,
center: latlng,
panControl: true,
zoomControl: true,
scaleControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
// Create map object with options
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
<?php
// uncomment the 2 lines below to get real data from the db
// $result = mysql_query("SELECT * FROM parkings");
// while ($row = mysql_fetch_array($result))
foreach($result as $row){ // <- remove this line
echo "addMarker(new google.maps.LatLng(".$row['latitude'].", ".$row['longitude']."), map);";}
?>
}
function addMarker(latLng, map) {
var marker = new google.maps.Marker({
position: latLng,
map: map,
draggable: true, // enables drag & drop
animation: google.maps.Animation.DROP
});
return marker;
}
</script>
</head>
<body onload="initialize()">
<div style="float:left; position:relative; width:550px; border:0px #000 solid;">
<div id="map_canvas" style="width:550px;height:400px;border:solid black 1px;"></div>
</div>
</body>
</html>
当前仅显示一个标记,即最后一次获取记录 lat 和 long 分配在地图上 如何使用 mysql lat,long 值在地图上显示所有地方?
【问题讨论】:
-
“最后一行”是指
ORDER BY datetime DESC LIMIT 10? -
在表中具有纬度和经度值,这些是一个或多个记录,例如不同的地方,我使用这些纬度和经度值在地图上显示位置.....,使用此代码我能够只显示一个地方...,那么如何在地图上显示所有地方??
标签: javascript php mysql google-maps latitude-longitude