javascript to php
Results 1 to 2 of 2

Thread: javascript to php

  1. #1
    Junior Member
    Join Date
    Apr 2003
    Location
    UK
    Posts
    25

    javascript to php

    Hi, i have the following code that takes 2 addresses and returns the distance between 2 places. It is quite clever as in it returns the postcodes and full address in readable format. But i need this to run after a user has entered their details and it runs and returns the values? Also is this by the road or as the crow flies?

    Many thanks in advance.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Google Maps JavaScript API Example: Extraction of Geocoding Data</title>
    <script src="http://maps.google.com/maps?file=api&v=2&key=AIzaSyANQSRMzl2jNUON2VbF24f5xuPm_YtH6l0" 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;

    function initialize() {
    geocoder = new GClientGeocoder();
    }

    function showLocation() {
    geocoder.getLocations(document.forms[0].address1.value, function (response) {
    if (!response || response.Status.code != 200)
    {
    alert("Sorry, we were unable to geocode the first address");
    }
    else
    {
    location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
    geocoder.getLocations(document.forms[0].address2.value, function (response) {
    if (!response || response.Status.code != 200)
    {
    alert("Sorry, we were unable to geocode the second address");
    }
    else
    {
    location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
    calculateDistance();
    }
    });
    }
    });
    }

    function calculateDistance()
    {
    try
    {
    var glatlng1 = new GLatLng(location1.lat, location1.lon);
    var glatlng2 = new GLatLng(location2.lat, location2.lon);
    var miledistance = glatlng1.distanceFrom(glatlng2, 3959).toFixed(1);
    var kmdistance = (miledistance * 1.609344).toFixed(1);

    document.getElementById('results').innerHTML = '<strong>Pickup: </strong>' + location1.address + '<br /><strong>Dropoff: </strong>' + location2.address + '<br /><strong>Distance: </strong>' + miledistance + ' miles (or ' + kmdistance + ' kilometers)';
    }
    catch (error)
    {
    alert(error);
    }
    }

    </script>
    </head>

    <body onload="initialize()">

    <form action="#" onsubmit="showLocation(); return false;">
    <p>
    <input type="text" name="address1" value="Address 1" class="address_input" size="40" />
    <input type="text" name="address2" value="Address 2" class="address_input" size="40" />
    <input type="submit" name="find" value="Search" />
    <? echo $miledistance ?>
    </p>
    </form>
    <p id="results"></p>

    </body>
    </html>
    Kind regards
    Jules
    jules@hungrybear.co.uk

  2. #2
    Senior Member
    Join Date
    Aug 2008
    Location
    London, UK
    Posts
    753
    This has absolutely nothing to do with PHP. You'll probably get a better response by asking on the Google Maps dev forums.
    Ashley Sheridan
    www.ashleysheridan.co.uk

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •