Domanda

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.

È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top