SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do i simplify this google maps distance calculator

    So i put together this distance and rate calculator using google maps api, but since im not that great with javascript just yet, its all kind of pasted together, although it works just fine... im sure theres a better way to do this.... heres the demo
    http://tinyurl.com/bsvu5jn
    (works in san diego only)

    and heres the distance/rate calculator code
    HTML Code:
    	var geocoder, location1, location2, gDir;
     
    	function initialize() {
    		geocoder = new GClientGeocoder();
    		gDir = new GDirections();
    		GEvent.addListener(gDir, "load", function() {
    			var drivingDistanceMiles = gDir.getDistance().meters / 1609.344;
    			var drivingDistanceMiles = Math.round(drivingDistanceMiles*100)/100;
    		
    			var drivingrate1 = Math.round((drivingDistanceMiles*3.00)+2.80);
    			var drivingrate2 = Math.round(((drivingDistanceMiles*3.00)+2.80)*1.15);
    			document.getElementById('results').innerHTML = '<strong>Driving Distance: </strong>' + drivingDistanceMiles + ' miles <br/><strong>Rate estimate: </strong> $' + drivingrate1 + ' to $' + drivingrate2+'<br/>';
    			document.getElementById('map').innerHTML = '<iframe src="distancemap.php?from='+ location1.address+'&to='+ location2.address+'" width="500" border="0" height="250" style="border:0px solid #fff; overflow:hidden">Browser not compatible. </iframe>';
    			document.getElementById('address').innerHTML = '<input type="text"style="display:none" name="pickup" value="'+ location1.address+'"><input type="text" name="dropoff" style="display:none" value=" '+ location2.address+'"/><input type="submit" value="submit"  class="submit" />';
    		});
    	}
     
    	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};
    						gDir.load('from: ' + location1.address + ' to: ' + location2.address);
    					}
    				});
    			}
    		});
    	}
     
    the reason i use iframes is to load another map that shows the route between the starting address and the ending address, heres the code

    HTML Code:
         var map;
        var gdir;
     
        function initialize() {
          if (GBrowserIsCompatible()) {      
            map = new GMap2(document.getElementById("map_canvas"));
            gdir = new GDirections(map, document.getElementById("directions"));
     
            setDirections("<?php echo $_REQUEST['from']; ?> san diego", "<?php echo $_REQUEST['to']; ?> san diego");
    		
    		
          }
        }
        
        function setDirections(fromAddress, toAddress) {
          gdir.load("from: " + fromAddress + " to: " + toAddress);
        }
    	
    id like to learn how to just merge the two codes, anyone know how to do this?

    thanks!

  2. #2
    SitePoint Member
    Join Date
    Jun 2014
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    building a similar rate fare estimator app as well - did u ever fig it out?

  3. #3
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,890
    Mentioned
    211 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    Welcome to the forums

    Quote Originally Posted by proplane64 View Post
    building a similar rate fare estimator app as well - did u ever fig it out?
    Maybe the OP will return and give you an answer, but as the thread is 8 years old, I doubt it.
    I would therefore suggest starting a new thread, stating your problem and providing the coe you have so far.
    That way, you'll be much more likely to get an answer.


Bookmarks

Posting Permissions

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