0

I've got a relatively simple Google Maps implementation. It requests the user's geoposition information from the browser and (a) geocodes it, (b) pulls N nearest location records from a database (currently max 20 locations within 25 miles), (c) plots the user's location and the location records as markers, with information about the location (address, etc.) stored as a contentString to be viewed in the infowindow.

Screenshot

What I want to do is pass the lat / lon values for the location to a javascript function that gets directions and plots them on the map. My thought was to include them as a form as part of the contentString:

<form>
<input type="hidden" value="' + location[i].latitude + ' name="sitelat"/>
<input type="hidden" value="' + location[i].longitude + ' name="sitelon"/>
<input type="button" onClick="GetDirs()" value="Directions">
</form>

which would be retrieved in the function:

function GetDirs()
{
    var userlatlng = new google.maps.LatLng(userlat, userlon)

    sitelat = document.getElementById(sitelat);
    sitelon = document.getElementById(sitelon);

    var sitelatlng = new google.maps.LatLng(sitelat, sitelon)

    // continue to get directions from userlatlng to sitelatlng

} //GetDirs

There are 700 records in the database, any 20 of which could be populated on the map at any given time, all of which have 'sitelat' and 'sitelon' properties in the contentString.

My mental block is how to write one function that retrieves the lat / lon from the correct infowindow. What am I missing?

1
  • 1
    You don't even need a form, just create the button and pass the lat and lon parameters to the onClick function
    – JoseM
    Mar 13, 2014 at 19:14

1 Answer 1

2

Change your html to just

<button 
  onClick="GetDirs(' + location[i].latitude + ', ' + location[i].longitude + ')"
>Directions</button>

And your function now is just

function GetDirs(sitelat,sitelon)
{
    var userlatlng = new google.maps.LatLng(userlat, userlon); 
    var sitelatlng = new google.maps.LatLng(sitelat, sitelon);

    // continue to get directions from userlatlng to sitelatlng

} //GetDirs
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.