6

Is it possible to do a google map lookup using the google maps API from a UK postcode? I know you can search by UK postcode on their website, but this converts to lat / long. I don't have access to the PAF database to be able to convert to long / lat.

An example:

Users have an item to sell. One of the details of that item is a postcode, where the user / item is located. When the items are displayed on the front end of the website, there needs to be a google map of the items location generated using the postcode.

If this is possible, how do I do it?

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
Robin Barnes
  • 13,133
  • 15
  • 44
  • 45

9 Answers9

11

What about using:

<img src="http://maps.google.com/maps/api/staticmap?center=POSTCODEHERE&zoom=14&size=200x200&maptype=roadmap&markers=color:ORANGE|label:A|POSTCODEHERE&sensor=false" style="float: right">

Then replace POSTCODEHERE in the two sections above with their postcode.

You can change the size from 200x200 or the marker colour, label etc. too if you wish.

dbramley
  • 111
  • 1
  • 2
3

You can do it purely though Google maps.

I did it for a client earlier this year and have just had to do a few modifications. I also did some direction-grabbing. It's all pretty simple but best viewed in context.

Take a look at the source of the page I made.

Community
  • 1
  • 1
Oli
  • 235,628
  • 64
  • 220
  • 299
  • Your site seems to automate google, not quite what the OP asked for, but quite possibly what he wants. – gbjbaanb Dec 20 '08 at 18:00
  • Have you looked into the legality of this approach though? (as mentioned in my answer) – Ben Aston Dec 20 '08 at 18:52
  • Re both: It's all using their public API. It's not the best documented of features, but it's not like I'm screen scraping! I am doing more than the OP is asking for because I'm hooking into LocalSearch for directions, but the whole geo-coding thing is possible. – Oli Dec 20 '08 at 22:05
  • Hi, I had a look at the js source for the page you made and rexamined the google api. It looks like you're right. Going to try to implement it all tomorrow. Thanks a lot. And yes - your solution seems perfectly legal as part of google's api allows for an api call based search. – Robin Barnes Dec 31 '08 at 02:48
  • 3
    the source pointed to by this page no longer exists, therefore this answer becomes pretty much useless. Please revise and include more information in the actual post itself. – Mark Allanson Mar 05 '11 at 16:10
  • 1
    Vote turned upside-down! Though I still think you should add some more detail in the answer, for posterity. – Mark Allanson Mar 28 '11 at 14:22
  • FYI, it seems the A & B of that get directions search are around the wrong way? I would want to go from my entered postcode to the business in question. – Mark Glasgow Apr 30 '15 at 07:22
1

It's (now) very easy to do this using google's LocalSearch API:

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");
}

callbackFunction() will receive a GLatLng object with, in my experience, very accurate coordinates. In fact, it's trivial to then feed that GLatLng to a GClientGeoCoder's getLocations() method and get back full Placemark details, which include details down to the level of address range (e.g. 1-18 Foo Street).

The real question is: how legal is that?

Bobby Jack
  • 15,689
  • 15
  • 65
  • 97
1

Google does not provide a geocoding api in the UK because of the licensing model the Royal Mail releases postcode data under.

The are however some tools that people have written that enable geocoding using google, but that would be technically illegal afaik.

One option then is to use one of the several uk geocoding providers. I don't want to sound lazy but they are easily googled. They typically charge a few pence per geocode.

Ben Aston
  • 53,718
  • 65
  • 205
  • 331
0

you need the PAF database, each postcode is held as a polygon, so unless you have that initial data you cannot restrict the search to the polygon, or to a radius around the centrepoint.

The postoffice will sell you all the data you require though, prices start from £85pa.

PS. Google does it because they have the PAF database, when you type in the postcode, they lookup the centre and display that on their map.

gbjbaanb
  • 51,617
  • 12
  • 104
  • 148
  • Yeah this is pretty much what I thought the deal was. Really only £85 for PAF? At one company I worked for they charged us like £5k for it. – Robin Barnes Dec 20 '08 at 16:24
  • "start" at £85, for a single user. For a website, it's £3850, but you do get everything, not just postcodes. – gbjbaanb Dec 20 '08 at 16:56
0

The folks behind openstreetmap.org have been working on a free equivalent - I don't know how ready for prime time it is, but FWIW:

http://freethepostcode.org/

frankodwyer
  • 13,948
  • 9
  • 50
  • 70
0

Small UK business or charity can apply for a free licence of use PAF. https://www.poweredbypaf.com/register-for-free-use-of-paf/

faye
  • 95
  • 7
0

http://www.websemantics.co.uk/resources/postcode_to_coordinates_conversion_tool/

Site will provide you with a longitude and latitude which you can place right into your google code.

Shadi Almosri
  • 11,678
  • 16
  • 58
  • 80
-1

No one seems to have searched very hard... there is a freely available list of UK postcodes and positions out there. More importantly its all redundant because Google maps provides the ability to search by post code to begin with. Even using the API is a redundant extra given that the service is provided over the internet... there is nothing to stop you sending a http request yourself and displaying portions of the returned data, as long as you preserve any necessary copyright messages etc..

jheriko
  • 3,043
  • 1
  • 21
  • 28