0

I've looked around for this most of the morning and haven't found a suitable way of finding UK postcodes from their geolocations (longitude / latitude) using Google Maps, is there anyone out there with any experience of this?

I understand Royal Mail has an API for the postcode search from a geolocation, but it's horribly expensive for such data, surely there has to be another way?

To note, the functionality I'm trying to achieve is that of: http://www.rightmove.co.uk/draw-a-search.html. When the shape is created, a list of UK postcodes (the first 3/4 digits of the postcode, for the area) must be given.

Thanks in advance.

Chris Dixon
  • 9,147
  • 5
  • 36
  • 68
  • 1
    https://developers.google.com/maps/documentation/geocoding/#ReverseGeocoding – Paul Sullivan Apr 23 '14 at 13:17
  • I did come across that, but there seems to be nothing there for areas, just long/lats? In theory I'd need a number of geolocations to be passed in and all the postcodes given that are in that area. – Chris Dixon Apr 23 '14 at 13:18
  • Isn't that a "a suitable way of finding UK postcodes from their geolocations (longitude / latitude)"? – geocodezip Apr 23 '14 at 13:32
  • There's a 3rd paragraph in the question, from the beginning of posting it :) – Chris Dixon Apr 23 '14 at 13:50

1 Answers1

2

You have at least 3 requirements

  1. A list of UK postcodes with coordinates.
  2. Drawing polygon on Map.
  3. Locating postcodes within polygon.

Google for #1 (uk postcode coordinates free). Store data required in database

Use Google Shape library for #2

For # 3 you

Use the following query to eliminate the majority of postcode outside polygon.(Server side)

SELECT  name, lat, lng FROM table WHERE (lat BETWEEN minLat 
                      AND minLat )AND (lng BETWEEN minLng AND minLng)

to

Then use point in polygon to eliminate points outside polygon (client side)

function pointInPolygon(polySides,polyX,polyY,x,y) {
 var j = polySides-1 ;
  oddNodes = 0;
  for (i=0; i<polySides; i++) {
    if (polyY[i]<y && polyY[j]>=y  ||  polyY[j]<y && polyY[i]>=y) {
        if (polyX[i]+(y-polyY[i])/(polyY[j]-polyY[i])*(polyX[j]-polyX[i])<x)  {
            oddNodes=!oddNodes; 
        }
    }
   j=i; }

  return oddNodes;
}

This Link shows #2 & #3

david strachan
  • 7,174
  • 2
  • 23
  • 33
  • For part 3, Google Maps has a containsLocation method whcih may be easier to use, https://developers.google.com/maps/documentation/javascript/examples/poly-containsLocation – Doogal Sep 20 '14 at 21:39