faqts : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

4 of 22 people (18%) answered Yes
Recently 2 of 10 people (20%) answered Yes


does anyone know how I can work out the x,y coordinates on a map from a given postcode?

Feb 28th, 2008 00:13
dman, buca bay, Matthew Wilkinson, Dave Martindale, http://www.ttnr.org

There are databases/data available that lists longitudes and latitudes
of cities, and their postcodes. Using this data, you can plot the x,y
cordinates on any map interface, where the x and y are latitude and
There are two ways to get a physical address converted to a latitude,
longitude pair that can be used to plot your point on a map:
i) Geocode locally. Download a list of zipcodes and their corresponding
latitude, longitude cordinates and convert them to a format that you can
access quickly with php. example: save the data to a mysql database.
You can find the data needed at sites like: usps, us census etc.
ii) Use a geocoding service. This is a service offered by a website that
will take an address or zipcode and return its latitude and longitude.  
Some sites that offer free geocoding:
http://developer.yahoo.net/maps/rest/V1/geocode.html, http://geocoder.ca/
All three sites I listed will take an HTTP GET Request, that you can
make from your server suing fopen, fsockopen, CURL etc. 
Heres an example of a requets to the Yahoo Geocoding API:
Just copy and paste the url to your browser. 
To get the actual latitude and longitude cordinates from the Geocoding
Web Service, you need to supply your query (zipcode) in a POST request
to the Geocoding servers API, like the example above. 
The service will return a xml page, which you will then need to parse
with php to get the latitude, longitude cordinates. 
The xml schema of each geocoding service is different so needs to be
parsed differently to get your longitude and latitude pairs.
How to plot this longitude, latitude pair on a map: 
To plot this point on a map, you then need to use the API interface
provided by the map you are using. 
For exmample: you can plot your poing on a google maps Interface, by
using the javascript API described here:
Or you can plot your poing on yahoo maps:
Or you can use a custom map interface.