本文介绍了将值从PHP传递给html函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图获取由地理编码生成的坐标传递给函数初始化,以便可以绘制具有该位置的地图。我已经从PHP的变量继承了函数initialize(),但它没有读取lati和longi值。
<!DOCTYPE html>
< html>
< head>
< script type =text / javascriptsrc =https://maps.googleapis.com/maps/api/js?sensor=false>< / script>
$ dlocation = $ _ POST ['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。 \\\
;
回声'经度:'。 $经度。 \\\
;
?>
< script type =text / javascript>
函数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-canvasstyle =width:1000px; height:900px>
< / div>
< / body>
< / html>
请尽快帮忙
解决方案
var longi =<?php echo $ longitude;?>
^分号缺少
,它应该是数字,而不是字符串
和
var latlng =
new google.maps.LatLng(('lati','longi')) ;
^你应该传递变量值而不是字符串
和
center:new google.maps.LatLng('lati','longi'),
^与上面相同,变量不是字符串
am trying to get the coordinates generated by the geocode passed to the function initialize so that a map with the location can be drawn. i have carried over the variables from the php to the function initialize() but it is not reading the lati and longi values.
<!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>
please help ASAP
解决方案
var longi = "<?php echo $longitude;?>"
^ semi-colon is missing
and it should be number, not string
And
var latlng =
new google.maps.LatLng(('lati', 'longi'));
^ you should pass variable value not string
And
center: new google.maps.LatLng('lati', 'longi'),
^ same as above, variable, not string
这篇关于将值从PHP传递给html函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
08-19 02:35