sintax63 Posted February 10, 2010 Share Posted February 10, 2010 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 https://forums.phpfreaks.com/topic/191621-geocoding-an-address-to-get-coordinates-for-google-maps-api/ Share on other sites More sharing options...
MatthewJ Posted February 10, 2010 Share Posted February 10, 2010 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 https://forums.phpfreaks.com/topic/191621-geocoding-an-address-to-get-coordinates-for-google-maps-api/#findComment-1010086 Share on other sites More sharing options...
GKWelding Posted February 10, 2010 Share Posted February 10, 2010 As above. Sign up, get an API key, pass in the address string or whichever method you choose and get a return that will include long and lat. You can also choose the return type but I tend to parse it as XML. Link to comment https://forums.phpfreaks.com/topic/191621-geocoding-an-address-to-get-coordinates-for-google-maps-api/#findComment-1010089 Share on other sites More sharing options...
sintax63 Posted February 10, 2010 Author Share Posted February 10, 2010 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 https://forums.phpfreaks.com/topic/191621-geocoding-an-address-to-get-coordinates-for-google-maps-api/#findComment-1010096 Share on other sites More sharing options...
MatthewJ Posted February 10, 2010 Share Posted February 10, 2010 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 https://forums.phpfreaks.com/topic/191621-geocoding-an-address-to-get-coordinates-for-google-maps-api/#findComment-1010122 Share on other sites More sharing options...
GKWelding Posted February 10, 2010 Share Posted February 10, 2010 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 https://forums.phpfreaks.com/topic/191621-geocoding-an-address-to-get-coordinates-for-google-maps-api/#findComment-1010124 Share on other sites More sharing options...
sintax63 Posted February 10, 2010 Author Share Posted February 10, 2010 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 https://forums.phpfreaks.com/topic/191621-geocoding-an-address-to-get-coordinates-for-google-maps-api/#findComment-1010140 Share on other sites More sharing options...
MatthewJ Posted February 10, 2010 Share Posted February 10, 2010 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 https://forums.phpfreaks.com/topic/191621-geocoding-an-address-to-get-coordinates-for-google-maps-api/#findComment-1010420 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.