How should I make the "close to".. "near by" - php

I would like to make add a feature to my search field.
So you can write Close to: Copenhagen (example).
It should then take like 5 km range around /inside Copenhagen (copenhagen is capital of denmark), and get the addresses that are within, and show them as results..
So it takes all addresses, checks if they are within 5 km range around copenhagen and then display those that are.
Is this possible to do? If not, any simliar ways to do this? What should i look into?

You can use (for example) the Google Maps API to geocode a text description of a location (the search term, and the addresses) into a GPS coordinate location. You then check which of the locations in your database is within range of the search term location. The terms and conditions of the use of that API require you to use the geocoding results to display a Google Map though.
Determining "within range" efficiently might be tricky though, you generally want either a GIS-capable database to do the calculations for you. A hackish shortcut that's easy to implement over any DB is taking the points 5 kilometers (or whatever distance you want) north, east, south, and west of the search term location, and then looking for addresses that have latitudes and longitudes between the minimum and maximum latitudes and longitudes that gives you. This page lists what the required formulas are. (This approach will not work if the searched area is near where longitude "wraps around" and doesn't search a rectangle because of the curvature of the earth, but not many people search for restaurant reviews or what have you in the Arctic or the middle of the Pacific.)

You should look at geocoding.
One way to do that is to use the Google Maps API, it allows you to convert an address into coordinates and from those you can easily calculate addresses that are within a certain distance.
Take a look at:
http://code.google.com/apis/maps/documentation/webservices/index.html
I'm sure that if you look for "geocoding" you will find many other solutions, if you don't like this particular one.

Related

Calculating distance between two post codes

I would like some help with how to go about calculating the distance between two post codes. Here is the setup that I wish to have, let me give you a scenario.
A user logs into my website (they have provided their details, their postcode being one of them). That data is stored in the users table.
I have another table with details about locations. I would like to display locations (and the distance) that are closest to the user.
I know that there are certain methods that can be used, for example:
Converting a postcode to a long-lat and using built in methods to calculate the distance, I think Google offer a free service. But I have no idea how I would implement such a thing. All the tutorials I have taken a look at seem too complicated and for the time being I want a very crude and simple solution upon which I can build and more importantly learn!
I am fairly familiar with PHP and MySQL when it comes to basic input/output of data. But I am not sure of the best way to handle the information with regards to user data, and the locations of the 'businesses'.
Any suggestions, comments will be more than appreciated.
If you're US based, this API should meet your needs: https://www.zipcodeapi.com
It's made specifically to find distance between two zip codes, tells you the long-lat for zip codes, find all zips relative to a location, etc.
Get postcode northings eastings data free from ordinance survey (credit them on your webpage to get free)
2 calculate distance from the appropriate csv flat file (M.csv contains all manchester postcodes and north and eastness).
Distance is sqrt((n1-n2)**2 +(e1-e2)**2) - excuse my fortran
If its slow,and you want an approximate distance create a small database of first half postcodes averages so you are looking up M20 not M20 3nb.
5 NB google don't like you using their postcode apis outside google Maps.
I am wondering about an algorithm so that for short distances, if you are looking at places close to a postcode, you just search M postcodes for a M postcode (and maybe nearby post areas like SK, OL, if you have a table of adjacent postcode areas).
7 Any body taken this further?

Google Places API search for a PLace in GLOBE

I want the user to enter a name of a place, and it will return all the matching places, User will select one from the list, In Google Place API I need to provide a lat, lng and search within a certain radius.
But I want no radius, rather I want it to search the globe, much like the place search in Google Earth.
Right now I am planning to observe Google Earth under wireshark and I'll repeat the same. Is that lawsuit ? or is there any other straight way to do the same ?
I've no problem in using services like bing or any others, if they offer the same
Have you thought about entering a lat,lng of 0,0 and a radius of 12,500 miles? That should cover the world.

Post Code Lookup

A client wants a page on their website where the user can search for a stockist of their product range. What they want is the ability to enter a UK PostCode and for a list of their stockists to appear in closest order.
Is there a way to utilise Google Maps to determine the closest supplier?
I have a PHP MySQL database with ALL the suppliers Postcodes and details and I'll have the postcode of the user to use as well.
I think you'll need the Ordnance Survey CodePoint data to map your postcodes to a longitude and latitude. Codepoint is available as a commercial product, or there's also Codepoint Open
Once you have the latitude and longitude, then you can use standard algorithms such as Haversine or Vincenty to calculate distances between the points
EDIT
Structure of the CodePoint Open CSV files can be found here. Note that location is held as Northings and Esatings rather than Longitude and Latitude, so it will need converting. There's a number of articles about this on the web, e.g.
http://www.deepbluesky.com/blog/-/converting-os-coodinates-into-longitude-latitude_7/
http://mediakey.dk/~cc/convert-northing-and-easting-utm-to-longitude-and-latitude/
but you need to be aware that OS Northings/Eastings are based on the Airy 1830 ellipsoid rather than the WGS84 model used by Google maps (and most GSM systems). Failing to allow for this difference can put you out by anything between 70-120m between Cornwall and East Anglia.
You can also find PHP functions to do this conversion at the Movable Type site (essential reading for any PHP developer, working with GeoData. I'd recommend adding a couple off columns to your stockist database for longitude and latitude, and a one-off script to update all existing data using the Codepoint data, then modify your insert/update routines for stockists to keep this information up-to-date.
Using PHP, another solution for this conversion is PHPCoord by Jonathan Stott
For higher performance in your database query, do a lookup of lat/long against a "bounding box" before calculating distances. I've explained how to do this in response to a similar query.
I think you'll need a little more data than that. I'd suggest that you need to store the latitude and longitude of every UK postcode alongside the postcode itself. I think this data is available from the Royal Mail for a cost and I remember reading somewhere that it was going to be available in the public domain too. Have a user enter their postcode, look up their latitude and longitude in the db and then use that to perform another query that calculates the closest supplier to them, perhaps within a certain number of miles. You could perhaps create a stores procedure to do all of this on the db. This post seems to have some details on how to do this.
If you want it to be fast then pre-compute the distances between postcodes within a certain radius of each other - see the following:
Calculate distance between zip codes and users
This should work fine in your case unless you think a user would be willing to travel more than 100 miles to purchase a product ??
Instead of Google Maps, this open postcode geocoding API may be of use to you. As others have mentioned, once you have lat long for both points you can use standard algos to find distances. A previous question contains some info on how to do this directly in SQL.
You may find this website helpful: SQL/CSV Postcode Database
I've previously used this to lookup long/lat from a postcode "outcode".
There's also a php script (which I've not tested) which calculates distance between two postcodes.

"Find nearest location" by Zip/Postal Code?

I need a "find nearest location" on our website.
Where visitor enters their zip/postal code, then they are redirected to specific webpage for our nearest location. We have forty USA and Canada locations.
How can I build something like this? Could I do this with the Google Maps API? I already have a custom map on Google Maps. It's plotted with our locations. It would be nice to send Google Maps a command to say "what's our nearest location at ________ zip code".
Any suggestions?
if you have the longitudes and latitudes of each zipcode (search google) you can use the
Haversine Formula to calculate nearest neighbours.
http://www.codecodex.com/wiki/Calculate_Distance_Between_Two_Points_on_a_Globe
You need a database of zip codes with longitude and latitude, from which you can calculate the distance.
Google Maps have restrictions in using is for business purposes, so you might want to consider those (restrictions) upfront.
From my former experience I would suggest MapQuest API. You can find details here:
http://developer.mapquest.com/ and some quick start guide here http://www.mapquest.com/features/developer_tools_oapi_quickstart
MapQuest seams to be better when it comes to commercial deals. Anyway, check their T&C before you will implement it in production.
They have JavaScriopt an I think regular WS for geo-location decoding.
Regards
Konrad

google Maps getDistance without loading the page

Google maps has a function that lets you retreive the distance between two points: getDistance(), of the GDirections class.
Now, this is Javascript, so it'll only be able to calculate once the page has loaded.
My search form however, has the ability to indicate what's the maximum distance you want between yourself and another person and based on that, a list of search results has to be provided.
Since I can't use this function in PHP, is there another way for me to calculate the distance between 2 points on the earth? By giving up the street, postal code and city name, just like what Google maps needs.
This page has a list of google maps parameters for use in http connection. By specifying the output paramter you can choose to give back kml files or similar and can be used in any lanuage that can make http connections.
Looks like you want to calculate a Great Circle Distance
Formulas have been discussed here on stackoverflow before.
A point on earth is defined by it's latitude and longitude. If you want to calculate the distance between 2 points on earth by giving up the street, postal code and city name, you will need geo-referene data.
This data is available for free on the internet, but the accuracy and availability differ greatly from region to region. (USA data is of good quality, data for Kenia for example will be harder to come by)
So to answer your question:
Yes, there are other ways to calculate what you want. But they require more work/are more complex than just querying the google API.
You might want tot read: Creating a Store Locator with PHP, MySQL & Google Maps
Hope this points you into the right direction.
You can use kml file. It's xml-formatted file that you can recieve by link like http://maps.google.com/maps?f=d&hl=en&saddr=<src latitude>,<src longitude>&daddr=<dist latitude>,<dist longitude>&ie=UTF8&0&om=0&output=kml
in recieved file you can parse and summate distances from

Categories