Using Yahoo Geocode API and Google Maps
December 1st, 2005
I put an app together this weekend that geocodes an address from an input form and displays the location on a Google map. I’ve cleaned it up a little and put it online for anyone who’s interested…
http://81nassau.com/demos/geocode/
The app consists of a simple HTML form that allows you to input street, city and state. The lookup/submit button on the form initiates an XmlHttpRequest (ajax) to a simple PHP proxy script that I wrote. The PHP script takes the form input (street, city, state) and creates a Yahoo! Geocode REST request, and makes the request to the Yahoo! geocode service. The script then returns the Yahoo! XML data back to the browser. Some javascript is used to parse the XML from Yahoo! and put the lat, lng and zip on the form. A pin is also drawn on the map (Google).
Below is the code for the simple PHP proxy. Feel free to take/use the code as well as the html/javascript. Just do a view-source on the demo link above to see the html/javascript. Please note that there is minimal error checking in all of the code…add where needed.
Enjoy!
< ?php
// Simple proxy script for Yahoo! Geocode API
// Note: there is no error checking done on the input or return from Yahoo!
header('Content-type: text/xml');
$street = $_REQUEST["street"];
$city = $_REQUEST["city"];
$state = $_REQUEST["state"];
$url = 'http://api.local.yahoo.com/MapsService/V1/geocode?appid=YOUR_APP_ID_GOES_HERE' .
'&street=' . urlencode($street) .
'&city=' . urlencode($city) .
'&state=' . urlencode($state);
$xml = get_content($url);
print $xml;
function get_content($url)
{
$string = '';
$handle = fopen($url, "r");
if ($handle)
{
// php 5
$string = stream_get_contents($handle);
/* php4
while (!feof($handle)) {
$string .= fread($handle, 8192);
}
*/
fclose($handle);
}
return $string;
}
?>
January 23rd, 2006 at 8:01 am
I know this must be my poor coding skills, but I get this error “Failed to retrieve and parse geo results”. I named the PHP proxy script “geo_lookup.php” and put it in the same directory as the .html file. Is that what I am supposed too?
January 24th, 2006 at 10:03 am
Hi Dave,
Here are a few things to check:
1) Did you get your own App ID from Yahoo? If you haven’t, sign up for an app id and plug it into the geocode URL in the php script. Just replace YOUR_APP_ID_GOES_HERE with your own. Take a look here: http://developer.yahoo.net/faq/index.html#appid
2) Try calling the geo_lookup.php script directly, putting some values for the street, city and state in the URL. e.g.:
geo_lookup.php?street=350 Fifth Ave&city=New York&state=NY
Make sure a valid XML response is displayed. If you get errors and you’re using php4, you will need to comment out the php5 line (in geo_lookup.php) and uncomment out the php4 lines in the get_content() function.
3) Add an alert call to print out the exception in the javascript of index.html (before the “failed to retrieve and parser geo results” message). e.g.:
} catch(e) {
alert(e);
status.innerHTML = “Failed to retrieve and parse geo results!”;
}
Let me know if you’re still having problems.
January 24th, 2006 at 10:22 am
Wow, what amazing support. Number 2 above was my issue. The geocode is working perfectly now. This is great! Thanks very much!
February 23rd, 2006 at 5:30 am
I’m having a problem where all I get is a map of New York when I use your api. I am passing a street address and zip instead of address, city, state. I modified the .php to use zip instead of city state. What might I be doing wrong.
February 23rd, 2006 at 11:00 pm
Hi Jim,
It’s hard to say what the problem is without looking at the code changes.
What happens when you click on the “Locate on Map” button? There is some status text right above the map that should tell you if the location was found, or if there was a Geocode error (from Yahoo). What sort of status messages do you see?
The other thing to look into is testing the php script by itself. Make sure you can call it with the correct params (zip, etc) and make sure you’re getting back the correct xml from Yahoo. You can test that by calling the php script directly from your browser.
Re: the map showing NYC….in the demo (http://www.81nassau.com/demos/geocode/), I center the map on NYC for no real reason, except that i live there. Anyway, you can change the the lat, lng and zoom level in the following line of the onLoad() function:
map.centerAndZoom(new GPoint(-73.98880, 40.749337), 5);
If you want the center of the US (approx) at a higher zoom level, try:
map.centerAndZoom(new GPoint(-96.6796875, 40.6473035), 13);
Let me know how it goes and if you’re still having problems. If you send me a link to your app, I can take a look at it.
March 9th, 2006 at 7:59 am
I have the Yahoo! App ID filled in. But I got a parse error and realized I was using PHP 4, so I commented the php5 code out and enabled the php4 code. But when I try to send to the geo_lookup.php?street=350 Fifth Ave&city=New York&state=NY I get the following error:
XML Parsing Error: not well-formed
Location: http://members.lycos.co.uk/itzjonas/Maps/geo_lookup.php?street=9410%20Streatham%20Rd&city=Sandy&state=UT
Line Number 2, Column 2:
-^
March 9th, 2006 at 8:14 am
Well, actually I sent in that address too —^ Neither one works :S
March 9th, 2006 at 8:34 am
Oh yeah, I tried the catch error code too and I’m getting:
TypeError: resultset[0] has no properties
March 9th, 2006 at 9:18 am
Jason,
Is your geo_lookup.php the same as the code above in this post or did you modify it?
When I tried your script, I got the same error — somewhere in the script, it’s outputting the following tags: “”. Is your geo_lookup.php outputting those? If you modified the code, feel free to post it or email it to me and I can take a look at it.
March 9th, 2006 at 9:42 pm
Jason,
It could be that lycos is inserting all of the additional html, javascript, ads, etc to each page..and that is screwing up the XML that should be returned from geo_lookup.php. You want to make sure that only the XML data (from Yahoo) is returned from the script. I’m not sure if you can do this from within lycos hosting or not.
If you change the Content-type in geo_lookup.php to “text/plain” and call geo_lookup.php from your browser, you’ll see what you’re getting back from the script (including what lycos throws in there):
header(‘Content-type: text/plain’);
March 9th, 2006 at 10:19 pm
I have the same code, haven’t modified it. Do I need to CHMOD it to a certain privilege? The only thing I’ve changed is in the HTML file I called geocodedemo.html – even then the only thing I added was a link to your geo_lookup.php called Get Source.
March 9th, 2006 at 10:25 pm
That makes sense, Lycos throws in a lot of extra junk to put those banners on there. I’m sure that’s it, I’ll try it on another site. Thanks!
BTW, how did you get that php syntax highlighting for your php code, is that just part WordPress? I see it’s using CSS to do so, but I wasn’t sure if that is hard coded or automatic
March 9th, 2006 at 10:47 pm
You’re welcome!
I’m using the CodeHighlight plugin for WordPress to handle the syntax highlighting. Here is a link to it:
http://www.chroder.com/archives/2005/04/16/wordpress-codehighlight-plugin/
March 22nd, 2006 at 1:29 am
This is an awesome script! It is exactly what I’m looking for… But I keep getting an error. When I goto geo_lookup.php?street=350%20Fifth%20Ave&city=New%20York&state=NY I get:
The XML page cannot be displayed. Cannot view XML input using XSL style sheet.
Invalid at the top level of the document. Error processing resource ‘http://xanthians.com/geo_lookup.php?street=350%20Fifth...
I am using PHP4, so I switched those comment tags around already. I dont know anything about XML, I am a PHP guy, so this is all leaving me pretty clueless.
March 22nd, 2006 at 1:41 am
hah i got it working… so stupid. It was just because my browser wasnt refreshing the page properly. I was pressing refresh (and it acted like it refreshed) but I dunno. Its done that before to me, no clue why.
April 20th, 2006 at 1:29 pm
Hello,
I have added this code in my website as per your instruction but when i enter the state city and address in the for it is giving me error
“Failed to retrieve and parse geo results!”.
When i explicitly run geo_lookup.php page with passing paramenters to it that page shows me the xml data
Can youlz help me.
April 21st, 2006 at 4:58 am
Hi Ashwini,
Try adding an alert call to print out the exception in the javascript of index.html (before the “failed to retrieve and parser geo results†message). e.g.:
} catch(e) {
alert(e);
status.innerHTML = “Failed to retrieve and parse geo results!â€;
}
The alert should give you an idea of what the problem is.
Let me know…
April 21st, 2006 at 12:41 pm
I have added the alert(e) before the fail message in code.
Its is giving me.
[object Error]
May 3rd, 2006 at 11:52 pm
im attempting to use your script, but i’m having a bit of a problem. when i add the alert i get “ResultSet[0] has no properties.” when i change the php file to type text/plain and type the url in directly, the xml object looks fine. when i do this same thing with type text/xml i get “XML Parsing Error: xml declaration not at start of external entity.” any ideas?
thanks
May 3rd, 2006 at 11:54 pm
and the arrow in the error does point to the xml declaration
^
May 4th, 2006 at 12:03 am
nevermind, i got it