views:

314

answers:

3

I have a form that a user needs to fill out where the location needs to be identified. I have latitude and longitude input fields on the form. I am also looking for the decimal lat and long. What I would like seems really simple. I just want to have a link that the user can click on that would popup a map (google or yahoo) and he could use the map to find the location and just click on it and that would automatically populate the two input fields for lat and long from the map. Has anyone done this before?

A: 

You will need to know the lat/long of at least TWO points on the map, preferably not very close together, and also know their pixel coordinates relative to one corner (i.e. the best two points would be opposite corners). Then it's simple math to convert the pixel coordinate of the click into lat/long by interpolation. However, note that this works only as long as the area covered by the map is "flat" relative to the Earth's curvature. If the map covered a large area, say 6000 miles wide, there would be significant distortion. If you needed this to be accurate you'd have to do some map projection to calculate the position.

Jim Garrison
A: 

Check out the Google Maps API as well as this link: http://www.gorissen.info/Pierre/maps/googleMapLocation.php

I think that the Google Maps API probably can do what you're looking for.

kmontgom
Google maps API at http://code.google.com/apis/maps/documentation/reference.html
kmontgom
Thanks for the link, i'm going to check it out.
Joe
+3  A: 

Using the Google Maps API, you can do this pretty easily. Just add a click event handler to your map object, which passes in the latitude/longitude coordinates of where the user clicked (see details on the click event here).

function initMap()
{
    // do map object creation and initialization here
    // ...

    // add a click event handler to the map object
    GEvent.addListener(map, "click", function(overlay, latLng)
    {
        // display the lat/lng in your form's lat/lng fields
        document.getElementById("latFld").value = latLng.lat();
        document.getElementById("lngFld").value = latLng.lng();
    });
}
Tim S. Van Haren