rellis32 Posted March 18, 2008 Posted March 18, 2008 I put together this php to geocode an address in filemaker. I am new to PHP and this is the first PHP script I have ever written. I got most of the code from Begining Google Maps Applications with PHP and Ajax (great book). This was done using the Testbed v2 file. I created new fields, address, city, state, and zip to pass to the script. header('content-type:text/plain;'); $api_key = "...enter key here..."; // First we store our FileMaker expression $address= fm_evaluate('GetField ( "address" )'); $city = fm_evaluate('GetField( "city" )'); $state = fm_evaluate('GetField( "state" )'); $zip = fm_evaluate('GetField( "zip" )'); // Create a CURL object for later use $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Construct the request string $url = "http://maps.google.com/maps/geo?output=xml&key=$api_key&q="; $url .= urlencode($address); $url .= "&city=".urlencode($city); $url .= "&state=".$state; $url .= "&zip=".$zip; $url2 = "http://maps.google.com/maps/geo?output=xml&key=$api_key&q=".$zip; // Query Geocoder.ca for the lat/long curl_setopt($ch, CURLOPT_URL, $url); $response = curl_exec($ch); // Use SimpleXML to parse our answer into something we can use $googleresult = simplexml_load_string($response); //echo "Status: ".$googleresult->Response->Status->code."n"; if ($googleresult->Response->Status->code != 200 || $googleresult->Response->Placemark->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->Locality->PostalCode->PostalCodeNumber != $zip){ curl_setopt($ch, CURLOPT_URL, $url2); $response = curl_exec($ch); $googleresult = simplexml_load_string($response); echo "Results based on zip coden"; foreach ($googleresult->Response as $response){ foreach ($response->Placemark as $place){ list($longitude, $latitude) = split(",",$place->Point->coordinates); //echo "Result Address: ".$place->address."n"; echo "$latituden"; echo "$longituden"; } } } else foreach ($googleresult->Response as $response){ foreach ($response->Placemark as $place){ list($longitude, $latitude) = split(",",$place->Point->coordinates); //echo "Result Address: ".$place->address."n"; echo "Results based on full addressn"; echo "$latituden"; echo "$longituden"; } // for each placemark } //for Google result // Close the CURL file and destroy the object curl_close($ch); The script will take the address and geocode it, it then checks the returned zip code to insure it matches the original zip coe, if it doesn't it geocodes againonly passing the zip code in the url. I found this necessay because if I passed in a RR Box address in illinois, google was returningcooridinates for California. This doesn't work real well when trying to plot points on a map. So basically if google cannot find the exact address, the script retrieves the coordinates for the zip code only. Any suggestions on an easier or more elegant way of doing this would be greatly appreciated, as I am just learning PHP. Hope his helps someone out :)
Recommended Posts
This topic is 6345 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now