-2

我正在尝试使用带有随机坐标的 Maps API,但我不知道,我需要一些信息,如何将 .js 文件中的 php 值从 php 文件中放入 javascript 函数中?例如,如果我们有几个代码:

1)API代码生成2坐标之间的路线(order2.js):

(function() {
window.onload = function() {
// Creating a map
var options = {
zoom: 8,
center: new google.maps.LatLng(-33.3834, -70.6),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map'), options);
// Creating an array that will contain the points for the polyline
var route = [
new google.maps.LatLng(-33.02644,-71.539775),
new google.maps.LatLng(-33.605148,-70.702197)
];
// Creating the polyline object
var polyline = new google.maps.Polyline({
path: route,
strokeColor: "#ff0000",
strokeOpacity: 0.6,
strokeWeight: 5
});
// Adding the polyline to the map
polyline.setMap(map);
};
})();

2)谷歌地图API代码显示(map2.html):

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Chapter 4 - Google Maps API 3</title>
<link rel="stylesheet" href="css/graphic.css" type="text/css" media="all" />
<script type="text/javascript"
src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="js/order2.js"></script>
</head>
 <body>
 <input type="button" value="getValues" id="getValues" />
 <input type="button" value="changeValues" id="changeValues" />
 <div id="map"></div>
 </body>
</html>

3)如果我有下一个浮动随机代码(random.php):

<?php

    while($x <= 2995 and $y <= 71333)
    {
    $x = rand(2025,2995);
    $y = rand(70116,71333);
    $w = $x/100;
    $z = $y/1000;
    echo $w."<br>".$z."<br>";    
}    

?>

现在根据之前的代码,我真的需要知道:

如何将 $w 和 $z 值从 random.php 传输到与 order2.js 相关的任何函数(a,b)?

感谢您的有用帮助。

问候

4

2 回答 2

0

1)如果它只是用于创建随机数,你可以很容易地用 javascript 做到这一点。

2)如果从 php 获取数据真的很重要,最好的方法是创建一个对最终用户隐藏的 textarea。

在 textarea 中放置您要传输到 JSON 格式的 js 文件的所有数据!像这样的东西:

<textarea id="phpData" style="display:none">{"J":5,"test2":"N"}</textarea>

现在如果你想从 js 访问它:

var thePhpData = JSON.parse( $("#phpData").val() );

获取值:

 thePhpData.J
 thePhpData.test2
于 2013-09-25T04:53:22.510 回答
0

唯一的方法是你必须将你的 JavaScript 包含到一个 PHP 文件中......

Order2.php

<?php
include("random.php");

echo "
<script type='text/javascript'>

function(){
...
// to use the values in its place
".$w." .... ".$z."
}
";

</script>

?>

....是其他代码

于 2013-09-25T04:59:06.843 回答