Google Maps Distance Calculator

uglykidjoe

New Member
I found this piece of code on the web

<!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">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<meta name="robots" content="noindex,follow" />
<title>Calculate driving distance with Google Maps API</title>
<script src="http://maps.google.com/maps?file=ap...TQOC0OXpAMO40FHAxT29dNBGfxqMPq5zwdeiDSHEPL89A" 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 drivingDistanceMiles = gDir.getDistance().meters / 1609.344;
var drivingDistanceKilometers = gDir.getDistance().meters / 1000;
document.getElementById('results').innerHTML = '<strong>Address 1: </strong>' + location1.address + ' (' + location1.lat + ':' + location1.lon + ')<br /><strong>Address 2: </strong>' + location2.address + ' (' + location2.lat + ':' + location2.lon + ')<br /><strong>Driving Distance: </strong>' + drivingDistanceMiles + ' miles (or ' + drivingDistanceKilometers + ' kilometers)';
});
}

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);
}
});
}
});
}

</script>
</head>

<body onload="initialize()">

<form action="#" onsubmit="showLocation(); return false;">
<p>
<input type="text" name="address1" value="Address 1" />
<input type="text" name="address2" value="Address 2" />
<input type="submit" value="Search" />
</p>
</form>
<p id="results"></p>

</body>
</html>

I would like to create a Fabrik form that has the 2 address elements in and then saves to distance between the two in another field. Could some suggest a way forward please ?
 
I recently needed to use a couple of features of Google maps which are not currently supported by our plugin.

The options were to either make a duplicate of the Google Maps element and customise it or bypass it and use a custom template instead.

I took the latter option as I needed streetview and maps side by side with a number of features not currently in the Googlemaps element, (although I'll probably try to add some options as some point).

I created a display element simply to use as a placeholder for a <DIV>. This can be done in a custom template instead but it was easier to let Fabrik do this.

At the top of my templates default.php I put the following
HTML:
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
which loads the Googlemaps script. This may not be the best place to load it or call it but I'm still testing so it will do for now.

You can now use your form JS to call up the loading of the maps and any functions you want to use. There are hundreds of examples on the web which you can practically copy and paste, but you need to make sure the code is Maps V3 and not V2, which I suspect your example code is.

Use Fabrik to create field elements for storing addresses, coordinates and whatever else you need.

I will actually be doing exactly the same task on a little project I'm doing but I won't be at that stage for a week or two. If you can hold on for a couple of weeks I'll provide a writeup on it when done.


Cheesegrits loves Googlemaps and may be able to add the feature at some point. However due to various goings on and people on holidays etc this wouldn't happen anytime soon.
 
No problem, just say if anything is unclear as I obviously don't know your expertise in coding or Fabrik.
 
I haven't seen Felix online for a while. He had some real life issues he was dealing with (some serious house remodeling / repair stuff, iirc).

As he says, I usually love playing with the map element, but right now is just not a good time, as I'm just now about caught up from my last round of spine surgery that took me out for most of August, and am about to go in for my second round (a C5-C6 neck fusion). This time shouldn't be as bad as the lumbat fusion, but it's still going to put a serious dent in my work schedule. So I'm going to be hard pushed just to keep up and then catch up again with support. It'll probably be the new year before I get any time to start playing with adding new features.

-- hugh
 
Hi I would like to second this feature - I wish to create a form which calculates mileage based on 2 address options.
Option 1: A user could choose a preloaded home address ->Destination (plot on Google Map)
Option 2: A preloaded work address ->Destination
The form would then calculate the mileage.
Any suggestions or solutions or plugins! would be very welcome. In the meantime I will try working something out!
 
First off, Hugh...Welcome back as well as Merry Christmas to all and thanks for the post regarding MIA> Felix ;-) He was the first one I actually responded/spoke with 2.5 years on the beginning of what I thought Fabrik had under control. [mapping simply 2 points, at least to start]

http://fabrikar.com/features and of course http://fabrikar.com/help/tutorials?resetfilters=0&clearordering=0&clearfilters=0

>> http://fabrikar.com/help/tutorials/details/3/15

Let alone my goal of building a "trip planner" in which an end user could simply log into their account, enter dates, zip codes & a simple AM/PM drop down select [time zone included] in which would build & calculate. Then, using AJAX simply "ask the user" if they'd like to add another "stop-location" until done.

But back in June of 2012, was told that all things on hold due to release of 3.xx etc. It's now 2.5 years later [and honestly just venting] as I also had prior to Fabrik been testing with and making good headway with http://www.seblod.com/ and Sobi prior to turning to Fabrik as? YES< you guys spoke good English -smile- and the logic that I read, for sure was much better than SOBI as well as both the above used "3rd party plugins" in which I felt would have known would take much longer for any bugs happening during development as well.

The closest also at the time to anything I simply was going to start with was Chris and his http://mypoolstats.com/ratings/locations as he also was limping along with this and I know his dreams were higher for it as well.

My most recent search of just now, produced:

http://fabrikar.com/forums/index.php?search/2582908/&q=google+maps&o=date

And again, "just venting" but I purchased rsform pro 11/9 and not even played with it yet, as well as used it back before fabrik..but just mentioning that it has evolved fine, as well as I am just tossing these other two as well, for "food for thought" and possible reverse engineering to help Fabrik possibly

As total integration with "one system" such as this, was my goal.

Anyway,

1) This is similar to Chris's goal for sure. https://www.sysgenmedia.com/extensions/store-locator and then also I ran across a VirtueMart 2 point Google maps & distance integration as well.

http://www.gjcwebdesign.com/vm2t6/

Well, just wanted to pop on and say Merry Christmas to all, as well as answer and thank Hugh, Rob and the others here as well.
 
Looks like the v3 API makes this fairly easy:

https://developers.google.com/maps/documentation/javascript/distancematrix

... so all it would need would be some custom JS to grab the points from your two elements, call the getDistanceMatrix(), recevie the the resulting JSON data in a callback, grab the bits you want out of it, and display them in something like a read only field element. With a button element that kicks off your custom JS.

-- hugh

Hello Hugh, just thought I'd check in to see if this is getting any closer to a working function please?

Thanks, Jim
 
I recognize the site, but can't remember who's it was.

For the distance between points thing, I wasn't aware anyone was expecting me to do it. I just gave some advice on how to do it using the v3 maps API.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Staff online

Members online

Back
Top