Reputation: 611
I just need to know how to go about getting a postcode from the longitude and latitude.
Im using the google maps api to get the long and lat based on a search such as 'manchester' i then need a to get the postcode from this to query my database.
My code so far is
function getLatLng()
{
var localSearch = new google.maps.Geocoder();
var postcode = $("#address").val();
localSearch.geocode({ 'address': postcode },
function(results, status) {
if (results.length == 1) {
var result = results[0];
var location = result.geometry.location;
}
else
{
$("#error").html("Address not found");
}
});
}
Upvotes: 2
Views: 26768
Reputation: 11
.. search such as 'manchester' i then need a to get the postcode ..
You might need to think about:-
There is more than one 'manchester' in the world.
'Manchester' in the UK is covered by many postcodes; geocoding may give you only one of them, or none of them if the query is too general.
Apparently due to Post Office licencing issues, returned UK postcodes may be fragmentary - only the most significant part is returned.
Upvotes: 1
Reputation: 1831
First of all, you should know that you can get the postcode together with latitude and longitude when you search for 'manchester'. Check out this part of the docs for this. Check out the reply you get from google and you can get the postcode from that.
Or if you want to run another query to get the postcode from the coordinates you have (which doesn't seem useful since as I said you can do this in one call to the API), you can do reverse geocoding as Geert-Jan suggested.
Upvotes: 0
Reputation: 18895
What you're trying to do is (an example of) 'Reverse Geocoding'
See: Google reverse geocoding - how to snap to nearest full postcode
Specifically a link to:
https://google-developers.appspot.com/maps/documentation/javascript/examples/geocoding-reverse
Upvotes: 6