Geocoding UK Postcodes with Google Map API
Notice: As a few people have pointed out, this announcement from Google means the technique I describe below is no longer needed. Thanks Google!
Google Maps API provides a geocoding feature, for finding the latitude and longitude of places or addresses; but it does not work for UK postcodes. This is thanks to Royal Mail who have a copyright on the data, and are very restrictive with their (expensive) licenses for it.
There are various solutions out there for using 3rd party services and importing the data to be used with Google Maps, or for using community built databases for the info. However, I’ve had a few people ask me about doing it just though Google.
It is possible — Google AJAX Search API does provide geocoding for UK postcodes. We need to use the two APIs in harmony to achieve our result.
So here it is.
Step by step
I’ll assume you already know how to use Google Maps API, and you came here just looking how to add geocoding for the UK.
Step 1.
Grab a two API keys, if you already have your Google Maps API key, just grab an AJAX search key. You can get them here:
http://www.google.com/apis/maps/signup.html
http://code.google.com/apis/ajaxsearch/signup.html
Step 2.
Google will give you a sample page, you need to stick your two API keys at the top of the page, followed by a reference to your Javascript file:
<script src="http://maps.google.com/maps?file=api&v=2&key=*KEY*"
type="text/javascript"></script>
<script src="http://www.google.com/uds/api?file=uds.js&v=1.0&key=*KEY*"
type="text/javascript"></script>
<script src="gmap.js" type="text/javascript"></script>
Ensure the reference to your Javascript file comes after the two API keys.
Step 3.
In addition to the Google Maps API stuff, you need to stick a reference to Google local search at the top of your Javascript file:
var localSearch = new GlocalSearch();
You can grab my Javascript file right here, but remember you’ll need to change the API keys.
Step 4.
The key to this Geocoder is only a single function:
function usePointFromPostcode(postcode, callbackFunction) {
localSearch.setSearchCompleteCallback(null,
function() {
if (localSearch.results[0]) {
var resultLat = localSearch.results[0].lat;
var resultLng = localSearch.results[0].lng;
var point = new GLatLng(resultLat,resultLng);
callbackFunction(point);
}else{
alert("Postcode not found!");
}
});
localSearch.execute(postcode + ", UK");
}
It takes 2 arguments; postcode is the postcode you want to look for, and callbackFunction is the function you wish to run on the results.
Why is it necessary to do it this way? It is the way AJAX, and thus Google AJAX Search API, works - the request is sent, and a callback function is designated to handle the results returned, when they are ready.
In our case, the callback function can do whatever you want with the results, which will come in the format of a GLatLng (often just called a point); I’ve supplied 2 sample functions, placeMarkerAtPoint and setCenterToPoint which do pretty much what they sound like they do.
Step 5.
Putting aside accessibility and graceful degradation for the sake of simplicity in this tutorial, the last step we need is just to add some hooks into our Javascript:
<input type="text" id="postcode" size="10" />
<input type="submit" value="Place Marker" onclick="javascript:
usePointFromPostcode(document.getElementById('postcode').value, placeMarkerAtPoint)" />
We have a field for inputting a postcode, and I’ve added a button for placing a marker there. Where I have placeMarkerAtPoint you can put a reference to your own function, or you can even add a function right in there, like this:
<input type="submit" value="Do whatever" onclick="javascript:
usePointFromPostcode(document.getElementById('postcode').value,
function (point) {
alert('Latitude: ' + point.lat() + '\nLongitude: ' + point.lng());
})" />
Demo
If you are coming in from an RSS reader, either visit this blog post on the site, or see the demo page.
Postcode:
Conclusion
Until Royal Mail sort get their act together, and relax the licensing agreement, hopefully this will help people who want a ‘pure’ Google solution and hadn’t come across this option. Please use the comments section to let me know if you are using this, or if you have any improvements or suggestions.
Technorati Tags: postcode, google maps, api, Web 2.0, Web-Services
Dan said,
March 5, 2007 @ 4:29 pm
Brilliant, so simple been looking to the answer for this one for a while.
Lee said,
March 8, 2007 @ 7:57 am
Well, Well, Well Mr Anthony… You decided to actually do some work sitting in the bloody caribean!! .. hehehhe
Nice post bud… I’ll have a good read later today when I am more awake, hope your doing well over there
Eliphas said,
March 12, 2007 @ 5:43 pm
Nice tutorial, but now I would like to go further and i am stuck. how can i query the geocoder from a from and retrieve the lon and lat from the javascript and inseet them into a database can you provide me some tips .
thanks a lot.
Tom said,
March 12, 2007 @ 5:58 pm
Hi Eliphas,
Previously, in the terms and conditions there was a clause that you couldn’t store geocode data back into a database; however it doesn’t seem to be there anymore.
Because the lat/long are retrieved via Javascript, you will need to use an AJAX request to send this information back to your server. You would probably create a server side script (PHP, ASP or similar), and then POST this data back to your server via an AJAX connection.
Your server side script would then receive the data and write it to your database.
If I get time, I will write a quick tutorial on caching geocoding results this week. That should help you if you need further assistance.
mark said,
March 14, 2007 @ 1:38 pm
Hi,
Thanks for your tutorial, how would you get the following page to work and open the map directly on the passed postcode?
http://www.pets4homes.co.uk/pets4homes/home.nsf/TestMap?openForm&PostCode=bb24jh
I tried adding the following line to the onload event but it doesnt work :
usePointFromPostcode(document.getElementById(’postcode’).value, setCenterToPoint)
Thanks
Mark
Tom said,
March 14, 2007 @ 2:07 pm
Hi Mark,
getElementById()will only retrieve elements in the DOM tree; usually meaning elements you can see rendered on the page.What you need to do it to get a query string parameter from the URL; it isn’t as scary as it sounds, and there is a good article (complete with function) on it here:
http://www.netlobo.com/url_query_string_javascript.html
Using the function from the article, you’d then need to do something like:
I’ve not tested this code - but it should be something like that.
Good luck!!
Ryan Cullen said,
March 14, 2007 @ 4:11 pm
THANK YOU.
I’ve been looking for something like this for years. Not only does it do a full postcode lookup, it doesn’t require my rather slow page to reload each time.
ianM said,
March 14, 2007 @ 9:10 pm
Thanks a lot for that. I shall try it later, but it is something I am about to try so should get me up and running really quickly. As far as I can see from the two keys, they are identical. Definitely worth a digg.
Tim said,
March 16, 2007 @ 12:29 am
Hi thanks for your post -
I am very much starting out with this - so please forgive what might prove to be the stupidest question going - your gmap.js does not contain the api keys so I was concerned as to the format that they go in as I tried using the same tags as in the html and it doesn’t work - if i take them out I can get a lat long returned but that’s about all ( I wasn’t expecting a lat long without the api keys)
Many thanks again
Dave said,
March 17, 2007 @ 9:23 am
Hi Tom
Great tutorial - any news yet on the caching geocoding - this is exactly what i need.
Keep up the great work.
Thanks
Tom said,
March 18, 2007 @ 3:51 pm
Hi Tim,
I see from your link you seem to be up and running now. I include the API key in the HTML file, not the Javascript file. In the HTML, I first include the 2 Google scripts (with API keys), and then include my gmap.js file, which has my functions.
Dave,
Busy week! I’ll try to get it up in the next couple of days.
Alan said,
March 20, 2007 @ 2:50 am
Hi,
Firstly thanks for this example, it has proved to be extremely helpful.
I wondered if you could perhaps offer your insight too a small problem I am having. I have an array of postcodes which I want to place on a map, which I can do without problem. However each postcode has an associated info popup and I need to somehow feed the data I need to place in the popup to the result callback handler for the specific postcode it belongs to. I assume there’s a simple way of going about this but being a Javascript rookie I can’t seem to dig up any answers.
Chris said,
March 21, 2007 @ 11:53 am
Tom, thanks very much for posting this, very useful information! Got it all working on my site with no problems.
Devblog » Blog Archive » Postcodes to Google Maps on your website said,
March 21, 2007 @ 4:40 pm
[…] Using the default Google Maps API you can only include maps based on longitude and latitude and not on a UK postcode. After a bit of searching I found a brilliant blog post on Tom Anthony’s site that shows you how to use a few JavaScript functions to center the map on a postcode and then place a marker there as well. After following the tutorial I got the map in place but I wanted to extend it so that as the page loads it automatically centers on the postcode and places the marker there. […]
Tom said,
March 21, 2007 @ 5:20 pm
Hi Alan,
My preferred method for gathering up further information about map markers (for displaying in the info window or whatnot) is to store the informatino in an XML file, then fetch it with an AJAX connection.
You can then parse the XML with the built in DOM functions of Javascript, and grab your information.
I’m going to try to finishing up my caching tutorial today, and then if I have time this coming week I’ll make a tutorial showing what I mean above.
Chris,
Glad to hear it, thanks for the feedback!
Caching Google Maps Geocoder Results | Tom Anthony said,
March 21, 2007 @ 8:46 pm
[…] After my post Geocoding UK Postcodes with Google Maps API I’ve had a few people contact me about caching geocoding results back to a server, for subsequent pages. […]
Tom said,
March 21, 2007 @ 11:23 pm
The caching tutorial is up now; I hope that it answers your questions.
Harish said,
March 22, 2007 @ 10:36 am
Hi Tom,
I need to pass the raw UK address (not formatted), Could you please help me out on this??
Harish
Online Marketing Blog » Blog Archive » Geocoding Postcodes shouldn’t be hard… said,
March 22, 2007 @ 1:50 pm
[…] Luckily there’s more than one way around the problem, and you can geocode postcodes for free (in one case using more than one Google API). Just look at the list of approaches and tutorials on Google’s own resource page for external geocoding tools. I rather like this method, as it’s relatively simple to code up… […]
Tony Dillon said,
March 23, 2007 @ 12:59 pm
Hi Tom,
Thanks very much for a great tutorial. Got it up and running in only a few minutes, and it’s saved us a great deal of time, effort and money pursuing alternates. I’ll be extending your code to provide “find your nearest” functionality shortly, and may post the amended code if there’s demand.
Thanks again for a great tutorial. Keep up the good work.
Tony.
Xin said,
March 24, 2007 @ 5:09 am
Tony,
Please do. I’d like to see how you implement this.
I am using Tom’s technique with Ruby on Rails. The plugin I’m using for ‘find your nearest’ functionality is http://geokit.rubyforge.org/.
I’m currently writing up a tutorial on this. I’ll trackback to this post once it is done.
Xin
Tom said,
March 24, 2007 @ 5:37 am
Tony,
That sounds great. I’ve had no call for this yet, but a client of mine may need it soon. I have a couple of ideas for how the algorithm might work, but would love to see how you would implement it!
Xin,
Please do! It would be a great resource!
1000MileJourney » UK Geocoding in Rails said,
March 24, 2007 @ 7:18 am
[…] Tom Anthony’s has written an excellent article called Geocoding UK Postcodes with Google AJAX Search API. […]
Dave said,
March 25, 2007 @ 5:36 pm
Is any one else having problems getting the ‘Place Marker’ function to work.
Tried this several times and im getting nothing.
Sri said,
March 27, 2007 @ 6:32 am
how to show marker according to country names in .net
rob munro said,
March 27, 2007 @ 12:50 pm
Does this usage abide by royal mail copyright conditions? i.e. can i use it on a corporate intra-net? I guess this depends on weather google have paid for Royal Mails stupid copyright on postcodes latLongs.
do you know anything about this? or have any links to info on it?
David said,
March 27, 2007 @ 10:18 pm
Rob
I think the answer is no.
Read the Google MapAPI Terms and you will see that this sort of thing is not allowed - although you did say intranet and therefore who would know?
You need to check this out for yourself and make a value decision for yourself.
Tom - how about a contact me link on your site?
Eric said,
March 28, 2007 @ 12:56 pm
Rob
I think you need to check out the terms and conditions on Google - be carefull not to just look at the terms for Google Maps - they have to be taken in conjunction with the Terms for Goole its self and as this takes advantage of the AJAX search API you need these Terms as well - thre is some repetition BUT they all have something unique - there is also 3rd party considerations to be taken into account as well - I cat remember who all the parties are but for example NAVTEC and Yell have writes to do with their data…
I see you want it for an Intranet - but as you are then pulling in others info you need to be carefull.
Sorry not to be more supportive but every sitation is different and it depends on what you are trying to do - and the results of this action.
Tom said,
March 28, 2007 @ 1:59 pm
David-
Contact link is a good idea; I’m actually working on a complete site, but a so busy making other peoples websites, I haven’t time for my own!
Hopefully it will be up soon, and I’ll have time to write some more tutorials too.
Rob,
I agree with David & Eric, tread carefully. Though your situation is unique, so who knows. Let us know if you find out for sure.
Eric said,
March 29, 2007 @ 10:09 pm
I found this:
www.google.com/intl/en_ALL/help/terms_maps.html
which states
\”Also, you may not use Google Maps in a manner which gives you or any other person access to mass downloads or bulk feeds of numerical latitude and longitude coordinates.\”
might be relevant to some around here….
Dave said,
March 31, 2007 @ 10:39 am
Im still getting the same problem that the PalcceMarkerAtPoint function is not placing a marker.
Re-entered the code several times and re-downloaded the gmap.js file - same thing.
Cleared the cache.. etc etc. Nothing
Any clues what i am doing wrong?
d said,
April 3, 2007 @ 12:08 pm
Thanks for this useful stuff
BTW In step 1, the two google keys appear to be the same
hmnm said,
April 4, 2007 @ 11:07 am
hi, i want search by house number and postcode (like http://www.skiclub.co.uk/skitv/subscription/subscribe.asp ), can you help me?
Prashant Shah said,
April 10, 2007 @ 4:32 pm
Hi Tom,
Thanks for sharing this code with us. I am trying to use your code, ofcourse after replcaing the key, but the map doesn’t load for me. There is no such error but the map doesn’t get displayed on the page. Any idea if there is more to change in your code apart from changing the key? Also can we use this by passing address instead of postcode?
Thanks
Prashant
malcan said,
April 12, 2007 @ 1:17 am
Hi Tom,
I have a couple of sites based on the chauffeur industry and have been looking for a instant quoting system.
I have a simple system at present which is not accurate!
I have got a backend mileage calculator which I can set different rates for pence per mile which is fine.
What I need is an accurate A to B with the correct mileage and take this value and put into my calculator to get a Price.
I recently installed google maps and let people enter in the directions and then ask the customers to enter the mileage shown on the google results and enter the mileage into the calculator to get a price.
It would be nice to have something similar to this http://www.ecourier.co.uk .
I am just playing with a joomla site at the moment and am learning how it all works so many add ons and modules.
Is there a way of extracting the google mileage and to place it into a calculator to get a instant quote or do you have you any idea’s how this can be done!
Any advice would be great
Malcan
links for 2007-04-12 - Bomb.org.uk said,
April 12, 2007 @ 7:26 pm
[…] Geocoding UK Postcodes with Google Map API | Tom Anthony (tags: geocoding google googlemaps postcode AJAX map api JavaScript code webdev) […]
malcan said,
April 13, 2007 @ 7:03 am
Sorry Tom Put in wrong url
http://www.ecourier.co.uk/eCourierISAPI.dll?quote&id=2555232-37895456
Regards
Malcan
Dan said,
April 19, 2007 @ 7:40 pm
We have tested the code as above but problems with UK data - the response we get is:
-
-
England
-
603geocode
I’m not sure if things have changed since this message was posted?
Any help would be appreciated?
Tks
Dan
photomorgana said,
April 19, 2007 @ 11:09 pm
Hello Tom.
Very good tutorial about UK postcodes. I’m looking for something different. I’d like to have a possibility to read a town or a postcode from the marker placing on the map (I mean latitude and longitude point = town or village). Thanks in advance for an advice.
Raha said,
April 20, 2007 @ 5:01 pm
Its working for me thank you very much. its really charming. Do you know if we can use this in a business website where everybody can access it ?
And also I have one more question. Lets say some one put a post code. Is it possible to recieve a string of street address instead of Long and lat ? if yes where can I find this method ? or what is it ?
Thank you again for sharing this. Lovely.
shinji said,
April 24, 2007 @ 8:36 am
hi Tom,
i had some error while try this code, the error is
GlocalSearch is not defined
can you help me to solve this problem???
thanks.
andrew said,
April 25, 2007 @ 3:46 am
hi, Tom
i had trouble with your javascript code, when i try it in my browser the error says that GlocalSearch is undefined. is it Google AJAX Search API only can used on server in UK??? can you help me for resolve this problems???
Tom said,
April 26, 2007 @ 6:12 pm
Sorry for the slow reply - I’ve been on holiday.
Malcan - I’m not sure. did you get this working? If not it is maybe something that would be worthwhile writing an article about?
Dan - Did you get your problem worked out?
photomorgana - So you’d like to have a map, place a pin in it, and then have a report on what postcode/town the pin is in? Have I got the right idea?
Raha - Yes, using it on a business website is fine, as long as you aren’t charging people to use the map. (per Googles TOS). If you are looking to extract the address information, I’m sure it can be done somehow, but am not 100%. If I get a chance I’ll have a tinker about and see.
shinji - Are you including both the Google script tags *before* including your own Javascript?
andrew - Looks like you are having the same problem as shinji. Do you have an example URL I can take a peak at?
ipunk said,
May 1, 2007 @ 7:27 am
hi, tom!
i was used your script for getting uk postcode in the form. but if the form is submitted the value of latitude & longitude isn’t followed posted. can u help me for this problem?
the script look like :
var a;
var b;
function retValue(){
usePointFromPostcode(document.getElementById(’postcode’).value,
function (point) {
this.a = point.lat();
this.b = point.lng();
}
);
document.form1.long.value = this.b;
document.form1.lat.value = this.a
}
mrben said,
May 1, 2007 @ 4:31 pm
Prashant Shah - make sure that you’ve got your in there, and I found it helped to include width and height in a style.
Tom - 1 question for you - I’m putting my map on a dynamically generated page, and so I don’t want a form, I just want it to drop in the Postcode manually. But I’m having problems getting it to centre and place the marker, perhaps because it’s trying to run the functions before the map is loaded? Any thoughts as to the best way to achieve this?
Tom said,
May 2, 2007 @ 4:39 pm
ipunk-
Move your:
document.form1.long.value = this.b;
document.form1.lat.value = this.a;
Statements inside the callback function (replacing the 2
this.a = point.lat();lines), and see what happens.Good luck
Tom said,
May 2, 2007 @ 4:53 pm
mrben-
There is a couple of methods you could use. The key is the
addLoadEventfunction, which tells Javascript to queue up a function to run after everything is done loading.One method might be to write a
<script>tag into your page (at any point after the<scripttags already there) which has a function to act on the postcode, which is written by your server side script (I’ve used PHP as an example):<script type="text/javascript">
function doStuff()
{
var myPostCode = <?php echo $postcode; ?>;
usePointFromPostcode(myPostCode, placeMarkerAtPoint);
}
addLoadEvent(doStuff)
</script>
The addLoadEvent ensures this is run after everything else is done, and so it should work fine.
Let me know how you get on!
mrben said,
May 3, 2007 @ 8:52 am
OK - I’m making progress. I was almost there - was using window.onDomReady rather than addLoadEvent.
One of the strange side effects was an inability to both place a marker _and_ centre it - it seemed to only take the last argument as the one it performed. So I wrote an additional function into your gmap.js which does both.
Thanks so much for your help
Tom said,
May 3, 2007 @ 1:38 pm
mrben-
Depending on the implementation of
onDomReadyyou are using, you might be able to right anaddDomReadyEventfunction, which works almost identically toaddLoadEventand allows you to queue up several events to happen.Your solution works though, and is probably the more efficient way to do it if you always want to center the map and place a marker at once.
Nice work.
freshness said,
May 3, 2007 @ 3:12 pm
Hi Tom,
This works extremely well, thank you for your efforts. I was wondering if I could pick your brains?
I have a page where PHP spits out DIV tags with unique ID’s (formed from the DB primary key) and attributes for postcode and name of company.
I then use jQuery to parse all the divs in the page which have the attribute showOnPage=”true”, get the postcode attribute and add a marker to the map using your code
However! What I really want to do is add an Info Window to each marker, but I’m struggling with the code. So far, my code will create all the markers and info windows, but the content of the info windows are all the same!
Any ideas on how you could adapt your code to produce info windows?
Thanks in advance, freshness
freshness said,
May 3, 2007 @ 3:13 pm
Sorry, the attribute above should have been ’showOnMap=”true”‘. My bad
Steve said,
May 4, 2007 @ 11:33 am
I’m trying to do exactly the same thing as freshness and having exactly the same problem! Tried modifying the usePointFromPostcode function to take optional parameters with information about each marker that is then passed into the placeMarkerAtPoint function:
usePointFromPostcode(postcode, callbackFunction, optParam)
But with different parameters being passed in I’m finding that the callback function only ever sees the last optional parameter value. But the postcodes are different! I guess this is to do with the way callbacks work and although I’ve got a vague understanding of it got to admit I don’t understand fully. Any chance of a bit of advice on this Tom? Your great work so far on this is really appreciated!
Cheers,
Steve
Tom said,
May 4, 2007 @ 2:56 pm
freshness, Steve,
Try moving the line:
var localSearch = new GlocalSearch();Inside the
usePointFromPostcode()function. This will then create a seperate instance for each search, so the variables won’t get mangled.You can then pass your extra data as an additional parameter to the
usePointFromPostcode()function, which in turn passes it on the callback function. Alternatively, inusePointFromPostcode(), you can tack your extra info inside thepointyou create from the results. In which case the function definition would look like:function usePointFromPostcode(postcode, callbackFunction, infoText)And right after you create
pointyou would add this line:point.infoText = infoText;Now, any functions which use
pointcan access the associated infoText (to create an info window, use as a tooltip or whatever you want).I hope that helps!
Vee said,
May 4, 2007 @ 6:35 pm
Tom,
I have an access database for London, the user hits an address field button, Access then loads googlemaps and goes to the postcode for the address field. But it’s not always accurate.
I would like to be able to get the Geocodes for the addresses in my database, (I have about 4,000) do you know where/what the best way to go about this would be. I don’t want to use this on a website and I don’t really understand how to code ASP/Java/Ajax, etc.
freshness said,
May 5, 2007 @ 11:25 pm
Hi Tom,
Wow, who would have thought it would be that simple! As you instructed, I moved the variable declaration inside usePointFromPostcode() and bingo - my info windows are no longer muddled! Thank you so much, great work.
Tom said,
May 6, 2007 @ 12:20 am
freshness –
No problem - glad you got it all working
Vee –
I’m a little unsure of what you want to do. You *do* want to send people from your Access database to Google maps, right? Or is that a temporary solution? Bombard me with the details!
Vee said,
May 8, 2007 @ 11:47 am
Tom,
I have a fixed number of routes (think delivery vehicles). The routes have waypoints along the way. Here is an example.
Waypoint Postcode:
South St. SE1
Gill St. SE1
Bridge Road SE1
Marshal Road EC1
Great Street EC1
Main Roundabout EC2
At the moment, My database will allow a user to click on any waypoint in any sequence, and will then compile a parsed search list of the addresses for GoogleMaps. When a user hits the ‘Do Route’ button, the program switches to googlemaps, sends the parsed list, and Google Maps then runs the route. It works quite well for streets/places it knows, but when GM encounters places like ‘Main Roundabout’ like the last item in my sample list, GM just falls over. So I figured the best solution is not to rely on postcodes, but use LAT/LON Geo co-oridnates instead.
The problem is that I have a very large table of Addresses and places already in postcode format, so I need a way of translating the addresses/postcodes into a Lat/Lon geo format using an automated method. This is purely for the MS Access internal data records. In other words it is used standalone and is not going to be a website/or backend for a website. (I am not web/http savvy.)
does this make better sense now?
regards
Vee
Steve said,
May 9, 2007 @ 11:11 am
Just a quick note to say a big thanks for your May 4, 2007 @ 2:56 pm post Tom. For some reason just moving the GlocalSearch didn’t fix it with my code but putting the HTML in the point’s InfoText did. Cheers for the fix, you seem to be a man in demand at the moment!
Best regards, Steve
Huw said,
May 16, 2007 @ 5:39 pm
Great stuff! Really helpful & easy to set up - thanks. Configuring it further is still hard for a noob though! Would anyone be able to help http://googlemapsforum.com/showthread.php?t=40 ?
bernard said,
May 17, 2007 @ 7:10 pm
:) hi tom thanks for the tips
Andy said,
May 22, 2007 @ 2:18 pm
Simply amazing. Thanks for this.
scott said,
May 26, 2007 @ 6:05 pm
hi tom
Thanks for this code.
However, after replacing the keys, the map doesn’t load.
No map, no error messages.
really would appreciate some help - am I doing something really stoopid?
thanks
Scott
scott said,
May 27, 2007 @ 11:11 am
OK - found the problem.
The map div requires size parms e.g:
all is right with the World ..
But, can you help me … I need to grab the lat and lng values to store in a db
- any ideas??
Maxine said,
June 4, 2007 @ 12:08 pm
Hi,
2 qestions - there still appears to be a minor ‘innacuracy’ with the pinpoint locations for UK postcodes (e.g. if I enter a known address I can see that the marker is 100 yards down the road from where it should be…
and secondly.. the map always opens up in ’satellite mode’ - how do i set this so it always goes to ‘map’ view?
regards
Maxine
Avy said,
June 4, 2007 @ 1:20 pm
Thanks dude, I must say you saved my day. I’ve been struggling with UK geocoding but never got a way around it. This solution made more sense than any other solution (obviously a free one).
Cheers,
Avy
Tom said,
June 4, 2007 @ 3:59 pm
scott –
Take a look at the post following this one - it explains exactly what you are after!
Maxine –
Remember this is using postcode only, not a complete address. Postcodes aren’t unique to a single address, but cover a number of addresses. This is the ‘innacuracy’ you are seeing.
As for changing the view being used, in the mapLoad() function see this line:
map.setCenter(new GLatLng(54.622978,-2.592773), 5, G_HYBRID_MAP);
You can change G_HYBRID_MAP to G_NORMAL_MAP or G_SATELLITE_MAP depending what you want.
Avy –
You are welcome.
Ivan said,
June 6, 2007 @ 2:46 pm
Thanks a lot, you’ve saved my time
mani said,
June 7, 2007 @ 12:54 pm
hi,
i am using the same script what you gave below and grap the key using above mentioned url.but it shows the ‘Glocalsearch is undefined’ please help me
this is my key
I am not passing any extra argument to this function
function usePointFromPostcode(postcode, callbackFunction)
Please help me
Thanks
mani
Stew said,
June 7, 2007 @ 9:27 pm
Hi,
I am real problems gettin this to work
I tried the soucre from this page http://www.pets4homes.co.uk/pets4homes/home.nsf/TestMap?openForm&PostCode=bb24jh
and it works fine so I guess my keys/webspace etc are ok
All I get when I try Toms code is buttons but no map?
Any help would be great.
Thanks
Matt said,
June 11, 2007 @ 4:20 pm
Hi,
I’m having the same problem as above. The page loads with the buttons (map, satellite, hybrid, and the pan zoom buttons), but no actual map… just a blank white background.
Tom said,
June 11, 2007 @ 4:29 pm
mani - have you included both the script tags for Google at the top of your page?
Stew, Matt — either of you have an example URL you can post or email to me?
Have you tried copying the files from the demo page:
http://www.tomanthony.co.uk/demo/geocode_uk_postcode/
KD said,
June 14, 2007 @ 5:16 pm
Anyone know whether it’s possible to do the reverse of what’s been discussed above - notably click on the map and get a UK postcode back for this location?
Given there are lots of people who don’t know their postcode and far more who want to use tools that are driven by postcodes but don’t know them for abitrary areas, I consider this is just as vital.
Apologies if this has been discussed before - am new to this
Thanks.
KD
onefix said,
June 14, 2007 @ 7:25 pm
Thanks excellent info. only problem I had was not being able to display the maps correctly but as soon as I realized I needed a styles.css and nofollow.css document worked like a charm.
I am trying to setup the following option, when somebody clicks on a name of a town or village a map is displayed? Unsure how to go about this?? anybody have any ideas or does somebody want to earn some money setting up a script etc for me????
help (at) onefix.co.uk
thanks again seems very accurate postcode finder for postcodes I have tried
onefix : sorry here is site :http://www.cornish.co.uk/cornwall-towns-villages.php said,
June 14, 2007 @ 7:26 pm
sorry here is site :http://www.cornish.co.uk/cornwall-towns-villages.php
ian said,
June 15, 2007 @ 9:24 am
Was thinking about adding this feature for some time but thought it was to much grief up until now.
This is a great piece of work.
Thanks for the time and effort you put into this Tom.
I followed the above guide and it was working on first go, brilliant.
I do have one query however.
If i already have the postcodes and they are being passed to the page via php where would i put these so i could do away with the input fields ? i could echo them into a hidden field or something. but im not sure how to pass the postcode to the gmap.js.
I will figure it out soon. Its a great piece of work. thanks Ian.
Tom said,
June 17, 2007 @ 12:13 am
Hi Tom
Great tutorial!
As UK postcodes aren’t totally accurate for pinpointing a specific building, I have made a modification to allow draggable markers. A listener will then output the new lat and lng to a form field.
http://www.klubbedout.com/ajaxgeo.php
However, once a marker is moved the map won’t re-centre on the new point, the lat and lng (when clicking on show lat/lng) is still the one from the original lookup, also my form field isn’t filled until the marker is dragged.
Can you offer any assistance?
:-)
Tom said,
June 21, 2007 @ 6:04 pm
Hi Tom,
A couple of issues I can see:
- You allow multiple markers, so if I am centering the map, which marker should I use?
- The center map button still calls usePointFromPostcode(), so it ignores your updates completely. What you need is a wrapper function on the button, so if there is no marker, it calls usePointFromPostcode() as before, but if there is a marker, it centers there instead.
I hope this gives you a nudge in the right direction.
Kiran said,
June 23, 2007 @ 11:59 am
Hi Tom,
I have followed your tutorial, its gr8. but i couldnt see the Map over there. i have place two Keys and added the GMAP.js in the same path. but i couldnt see the Map as out put. Please have a look and let me know what happened.. Its really URGENT for me.. Please help me Tom.
THanks a lot.
Kiran
S K said,
June 23, 2007 @ 4:43 pm
Hi Tom
Excellent tutorial. I have teh same problem as other ie I’m having the same problem as above. The page loads with the buttons (map, satellite, hybrid, and the pan zoom buttons), but no actual map… just a blank white background.
I’ve used your sample .js file and input form and an API key that works on other files in the directory
Any help welcome
Tom said,
June 23, 2007 @ 6:29 pm
Kiran -
In your html, where you include the map, you are missing an important section of the URL:
http://maps.google.com/maps?file=api&v=2&key=……..
I think that should solve your problem.
S K -
You haven’t copied the styles.css file from the demo page, your map element has not height and width set, you need this CSS:
#map
{
height: 500px;
width: 500px;
}
:)
S K said,
June 23, 2007 @ 10:47 pm
Tom
I gave you the wrong URL. The issue was simply failing to include the styles.css file. Do you know how to set up a facility to click on the map and return the lat, long co-ords.
Kiran said,
June 24, 2007 @ 1:38 pm
Hi Tom,
I have updated file, according to your suggestions but the same effect. It couldnt solve the issue. Could you please look into and letme know..
Thanks in advance
Kiran
Kiran said,
June 24, 2007 @ 1:48 pm
Hey Tom,
Sorry for the Previous Post, It solved my issue.. Thank you so much TOM.
Kiran
Sharron Hibbert said,
June 25, 2007 @ 8:55 pm
Dear Tom
we are currently building a website but have come to a halt re the geocoding, we need something that will provide a return of postcode, street name long/lat co-ords, also whether you can search on postcode info to return a list of available addresses which would then return the long/lat co-ords once selected, we have had some responses but the prices have been too high.
Any suggestions?
kind regards
Sharron Hibbert
alex said,
June 27, 2007 @ 3:27 pm
Hi Tom,
Just want to say what an awesome tutorial! Exactly what I’ve been looking for, and I mean exactly!
Reading through the above posts, I arent sure if someone submitted this exact question and may have worded it slightly different, however:
Your geocoding form adds multiple markers on the map below, one marker for each postcode entered, which then stay on the map until you leave the page. How could I store the postcode entered into a list (txt, MySQL, etc) and then recall this list the next time the page is loaded so that the markers are still available? So effectively you are leaving a mark on a map for anyone else to see.
I have searched high and low to be able to do this and unfortunately to no avail (baring in mind I am a novice at server-side/scripting etc).
Will love ya forever!
Thanks
Alex
alex said,
June 27, 2007 @ 4:18 pm
Ah, I think the caching tutorial answers my question!
However, do you know of a way to make this particular type of map work with sites such as MySpace seeing as MySpace dont allow Javascript (or even PHP)!
Essentially what I would like is a simple form for MySpace users to enter their postcode into a textbox, the postcode would be passed off to my database, then a marker would appear on the localised map along with all the other markers of submitted postcodes (by refresh or something). I know the other MySpace map providers use work arounds via logging IP address and displaying on a map, but this is not accurate enough for my needs of a local based profile that doesnt require worldwide tracking!
This may be slightly off topic to the original map tutorial but any advice in the right direction would be deeply appreciated!
Thanks
Alex
Kiran said,
June 27, 2007 @ 7:21 pm
Hi Tom,
I have an requirement. 1) if need to pass the address in a text field, if i click on placemark or enter it should directly show the location in the Map. Could you please provide me the code or can u guide me on how to do this. one more option for me is 2) a link will there and some address will be associated to the link. If i click on the Link, this should show the location in the Map. Please let me know how to approach to this method.
Thanks in advance
Kiran jagarlapudi
Tom said,
June 27, 2007 @ 7:24 pm
Sharon -
I’m don’t think that Google Maps could do this. With Google Local Search you could get a list of addresses for businesses in the area of a postcode, but not all the addresses. So depending, that may be of some help to you. Otherwise have you tried http://www.postcodeanywhere.co.uk/ ?
Alex -
MySpace won’t allow any Javascript or iFrames, so the only solution you are left with is Flash. I think you could make a Flash form that used LoadVars to send the Postcode entered to your server, which would reply with a list of all Postcodes entered. Then use this very cool solution (which I only just found - am going to check it out more) to provide Google Maps via Flash:
http://www.afcomponents.com/components/g_map/
I think that would almost give you an entirely flash based solution, but I believe it would fall down as you couldn’t access Google Local search and thus couldn’t geocode the postcodes. You’d need to do that server side with one of the expensive databases.
Kiran said,
June 27, 2007 @ 7:57 pm
Hi Tom,
Could you please give me an update on this asap.
I have an requirement. 1) if need to pass the address in a text field, if i click on placemark or enter it should directly show the location in the Map. Could you please provide me the code or can u guide me on how to do this. one more option for me is 2) a link will there and some address will be associated to the link. If i click on the Link, this should show the location in the Map. Please let me know how to approach to this method.
Thanks in Advance
Kiran
Jitesh said,
June 28, 2007 @ 1:11 pm
In your application if i want to print the latitude and longitude on the website rather than giving an alert message…how can i do that…please reply when u get time
regards
jitesh
Stew said,
June 29, 2007 @ 6:58 pm
Hi Tom,
Got my map working (no style css DOH)
Anyhow, I am desperately trying to ‘auto load’ a postcode (stored in a variable) on loading the page.
Any ideas????
Thanks
Stew said,
June 29, 2007 @ 7:43 pm
Hi again,
Managed to do the varible in a postcode.
If i place a marker then center the map, i lose the marker.
In reverse, if a center the map, then place a marker, the map zooms out to some default value. Can I set this default value??
Thanks
Pythoneer » Finding UK places via Google maps API said,
July 1, 2007 @ 7:02 pm
[…] Your royal highness doesn’t like you finding royal places in the royal UK *cough*. How dumb is that. Therefore use a way around, as described very extensively on Tom Anthony’s blog in the article Geocoding UK Postcodes with Google Map API. Thanks Tom! […]
Izzy said,
July 2, 2007 @ 7:34 pm
Hi,
Great Tutorial, Didn’t have any problems at all.
I have however tryed to use the returned point var to use in the CDirections of the Google Maps API, and for some reason all i get returned is Null for the search.results , Is there anything I am missing?
Think this would be a great addition to the functionality of ur gmap.js file.
Thanks
William said,
July 3, 2007 @ 2:58 am
Hi Tom, thank you for the wonder script. I am now able to retreive many UK addresses and display them on my map.
Matt said,
July 3, 2007 @ 10:10 pm
I think that your solution is obsolete. The Google Maps API offers a way to geocode any UK ADDRESS or UK POSTCODE for free using the GClientGeocoder class in the API! You can pass it any international address, such as “Buckingham Palace, London” or just a postcode and it will return you detailed geocoded information.
FURTHER, Google offers a FREE UK/INTERNATIONAL HTTP geocoding service which returns, via plain old HTTP a nicely formatted XML structure of latitude/longitude and address information for any postcode/zip/address.
It is all here: http://www.google.com/apis/maps/documentation/index.html#Geocoding_Examples
And if you don’t believe the free http geocoding interface, try this address to geocode buckingham palace:
http://maps.google.com/maps/geo?q=SW1A1AA&output=xml&key=ENTER_YOUR_API_KEY
Has everyone else missed this??
Tom said,
July 4, 2007 @ 11:43 am
Hi Matt,
I can assure you that this amount of people have not just missed Google’s geocoder!
However, until recently the geocoder was disabled for both the UK (and others, such as China), and would simply return error 603 (G_GEO_UNAVAILABLE_ADDRESS) and wouldn’t work (as is very well documented by a number of sutes).
FURTHER, I discuss accessing the Google Geocoder via HTTP in the post following this one. It too would return the same error.
I have checked it periodically, and it has always had the same error (confirmed less than a month ago, see for example here). However, it seems you have stumbled across it after they have updated it.
I’m not sure if this update is intentional and can’t find any news about it. If Google had brokered a deal, I’d expect there to be some news about it. I am going to watch this over the next few days, and if it continues to work, then I’ll be sure to update the tutorial.
rickh said,
July 6, 2007 @ 10:38 am
hi tom,
this demo works fine in firefox2 but when i use ie7 i get no marker or anything. even on your demo page when i use the zoom feature using ie7 i just get back a blank map. ive even reset all manufacture setting in ie7 to no avail.
any ideas?
rickh
Mike said,
July 6, 2007 @ 1:01 pm
This is great and easy to use. However I am trying to automate this so when a page is loaded a postcode is grabbed from a querystring and then added to the function e.g usePointFromPostcode(’postcode’, setCenterToPoint). It works great in IE but in firefox it is not working. The error I get is map has no properties. The map obviously isn’t loading in time. I have tried to set timers etc.. but can’t find a way around it. Does anyone have any ideas
Mercurythread said,
July 16, 2007 @ 1:14 pm
Cheers. Have been pulling my hair out with this Postcode thing. Am just away to hack some of the code to make this do what I need it to.
Thanks again
Michael
Chris said,
July 19, 2007 @ 9:46 am
Tom,
This looks like a wonderful resource, which I’m just trying to get my head around. However, is it superceded by Google’s July 6 announcement ‘UK Geocoding Now Available in the Maps API’, or is it complementary?
See http://googlemapsapi.blogspot.com/2007/07/uk-geocoding-now-available-in-maps-api.html
Thanks, Chris
existem.blog » Using the Google Maps API to add maps to your site said,
July 20, 2007 @ 11:05 am
[…] Please visit this tutorial by Tom Anthony which details how to do this. […]
Gareth said,
July 22, 2007 @ 9:42 pm
Hi, fantastic example!
Just a quick question if you know the answer. I have a database written in MySQL and want to display a map based on the postcode in the db from a recordset. Rather than giving the user the ability to input via a form. Any ideas?
Thanks
Gareth
Mark said,
July 23, 2007 @ 1:09 pm
Thanks for this, I have implemented it on my pets for sale site pets4homes at http://www.pets4homes.co.uk
Craig said,
July 24, 2007 @ 12:59 pm
Hi Tom,
Great information and tutorial.. I’m taking advantage of the help you’ve been giving people in the comments to see if I can pick your brains….
I would like to implement a system where I can check if a route that someone enters on Google Maps passes a certain point (and judge proximity to that point).. I guess it could be done by getting the long/lat of each stage in the route directions and checking that against the point through a loop, but just wondered if there was already a function to check this? Any thoughts would be much appreciated..
Regards..
Alex said,
August 7, 2007 @ 3:48 pm
Hi.
What does the line localSearch.execute(postcode + “, UK”); do?
Hasn’t the point already been created by this stage?
Thanks
Gary F said,
August 14, 2007 @ 2:13 am
Chris, Google’s UK geocoding for addresses is pretty good but I found that it doesn’t work if the address you send it has a building name rather than a number. .e.g. “12 Smith Street, SomeTown, UK” works, but “Cherry Cottage, SomeTown, UK” does not.
Google’s address geocoding can sometimes be more accurate than postcode geocoding. I’d say it’s 50/50 meaning that half the time Google’s address geocoding is better and vice versa. Tom’s technique is brilliant though and it will work every time, unlike Google’s address geocoding. The perfect solution is to use address geocoding and if you get an error from the result try again using postcode geocoding. For large buildings (office blocks, flats, etc) with their own unique postcode you will find that Tom’s postcode geocoding is always bang on the mark.
Tom @ Fivebyte - I love your idea for getting users to update their coordinates using a draggable marker. I’m surprised no one has commented on your work (http://www.klubbedout.com/ajaxgeo.php) it’s really neat.
Gary F said,
August 16, 2007 @ 2:14 pm
My comment was deleted. Here goes again…
Chris, Tom’s clever postcode geocoder is still very important because the official gmaps geocoder for addresses doesn’t work if the address you supply has a building name. It requires a building number which is fine for many residential addresses except some blocks of flats or “Cherry Cottage” etc. The accuracy of Google’s address geocoding is not that accurate surprisingly. e.g. my address is 20m out.
Gary.
Chris Owen said,
August 21, 2007 @ 5:57 pm
Hi Tom/All,
I have had the same problem as others with GlocalSearch not being set. I have noticed however that my url to Google AJAX Search API is pointing to v2 where as yours is pointing to v1. How can I get v1, or do you have a solution using this API?
Peter said,
August 21, 2007 @ 10:34 pm
Tom, congratulations on this. I had a project that wnet on the back burner last year because there was no free way to convert postcodes to locations in the UK - now I can pick it up again
@Tom and @Gary F: now there’s Google local search, Google Maps geocoding and Google Ajax Search, can you shed any light on the difference? I was surprised Tom’s example above didn’t geocode an address - I thought the Ajax search tool would do this? Is there a break-down anywhere of what each is capable of?
Peter
Peter said,
August 21, 2007 @ 10:47 pm
@Gary F: To clarify my question, you say “Google’s UK geocoding for addresses is pretty good but I found that it doesn’t work if the address you send it has a building name rather than a number.”
What approach do you use for such buildings, if you don’t kow the postcode?
Tim said,
August 22, 2007 @ 11:29 am
Thanks - a great tutorial.
Have you come across the little problem of the geocoder treating postcodes with “st”, “nd”, “rd” or “th” as if they are addresses with 1st, 2nd, 3rd, or 4th? Working this out now…
Tim
Set best view for markers said,
August 25, 2007 @ 4:09 am
I have some markers on the map. I want to set best view for these markers (we can see all of these markers with the best view). Please show me the way to implement it.
Thanks,
Manjit dahiya said,
August 28, 2007 @ 7:06 am
Hi Expert
can anybody give me code in server side scrtip like C# or vb.net
Thanks in advance
29 Coolest Custom Search Apps Built on Google said,
September 1, 2007 @ 4:29 am
[…] Geocoding UK Postcodes – Step by step instructions for using the Google AJAX Search API to geocode UK postcodes so you can create Google Maps displays. […]
links for 2007-09-03 said,
September 4, 2007 @ 12:19 am
[…] Geocoding UK Postcodes with Google Map API | Tom Anthony (tags: geocoding googlemaps) […]
29 款基于 Google 的超酷自定义搜索应用 said,
September 4, 2007 @ 12:25 pm
[…] - Hoctro’s Place- Search Mashups- Google AJAX Search Plugin【自定义搜索引擎】- Atlas- Babelplex- Windsports- FeedSearch.net- FoxyTunes- Librarian Chick- Mashed Tickets- Oddflower- Searchthebeat- Stylehive- The Recycling Center- Tips-Search- SuccessForce- ccFotos- UniCommunity【电子地图的 Mashup】- The unofficial database- TrekMap- Two Minute World- Wines and Times- Geocoding UK Postcodes- Generate a Local Map Search【Google】- Google AJAX Search for iPhone- Google Map Search […]
myspace hide comments box codes said,
September 18, 2007 @ 9:13 am
myspace hide comments box codes…
myspace hide comments box codes…
Pragnesh said,
September 18, 2007 @ 1:30 pm
hi ,
i am trying this code in asp.ner master pages.
but its giving object not found error.
can u hel me…??
thanks
Praveen said,
September 20, 2007 @ 12:24 pm
Hi Tom,
Thanks for your code, it really helped me a lot.
Now what am wondering about is, is there a way to find distance for example say the user searches for hotels in Glasgow and you get list of hotels sorted according to its distance from Glasgow.
I dont think i made myself clear there,
I have a requirement for search functionality which takes user’s input either in form of postcode or name of the place. Then that info is used to find the hotels in that locality. If my client doesnt own any hotels in that locality, instead of saying there are no hotels around I would like to show a list of hotels nearby the user’s query. Also it would be better to show distance from the queried place to all hotels nearby.
To my understanding this needs the GeoCoding of the place queried and then display the place in the map then with “calculated distance” the nearby hotels could be sorted out.
But am not sure whether am in the right track, please let me know if that is possible or if there is another way am open to suggestions.
And one last question — is there a way to still calculate the distance in javascript disabled browsers?. This seems quite achievable as google maps do this in some strange means(atleast to me). If possible how?
Thats it from me!!
Thanks
Praveen
google mapquest said,
September 23, 2007 @ 2:15 am
google mapquest…
We are exceedingly pleased that you have stumbled onto this web page about yahoo maps….
Dawei said,
September 25, 2007 @ 11:51 am
well, very nice
Lisa said,
September 26, 2007 @ 8:36 am
Hi Tom,
It seems to be a very good solution.
I want to know whether we can use it in a similar manner to use zip codes for other countries like US etc.
Thanks,
Lisa
google » Geocoding UK Postcodes with Google Map API | Tom Anthony said,
October 7, 2007 @ 6:33 pm
[…] gurusblog wrote an interesting post today onHere’s a quick excerptTony […]
Vee said,
October 22, 2007 @ 10:04 pm
Hello Tom,
I was wondering if you could give some pointers on whether it is possible to get googlemaps to output a list of street names and generic postcode (eg: WC1 or NW2) within a given (user defined) distance based on a given postcode.
can it be done?
Dan said,
October 24, 2007 @ 8:35 pm
Hi Tom,
I came across your blog while looking for a solution to a problem I am having with UK Geocoding. I had previously been using the GClientGeocoder from the google maps API to map UK postcodes.
As of yesterday this was working great, however as of today it seems that google is no longer returning results for UK postcodes.
I was just wandering if you were aware of this and if you knew anything about it?
Emil Nenov said,
October 25, 2007 @ 10:08 am
Hi, I found the same that map UK postcodes doesn’t work from yesterday, does anyone knows something about that, is that temporarily or …
Regards,
Emil Nenov
Mike said,
October 25, 2007 @ 10:52 am
I have a directory which gets the postcode from a querystring in .net and geocodes for UK this has also stopped working i keep getting Postcode not found?
Dawei said,
October 26, 2007 @ 12:10 pm
Hi everyone, I got the same problem, it’s seems like google has been changed something on their end, which always returning the post code not found
Eric Johnston said,
October 26, 2007 @ 12:44 pm
It is some kind of bug.
It still works approximately if you omit the last letter of the postcode.
Best regards, Eric.
Ed said,
October 26, 2007 @ 2:42 pm
Hi
Could you show us the inline code (for php) required to implement the functionality to return glat/glon? Google seem to have withdrawn the http request method for quick postcode to glat/glon functionality.
Many thanks!
Luke said,
October 30, 2007 @ 9:27 pm
Hi Tom,
Excellent tutorial, Just wondering if there is any way you can also extract address details? for instance is it possible to;
Enter Uk Postcode->Get the Geocode Coordinates-> use the coordinates to get address details…such as town/County and street if possible?
Paul said,
October 31, 2007 @ 5:49 pm
I have just found you can get some address details using:
var town = localSearch.results[i].streetAddress;
var city = localSearch.results[i].city;
var region = localSearch.results[i].region;
Now the UK postcode geocoding has been stopped i have come back to this to pick them up
Pete said,
November 1, 2007 @ 4:30 pm
You’ve probably noticed that Google Maps have withdrawn their geocoding service for the UK now (within the last week or so).
I have a CSV file containing a complete list of UK postcodes, along with lat/long - around 2.5 million entries in total - if anyone’s interested: http://linuxbox.co.uk/postcode_database.php
Stephen said,
November 2, 2007 @ 12:53 am
Hello.
I keep getting a Javascript error of ‘localSearch has no properties’. Is this related to one of the latest posts by Pete saying Google have withdrawn their geocoding service for the UK?
I noticed your demo page still works and I tried copying and pasting the HTML source and altering the keys to mine but then I get no map.
Any thoughts appreciated, Ste.
Stephen said,
November 2, 2007 @ 1:26 am
Please ignore my last post. For some reason the Javascript generated from Google Maps when requesting a key is slightly different to the Javascript you use, and I was copying and pasting from Google. When I used your exact script source from the demo and just replaced your key with mine it worked fine.
But I do have a second question: How would I default the map to a position depending on a PHP database result. I have tried using the following code in the section:
usePointFromPostcode(”tf3 3at”, setCenterToPoint);
But this doesn’t change anything. I just get the usual view of the UK on load.
Regards,
Dan Player said,
November 5, 2007 @ 5:39 pm
Hi all,
Can anyone tell me how I can use this code to generate directions from a point already marked on the map. I want to be able to have people put their postcode into a box and get directions to the church and hotel for my wedding. Ideally I’d like to be able to have them put their postcode in and then it gives directios to the church then to the hotel in one list. It used to work before Google took away the postcode function. Here’s a link: http://www.moatway.force9.co.uk/budget.html
Thanks in advance.
Dan
raghav said,
November 12, 2007 @ 11:59 am
Hi all,
We are working on mashing up GoogleMap API in one of my project. We are finding difficulties in getting the correct location(lat & long) for UK postcodes. There is a significant difference with the Lat /Lng what I get from Geocoder and the one which I get from Isharemaps. For some of the codes, it is unimaginable. Is there any way to get correct lat & Lng through Geocoders with some Degree corrections on the latitude & Longtitude. Also I am looking @ a webservice instead of maintaining the whole database with me.
I found an article which tells me on getting the Correct Lat & Lng with some delta error corrections. But of late, couldnt find that article.
Pls suggest as I am in the middle of this project.
Dan said,
November 15, 2007 @ 10:54 am
Hi Tom,
Wondering if you can help?
We are looking at trying to get an address finder. So you type in your postcode click search and it returns a list of address that are linked to that postcode, containing the house numbers, street name, town and county.
Just wondering if you have done anything along these lines?
Thanks in advance,
Dan
David Savage said,
November 19, 2007 @ 11:07 pm
Hi Tom, and all
I am using a db query to return a postcode.
The usePointFromPostcode function works to place the marker in the correct place, but the map.setcenter just does not work for me.
map.setCenter(new GLatLng(54.622978,-2.592773), 14);
which obviously is Tom’s default setting (which works) - but how do I center the map around the marker?
I try map.setCenter(new GLatLng(point), 14);
map.setCenter(new GLatLng(marker), 14);
map.setCenter(point, 14);
map.setCenter(marker, 14);
map.setCenter(new GLatLng(resultLat, resultLng), 14);
but nothing works… any ideas?
Thanks
Dave
Jack wang said,
November 21, 2007 @ 8:08 am
To CEO: Brand name of tomanthony
Dear CEO,
We are Shanghai Ujane Information Technology Co., Ltd, a domain name register organization in China. We have something urgent to confirm with your company. Because we formally received an application from a company named Lexiao (Hongkong) Investment company. They are applying to register “tomanthony” and so on as Internet Brand name and CN domain name through us. Now we are in charge of this matter. In order to keep other party from using these domain names, we hope to get your confirmation about these domain names’ registration asap.
I’m looking forward to your prompt reply. You may ask someone in your company who is responsible for intellectual property right matters to contact us,or you can let the trademark office in China contact us directly as soon as possible.
Best Regards
Jack Wang
Tel: +86-21-26505825
Fax: +86-21-54243677
Mobil:+86-15800712491
Skype ID: edison.zhang88
Email: jack.wang@ujane.cn
Kai Jin said,
November 27, 2007 @ 3:52 am
The statement
Jack wang said,
November 21, 2007 @ 8:08 am
To CEO: Brand name of tomanthony
Dear CEO,
We are Shanghai Ujane Information Technology Co., Ltd, a domain name register organization in China. We have something urgent to confirm with your company. Because we formally received an application from a company named Lexiao (Hongkong) Investment company. …
This is a scam.
Middle East B2B said,
November 27, 2007 @ 6:50 am
Nice Example liked it….!
Aaron Whiffin said,
November 30, 2007 @ 4:47 pm
One of the best tutorials I’ve ever read
Many thanks!
colin said,
December 12, 2007 @ 5:26 pm
Hi,
I have just started using the API to pinpoint locations in the local area. I am using the GClientGeocoder() class in order to pin point addresses using the uk postcode. All works fine apart from when I use the point() returned by the getLatLng() method to setCenter() the marker on the map is located at the top left of the map control!! I have tried to center using the GetLatLngBounds() but it still remains in the top left corner of the control.
Any help would be appreciated.
Colin
Adam Liptrot said,
December 12, 2007 @ 11:59 pm
Thanks for this Tom - I’d found the geocoding included in the API to be a bit unreliable for UK postcodes. This solution however is spot on!
Shane said,
December 14, 2007 @ 4:37 pm
Great tutorial. I’ll be using it on a forthcoming site. Many thanks.
Study said,
December 15, 2007 @ 3:27 pm
That’s great example. I think we can also use web API provided at geonames.org. I seen using it on http://postalcode.globefeed.com/UK_Postal_Code.asp
Holiday Rentals said,
December 17, 2007 @ 12:18 pm
Very nice found you on google and answered exactly what I wanted to know. Many thanks.
Anshumaan Bakshi said,
December 24, 2007 @ 1:53 pm
Nice script……..it really works.
Thanks for the help
AB
postal codes said,
December 27, 2007 @ 12:12 pm
Search UK Postal Code (PostCode) of Place & City names in UK (Zip code, Postcode). Postal Code
Rehmanab said,
January 3, 2008 @ 3:07 pm
Thanks..
gareth said,
January 3, 2008 @ 7:15 pm
Just what I’ve been looking for,
Thanks for the effort.
Matthew said,
January 8, 2008 @ 6:58 pm
Fantastic brilliant solution
Property for Sale said,
January 9, 2008 @ 11:40 am
Hi there, I am making a property website and want to add this in my website. When user click on some property details it should show map with that property postcode. How can i pass postcode using php and fit in this script. thanks
Property for Sale said,
January 9, 2008 @ 10:14 pm
I am stuck. Basically dont know much about javascript. I have add all code using my own keys. But page coming is giving error and shows blank. Javascript debugger says localsearch is null.
Also what I have to add in html file. Is there someone can share its html file. I only need to work for one postcode then i will configure for autogenrated postcode values. thanks
kan said,
January 10, 2008 @ 6:30 pm
Hi,
how to get direction if I have got longitude and latitude for two places?
John Savage said,
January 13, 2008 @ 10:25 pm
We are losing our workplace car park in the next few weeks. I am looking for a way to gather postcodes of colleagues at work and pour them into the Google map software in order to display, by way of pins, all employees who want to take part in a car sharing scheme. The idea being that anyone in work can see at a glance, the location of other employees who live nearby… a real good use for this kind of software don’t you think… any ideas anyone.
Nick Gilbert said,
January 15, 2008 @ 4:47 pm
Although google now have a UK geocoding service available, since it’s launch they seem to have massively downgraded the accuracy of the service making it useless for most purposes. See here:
http://googlemapsapi.blogspot.com/2007/07/uk-geocoding-now-available-in-maps-api.html
Try comparing the two systems - the test page on the link above, and the test on *this* page. The method described on this page still seems to return accurate locations, but the Maps API Geocoder seems to return data which is up to a kilometer out.
I wonder why they’ve only degraded the accuracy of ONE of the APIs? Does anybody understand why they’ve done this?
Cars for Sale said,
January 18, 2008 @ 9:05 pm
Good tutorial to learn. I am looking for something similar to implement for one of my site. Will see how it goes.
Vic said,
January 21, 2008 @ 11:35 am
Excellent job Tom - very helpful.
So here is one more thank you to add to your already long list!
Keep up the good work.
Vic
James said,
January 22, 2008 @ 11:28 am
Hi Tom
Great little function, just one thing I am trying to pass info from a db to pop into the popup box for the marker. Just not sure how to pass the info.
Here is the code I am using:
var loc = ‘’;
var car = ‘’;
usePointFromPostcode(loc, placeMarkerAtPoint)
The var car is what I want to be displayed in the popup box next to the marker.
Thanks for help in advance. James
jude said,
January 26, 2008 @ 2:02 pm
looking for a lady from 23 to 48 for serious relationship.i’m 23yr.please contact through my IM judox4u@yahoo.com
Zn Studios said,
February 4, 2008 @ 11:20 am
Hi - I am getting the “map has no properties” error, its driving me mental - anyone have any ideas??
Bluebarnabus said,
February 28, 2008 @ 6:02 pm
My experience of using the Google Maps HTTP Geocoder for UK postcodes is that it is only uses part of the postcode, for example, XX2 1AN, XX2 1BB and XX2 1XY (if such postcodes exist) would all return the same lat/long values.
You might find MultiMaps offering is more accurate.
b.ramprasad said,
March 10, 2008 @ 2:41 pm
i am using google map for showing path for two places using my own points. when i am using this “Geocoding UK Postcodes with Google Map API” i am not able to get the path. waht to do
Rachel said,
March 19, 2008 @ 7:11 pm
I’m trying to get a map of the DL12 postcode area. Will doing what you suggest ge theis for me?
lookup reverse phone number said,
March 20, 2008 @ 7:05 am
lookup reverse phone number…
Which cell reverse phone lookup providers are you relying on? There are dozens of cell reverse services but you want to make sure you’re using the most accurate information available, right?…
angel said,
March 21, 2008 @ 2:54 pm
Hello,
Could you please provide me some guidance regarding how exactly link gets formed
wen you enter any street address in the address bar of web browser?
Does any API do this whole function of locating that place? Actually I am new to all these things, so I dont have much idea about this.
Thank you.
Chandra said,
March 31, 2008 @ 3:07 pm
Hi Tom,
I have found the tutorial to get the marker up very useful especially because I’m not a pro at javascript.
This will give you an idea on what I’m up to.
‘http://mimtech.pepperio.net/contactus.html’
However, I’m not able to get the FROM functionality on my site working.
If you have a look at the URL above, you see a set of hard coded directions to my office. I’m trying to get this to look a bit more dynamic using a FROM - TO functionality of the maps. I have two options of design to do this:
1. To have the map by default as it is just now when the page loads and then giving an option at the bottom to get the use to type in From and To. The submit resulting a list of directions.
2. This option is to have the default map on loading of the page. Below the map there is a From input box. On submit it shows directions from the typed in Address / POSTCODE to my office (This is coded in the script).
I would like to put more emphasis on the use of “PostCode” in the “From” field.
Here is are some bits and pieces of the script:
if (GBrowserIsCompatible())
{
var map = new GMap2(document.getElementById(”map”));
var panel = document.getElementById(”panel”);
var dir = new GDirections(map, panel);
map.setCenter(new GLatLng(55.863741, -4.24171), 14);
var marker = new GMarker(new GLatLng(55.863741, -4.24171));
map.addOverlay(marker);
var html=” ” +
“MIM Technologies Ltd” +
“141 St James Road, G4 0LT” +
“Tel: (0044) 0141 3038239″;
marker.openInfoWindowHtml(html);
map.addControl(new GSmallMapControl());
}
I have tried various other functions but had no success. Can you please help me with this? Any help is appreciated.
Just to add that I’m using a CMS called Pepperio to do the website.
Thanks!
Chandra
Postcode Finder said,
April 4, 2008 @ 1:30 pm
We’ve been giving this a go, but found there was a real lack of a decent free postcode finder in the UK in the first place - which we needed for some data in the first place. So we didn’t have much to work with.
When our planned tool is complete we’ll post here again.
vashisht said,
April 5, 2008 @ 8:51 am
Nice one!!
Can we have some workaround to use this server side so that we can store the GECOCODES in storage file and then display the multiple address at same time on the map.
e.g i have multiple office location in UK, and i need to show them at same time on map.
Robert Krueger said,
April 10, 2008 @ 12:32 am
Hi Tom,
Can you suggest any of these third party services that you referred to?
I have given my programmer your URL to take a look at your Google solution, but I would like a backup in case he sees a problem.
Thanks,
Robert
Joe Turner said,
April 10, 2008 @ 10:54 am
Used this code for generating a map recently to show local stockists of a product. Have a database of postcodes to find the nearest 10 stockists, and then use their postcodes with this code to plot pins on the map.
So using the AJAX function I just ran a loop for all 10 stockists. The pins are plotted fine. All brilliant!
Then tested in the dreaded IE! Some of the bubbles had swapped marker. No idea why at all. I bodged a function to delay the loop for half a second before running the next postcode search and what do you know it works now.
Any ideas?
Anti-Spam said,
April 10, 2008 @ 8:30 pm
Thanks for this. Very clear and understandable, helped me a lot. In fact it helped me get a job.
Tom said,
April 13, 2008 @ 9:38 pm
Thanks, this helped me a lot.
steve said,
April 15, 2008 @ 11:33 am
Thanks Tom - this has inspired me to add a new feature on a website. I would like users to submit a form to a php function that writes a postcode to a text file and then a point is display on a google map on another page. Can you possibly give me any ideas how this might be done.
The idea is that people pledge to help with global warming and their pledge is marked on the map!
Digital Pen said,
April 22, 2008 @ 2:25 pm
Can this method return street level info as well as postcodes from WGS84 coordinates?
Alex Poole said,
April 28, 2008 @ 10:45 am
Excellent & elegant example, thanks. This appears massively more accurate than Google’s much-vaunted “native” geo-coder. I wonder whether they’ve made it innacurate to keep the Royal Mail happy or something. Regardless, this is excellent, cheers again..
Just need to code up a server-side version now
Andy Newby said,
April 30, 2008 @ 10:43 am
Hi,
First of all - thanks for this code - It *almost* does what I’m trying to achieve
Basically, I want to use this - but actually pre-populate it, with a selection of Postcodes.
I’ve tried adding this into the JS:
localSearch.execute(”rh12 3he, UK”);
localSearch.execute(”rh14 9jl, UK”);
…but that doesn’t work.
Anyone got any suggestions? Heres an example of what I’ve tried:
http://www.gossamerlinks.com/testing_geo.html
TIA!
Andy
Nic said,
May 3, 2008 @ 7:11 pm
Does this all mean that its possible to do this:
Have a customer visit a web site and enter thier post code, then have google work out the distance from an office and send it back to the web site to see if they are too far away (say over 50Miles for example(?
Because that would be very cool.
interesting code snippets » Blog Archive » Geocoding UK Postcodes using PHP said,
May 12, 2008 @ 2:13 am
[…] Geocoding UK Postcodes with Google Map API […]
Rob said,
May 19, 2008 @ 11:31 am
Grewat code works well.
My question is:
I am looping through a database mapping the post codes, but I want to have an Information Window for each post code, how do I do this?
naimulah said,
May 20, 2008 @ 11:31 am
Hi Anthony,
I am looking for a way to find the postcode for a lat/long. Any ideas if i can acheive this and how?
Many Thanks
seeta said,
June 11, 2008 @ 10:37 am
I would like to thank you for coming up with an genius bit of coding and sharing it with the rest of us, so that we may continue and build and adapt for our own applications.
flowerdaddy said,
June 12, 2008 @ 11:17 am
Hi, thanks for this post.
This is much better than the Google geocoding API.
The long and lat that are returned are far more acurate than just putting your postcode into the geocoding api.
I will be using this technique on an estate agency website which I’m currently building.
It will be going live at www.sellinghouses.co.uk next week some time (w/c June 16) in the mean time you can sneak a preview at www.thetaylors.eu/properties.asp
Many thankd for the excellent post.