如何输出xml使用google map api结果

时间:2010-02-06 18:20:36

标签: php google-maps

这是html代码

<script src="http://maps.google.com/maps?file=api&v=2&key=" type="text/javascript"></script>
 <!-- According to the Google Maps API Terms of Service you are required display a Google map when using the Google Maps API. see: http://code.google.com/apis/maps/terms.html -->
 <script type="text/javascript">

 var geocoder, location1, location2, gDir;

 function initialize() {
  geocoder = new GClientGeocoder();
  gDir = new GDirections();
  GEvent.addListener(gDir, "load", function() {
   var drivingDistanceKilometers = gDir.getDistance().meters / 1000;
   document.getElementById('results').innerHTML = drivingDistanceKilometers;
  });
 }

 function showLocation() {
  geocoder.getLocations(add1, function (response) {
  location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};

  geocoder.getLocations(add2, function (response) {
  location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};

  //gDir.load('from: ' + location1.address + ' to: ' + location2.address);
  gDir.load('from:' + location1.address + ' to: ' + location2.address);
  });


  });
 }

 </script>


</head>

<body onload="initialize();showLocation()">
 <p id="results"></p>

</body>

输出将是这样的, 1.625

当file_get_contents()在php中的html链接时,它将获得所有html包含标题,但不会得到结果 1.625

希望你理解我的英语

1 个答案:

答案 0 :(得分:2)

我怀疑你没有明白PHP在你的服务器上运行,而Javascript在用户的浏览器中运行。当服务器将代码提供给浏览器时,将数据从服务器传递到浏览器会相当自动地发生。将数据从用户的浏览器传递回服务器更加困难。

您可以使用GDownloadUrl()将结果从Javascript传递回您网站上的其他服务器,或者通过巧妙地使用参数,返回到同一服务器脚本的另一个实例。我不相信可以将它发送回相同服务器脚本的相同实例,就像服务器向客户端发出“请求”并收到“回复”一样。

我建议要么从根本上重新思考策略,无论你实际想要实现什么,并安排事情,以便客户端从服务器请求数据,而不是服务器从客户端请求数据。

考虑投票Issue 235,如果实施,可以让您的服务器直接从Google请求数据,而无需通过客户端的Javascript向Google发送请求。

快速查看您尝试做的是否违反条款也是一个好主意。