我正在尝试从数据库中获取坐标值,并在所有保存的位置上放置一个标记。我没有显示任何错误,但它没有在 map 上放置标记。任何帮助将不胜感激。
<div id="floating-panel">
<input id="address" type="textbox" placeholder="type your address here">
<input id="submit" type="button" value="Click for location">
</div>
<script>
function initMap() {
var mapProp = {
center: {lat: 33.641656100000000000, lng: 72.983588599999960000},
zoom:8,
panControl:true,
zoomControl:true,
mapTypeControl:true,
scaleControl:true,
streetViewControl:true,
overviewMapControl:true,
rotateControl:true,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map'),mapProp);
var geocoder = new google.maps.Geocoder();
document.getElementById('submit').addEventListener('click', function() {
geocodeAddress(geocoder, map);
});
}
function geocodeAddress(geocoder, resultsMap) {
var address = document.getElementById('address').value;
geocoder.geocode({'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
resultsMap.setCenter(results[0].geometry.location.lat(),results[0].geometry.location.lat());
var marker = new google.maps.Marker({
map: resultsMap,
position: results[0].geometry.location,
animation:google.maps.Animation.BOUNCE
});
//var latitude = results[0].geometry.location.lat();
//var longitude = results[0].geometry.location.lng();
window.location = "distance_cal.php?Latitude=" + results[0].geometry.location.lat() + "&Longitude="+ results[0].geometry.location.lng();
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
<?php
$query = mysql_query("select * from saved_positions") or die (mysql_error());
while($row=mysql_fetch_array($query))
{
$scl=$row['school_name'];
$city=$row['city'];
$lon2=urlencode($row['longitude_s']);
$lat2=urlencode($row['latitude_s']);
?>
var lat='<?php echo '$lat2'?>';
var lon='<?php echo '$lon2'?>';
var myLatLng = {lat, lng};
var marker = new google.maps.Marker({
map: ResultMap,
position: myLatLng,
animation:google.maps.Animation.BOUNCE
});
<?php
}
?>
}
</script>
最佳答案
这是非常奇怪的代码。
var lat='<?php echo '$lat2'?>';
var lon='<?php echo '$lon2'?>';
var myLatLng = {lat, lng};
应该是这样的:
var lat = <?php echo $lat2?>;
var lon = <?php echo $lon2?>;
var myLatLng = {lat: lat, lng: lon};
您在代码中的何处定义了变量ResultMap
?
var marker = new google.maps.Marker({
map: ResultMap, // <--- where does this value come from?
position: myLatLng,
animation:google.maps.Animation.BOUNCE
});
我建议你使用任何模板系统(比如 smart 之类的)来分离 PHP 代码和 JS 代码。
关于javascript - PHP MySQL 中的地理编码动态标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35826902/