www.webdeveloper.com
Results 1 to 2 of 2

Thread: Google map Gdirection problem

  1. #1
    Join Date
    May 2011
    Posts
    4

    Google map Gdirection problem

    I have an html form and when i submit the form it call the function test(address) and parse the value of the form as a parameter.

    Javascript code:
    var geocoder, location1, location2, gDir;
    var to = "102-108 St Georges Road, Preston, VIC 3072, Australia";

    geocoder = new GClientGeocoder();
    gDir = new GDirections();


    function test(address)
    {
    var from = document.getElementById("address").value;

    var test = Distance(from, to);

    alert(test);
    }


    function Distance(fromAddress, toAddress) {
    GEvent.addListener(gDir, "load", function() {
    var drivingDistanceKilometers = gDir.getDistance().meters / 1000;
    return drivingDistanceKilometers;
    });
    geocoder.getLocations(fromAddress, function (response) {
    if (response || response.Status.code == 200)
    {
    location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
    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);
    }
    });
    }
    The function test call the function Distance which in return calculate the distance in kilometres between 2 locations but it's not working properly. I receive an alert with undefined. I try to change return drivingDistanceKilometers; to alert( drivingDistanceKilometers); in Distance function but again it's not working properly. I receive 2 alerts, the first on is still undefined but the second alert actually calculate the distance.

    Can anyone help me please. I want the Distance function to return the drivingDistanceKilometers but not in an alert.

  2. #2
    Join Date
    May 2011
    Posts
    4
    I slightly modified my code:

    Code:
    <html>
    <head>
    	....
            ....
            ....
    	<script type="text/javascript">
     	var geocoder, gDir, location1, drivingDistanceKilometers;
     	geocoder = new GClientGeocoder();
    	gDir = new GDirections();
    
    	function Distance(fromAddress) {
    		var d1 = tomap(fromAddress);
    		alert(d1);
    	}
    	function tomap(address) 
    	{
    		geocoder.getLocations(address, function (response) {
    			
    			if (response || response.Status.code == 200)
    			{
    				location1 = {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: 25 Spencer Street, Nothrocte, VIC 3070, Australia' );
    			}
    		});
    			return gDir.getDistance().meters/1000;
    	}
    </script>
    </head>
    <body>
    	<form action="#">
    		<p>
    			<input type="text" name="address1" />
    			<input type="button" onclick="Distance(address1.value)" value="Search" />
    		</p>
    	</form>
    </body>
    </html>
    Now I have to click the search button twice or even more to get the result.

    Any help please. I've been struggling for days with that one

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
  •  
HTML5 Development Center



Recent Articles