Jump to content

Help getting this google map to automatically grab lat/long


perficut

Recommended Posts

You guys have been a huge help lately and although I like to try and figure things out myself since for me atleast, its a great way to learn and retain. However, Im just stuck.  Heres what I have.

Simple page, loads a map automatically based on the lat/long co-ordinates. Then using a form input, grabs the lat/long co-ords of the new address and displays a map using that info.

I'd like to put this final code into a php file which already has a number of data fields being passed into it, of which is $street $city $zip Since this is the case, can we eliminate the form field here, but still have the action still take place using the above data fields. This way, the page will automatically get the long/lat co-ords of the $street $city $zip which is passed in from a previous form,  and display the appropriate map.

[code]<!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"/>
    <title>Google Maps API Example - Geocoding API</title>
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAzcL9hcXWFSlgOlWZDTLn6hRevAYoGCHvlUG86GujGYzhd2HRrBSSDfr44HxVmYLuH7RbNstxAlBfdQ" type="text/javascript"></script>
    <script type="text/javascript">
    //<![CDATA[


    var map = null;
    var geocoder = null;

   
    function showAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              map.setCenter(point, 18);
              var marker = new GMarker(point);
              map.addOverlay(marker);
              marker.openInfoWindowHtml(address);
            }
          }
        );
      }
    }
    //]]>
   
function load() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(37.4419, -122.1419), 13);
        map.setMapType(G_SATELLITE_TYPE)
geocoder = new GClientGeocoder();
      }
    }

</script>
  </head>

  <body onload="load()" onunload="GUnload()">
   
<form action="#" onsubmit="showAddress(this.address.value); return false">
      <p>
 
        <input type="text" size="60" name="address" value="368 clover court glen burnie md 21060" />
        <input type="submit" value="Go!" />
      </p>
      <div id="map" style="width: 500px; height: 500px"></div>
    </form>

  </body>
</html>
[/code]

In other words.....

Since I already have the data coming ito this file alread in $street $city and $zip fields. I'd like the program to display the map using that info, instead of displaying a default map using the co-ordinates 37.4419, -122.1419 , then asking for the address again, in order to display the right map.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.