views:

328

answers:

1

here is the html code

<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;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>

the output will be somthing like this,1.625

when file_get_contents() the html link in php,it will get all the html include title,but not the result 1.625 ,

hope you understand my english

+1  A: 

I suspect that you don't get the point that PHP runs on your server and the Javascript runs in the user's browser. Passing data from server to browser happens fairly automatically as the server serves the code to the browser. Passing data from the user's browser back to your server is more dificult.

You can use GDownloadUrl() to pass the result from the Javascript back to another server on your site, or, with cunning use of parameters, back to another instance of the same server script. I don't believe it's possible to send it back the the same instance of the same server script, as if the server had made a "request" to the client and received a "reply".

I suggest either fundamentally rethinking the strategy for whatever it is you're actually trying to achieve and arrange things so that the client requests data from the server rather than the server requesting data from the client.

Consider voting for Issue 235 which, if implemented, would make it possible for your server to request the data directly from Google, rather than needing to make the request to Google via Javascript in the client.

It would also be a good idea to have a quick look to see if what you're trying to do is a violation of the Terms.

Mike Williams