Jump to content

Geocoding An Address To Get Coordinates For Google Maps API


sintax63

Recommended Posts

I am looking for a way to automatically take a user inputted address (street, city, state, zip) and have it geocoded into lat / lng coordinates for an embedded google map on my site. I have been doing this by hand but is becoming tedious and I know there is a way to do it.

 

I am using two tables for this right now: "profiles" which contains the physical address, and "geocode" that contains a matching ID variable to the profiles entry along with the set of coordinates. Ideally, whenever someone fills out their profile page it would automatically convert the address and then insert into my tables.

 

I've come across a few tutorials online but they are pretty Javascript heavy and that is not a language I'm real familiar with.

Link to comment
Share on other sites

You can do this with the maps API. You just need to create an instance of the geocoder and pass it the address to geocode. The maps api reference has a ton of examples on how to do this.

 

I have an example of how to do this at home but I'm at work. I would be glad to post what I did when I get home

Link to comment
Share on other sites

Oh, I do have an API key and my maps and databases already set up. I was just looking for a way to automate the geocoding process.

 

I guess what I don't understand is how it functions. When someone updates their address on my site, I initiate the request to Google, but then how do I capture the results of that request to enter the coordinates along with their other profile data into my table?

Link to comment
Share on other sites

I found mine...

 

I just did it by filling a form field with the value for insert into the db. It just requires the user to click the get lat/lng link after adding the addres, city, and state.

 

You should be able to adapt it to what you need

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAS3g6APnAOymBE-H6WcqW-RT2yXp_ZAY8_ufC3CFXhHIE1NvwkxTiZR4zDX-LgaGE_-sLB3Vu1Z930g" type="text/javascript"></script>

<script type="text/javascript">
//<![CDATA[
function searchLocations() {
  geocoder = new GClientGeocoder();
  if(document.getElementById('address').value != "" && document.getElementById('city').value != "" && document.getElementById('state').value != "sel") {
  var address = document.getElementById('address').value + " " + document.getElementById('city').value + " " + document.getElementById('state').value;
  geocoder.getLatLng(address, function(latlng) {
	if (!latlng) {
	  alert(address + ' not found');
	} else {
	  document.getElementById('lat_lng').value = latlng.toString();
	}
  });
  } else {
  alert("Please make sure address, city and state are all filled in prior to getting lat/long");
  }
}
//]]>
</script>

</head>

<body>
<form name="form" action="">
<p>*Location Address<br />
  <input name="address" type="text" id="address" size="45" />
  </p>
  <p>*Location City<br />
    <input type="text" name="city" id="city" />
  </p>
  <p>*Location State<br />
    <select name="state" id="state">
      <option value="sel"></option>
      <option value="IA">Iowa</option>
      <option value="IL">Illinois</option>
    </select>
  </p>
<p>*Latitude/Longitude<br />
    <input name="lat_lng" type="text" id="lat_lng" size="45" />
  	<a name="top"></a>
  	<a href="#top" onclick="searchLocations();">Get lat/lng</a></p>
</form>
</body>
</html>

Link to comment
Share on other sites

How are you passing the data to the google maps API?

 

If you're using the cURL library or something similar Google will automatically send you back a response within the request. So for my script's it's as follows (excuse the pseudo code):

 

$xml = curl_exec($data);

// parse the string response from google
$response = simplexml_load_string($xml);

// response is now an object so output the required properties
echo $response->lat;
echo $response->long;

Link to comment
Share on other sites

MatthewJ -

 

Thanks for that code. I have tried it out and it seems like it will work. The one problem I see is that in my table, I have two separate fields; one for $lat and one for $lng.

 

Is there a way to split up the results so I have two separate variables? I picked at the code a bit but not being versed in JS all I did was break it. LOL

Link to comment
Share on other sites

I just use a small snippet on the php side before I insert it like

$parts = explode(",", $result);
$lat = str_replace("(", "", $parts[0]);
$long = trim(str_replace(")", "", $parts[1]));

 

I just threw that together untested, but I'm sure you get the point

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.