我试图将传递给该函数的地理编码生成的坐标初始化,以便可以绘制具有该位置的地图。我已经将变量从php转移到函数initialize()但它没有读取lati和longi值。
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<?php
$dlocation =$_POST['address'];
// Get lat and long by address
$address = $dlocation; // Google HQ
$prepAddr = str_replace(' ','+',$address);
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
$output= json_decode($geocode);
$latitude = $output->results[0]->geometry->location->lat;
$longitude = $output->results[0]->geometry->location->lng;
echo 'latitute:'.$latitude . "\n";
echo 'Longitude:'. $longitude. "\n";
?>
<script type="text/javascript">
function initialize()
{ var lati = "<?php echo $latitude;?>";
var longi = "<?php echo $longitude;?>"
var latlng = new google.maps.LatLng(('lati', 'longi'));
var mapOptions = {
center: new google.maps.LatLng('lati', 'longi'),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map-canvas"),mapOptions);
}
</script>
<title></title>
</head>
<body onload="initialize()">
<div id="map-canvas" style="width: 1000px; height: 900px">
</div>
</body>
</html>
请尽快帮助
答案 0 :(得分:1)
var longi = "<?php echo $longitude;?>"
^ semi-colon is missing
and it should be number, not string
和
var latlng =
new google.maps.LatLng(('lati', 'longi'));
^ you should pass variable value not string
和
center: new google.maps.LatLng('lati', 'longi'),
^ same as above, variable, not string
答案 1 :(得分:0)
将您的功能更改为
提供API引用,google.maps.LatLng类构造函数接受Number
值
function initialize()
{
var lati = parseFloat("<?php echo $latitude;?>");
var longi= parseFloat("<?php echo $longitude;?>");
var latlng = new google.maps.LatLng((lati, longi));
var mapOptions = {
center: new google.maps.LatLng(lati, longi),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map-canvas"),mapOptions);
}
我不知道你的功能是否正确w.r.t谷歌地图,这只是javascript修复