我正在尝试获取由传递给函数初始化的地理编码生成的坐标,以便可以绘制具有该位置的地图。我已经将变量从 php 转移到函数 initialize() 但它没有读取纬度和经度值。
<!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>