I am building a script that's powered by google maps where user chooses the a location from a map and save the long, lat in database aa a part of directory listing form.
I was thinking to add a search functionality by world continent for (America, Asia, Europe, Africa).
but this require having the coordinates of these locations like America long between 'xx' and 'yy', lat between 'aa' and 'bb' so I can look it up in the database.*
And I don't seem to find these info any where,
Any help would be appreciated.
Ok, as a quick solution for this search functionality, I'd set-up two tables in a database. One would map every listing to a country, another would map every country to a continent, so that search could be performed joining these two tables. Use google geocoding to get country from latitude/longitude if needed.
A mysql continent/country database can be found here.
This website will help you quickly get your own bounding boxes:
http://bboxfinder.com
For example, for one project I didn't want to include Europe all the way North to Knivskjellodden or east to the Urals, so I drew this:
There are no simple "lat/lng bounding boxes" for the continents because the continents have irregular boundaries. For example Ankara in Turkey has approximatively lat=40, lng=33. But the latitude 40 crosses Europe, Asia and America; and the longitude 33 crosses Europe, Asia and Africa.
Similarly, there are no simple "lat/lng bounding boxes" for states.
If you know the tiling algorithm of google maps you can try the spatial index to get all geo codes from a bounding box from a continent. Like the tiling algorithm this method is not very accurate.
Related
Hi,
I am now developing public transportation guide software. In Europe an US Google Map provides this but in Turkey it does not. I have database which contains all stations’ latitudes and longitudes, and other bus- line, station information. In my plan, firstly I will use graph theory (stations are vertexes; edge weights are distances among stations) and connect stations which are on the same bus-line; then find routes. After that I will demonstrate route on Google Map. I have accomplished the first step, connecting stations. However, after that I found a mistake in my plan which is shown in the figure.
Person wants to go near A to K, the program should say
Walk to station A
Got on bus 8 at station A
Got off bus 8 at station E
Walk to station H
Got on bus 970 at station H
Got off bus 970 at station K
But, there is no connection between station E and H. So, graph algorithm cannot find a route from A to K. I should define a walk path between E and H. However this is only small demonstration of the city there are over 6500 station in the city. How can I solve this problem?
I have an idea that add connections between stations with in 1km range; but I think that it is inefficient.
Thanks.
Add a directed edge between E and H with weight say 'a'. Choose 'a' in such a way that none of the edge weights in your network have the same weight 'a'. For example, you can choose 'a' to be 0 as I am sure none of the edges in your network have weight zero as that would mean there is no distance between some two stations. Next, code your program in such a way that whenever there is a route selected that contains an edge with weight 'a', then the program should say, "Walk from station E to station H" or whichever nodes the edge with weight 'a' connects.
Otherwise, you can have a graph such there are two directed edges from one node to another whenever you have to walk. Say one edge is weighted 0 and the other by the distance between the two stations. Make sure your program is coded in such a way that when there are two edges encountered between say stations A and B, one with weight 0 and the other with say 'x', then the program gives the instruction, "Walk from station A to B, a distance of 'x' km."
I have city example: London with center lon, lat and radius of city.
I want to divide city region into 20 or n different circles and want to get lon, lat and radius of each region.
Is there any algorithm available in php?
Thanks
Consider using an existing database/service to turn your lon/lat values into existing region/city/district information including zip/post code or NW, SW etc. Providers often provide good APIs and example code for this to make your life easier.
http://www.gpsvisualizer.com/geocoding.html
I am developing a travel portal website.
Presently I am working on the destination module.here I need to display the nearest cities based on the places entered.
for example,If any one enter the name bangalore,The nearest cities around bangalore city should display.
I am searching for the google api to get the nearest cities around a place.
Can anyone help me to get the best api to display the nearest cities ??
Thanks In advance
This might be an alternative for you,
Another option:
Download the cities database from http://download.geonames.org/export/dump/
Add each city as a lat/long -> City mapping to a spatial index such as an R-Tree (some DBs also have the functionality)
Use nearest-neighbour search to find the closest city for any given point
Advantages:
Does not depend on aa external server to be available
Very fast (easily does thousands of lookups per second)
Disadvantages:
Not automatically up to date
Requires extra code if you want to distinguish the case where the nearest city is dozens of miles away
May give weird results near the poles and the international date line (though there aren't any cities in those places anyway
I'm working on a website that allows users to find our nearest Motel location (There are 26 of them across the US). I have a list of cities where they are located at.
I want to display the nearest location when a user goes on our front page. For example if a user comes from Newark, NJ, he will be shown images from our NYC motel and if a user comes from San Jose, CA he will be shown San Francisco images.
What's the best way to do this? Does anyone know any examples out there on the web that shows what I'm trying to do? Is this even possible?
I saw Groupon and LivingSocial using this so I thought why dont I give it a shot. :)
If you're using apache, you can give geoip a try.
http://www.maxmind.com/app/mod_geoip
You'd first need to store the latitude/longitude coordinates of your motels in a database - use google maps, it won't take too long with 26.
Then get the visitor location, using something like IP2Location - http://www.ip2location.com/developers.aspx
Then calculate the distance between the visitor and each of your motels - http://sebastian-bauer.ws/en/2010/12/12/geo-koordinaten-mysql-funktion-zur-berechnung-des-abstands.html (it is in English...)
You could give Geolite city a try. It is free (with an attribution clause) or can alternatively be bought. That reduces the "find closest city to user's IP address" problem to "find the closest city to a known city".
The same company offers a city database, which among other things contains longitude/latitude. That should do the trick.
You can get the nearest city to an IP address using the free data on MaxMind.com (I beleive you can pay for more accurate databases). If you can get the lat/long for the city and compare it to the lat/long for each of your motels you win :)
What you want is to store the lat/lng coordinates of the motels and then let the user type his location into the browser most likely a zipcode and then you want to use the harvesine formule to calc the distance between the user and the motel to display nearby motels. Or you can use a IP to geo coordinate service like IP2location to get the users location.
I used this code to calculate distance of a googlemaps latitude and longitude from a table of addresses.
Geocode was a function that returned an array of latitude and longitude from google maps api given an address string, in the case below a zip. The table of your locations would have to include lat and lng columns. In this example i used decimal(8,5) types but you could also use a point column type.
$starting_location = geocode($zip);
$distance = '(3959 * acos(cos(radians('.$starting_location['latitude'].')) * cos(radians(lat)) * cos(radians(lng) - radians('.$starting_location['longitude'].')) + sin(radians('.$starting_location['latitude'].')) * sin(radians(lat))))';
$location_row = query('SELECT location_id,addr,addr2,city,state,zip,phonenumber,'.$distance.' AS distance FROM location_info WHERE '.$distance.' is not null ORDER BY distance LIMIT 1');
Correct me if I am linking to another's answer incorrectly, but I think I got my direction from this question: Fastest Way to Find Distance Between Two Lat/Long Points
In my database I have a list of places and for each I have a street name and number, postcode, city and county. Some of them have a latitude and longitude location.
And I have the geo location of the city centre for example. I would like to display only the places that are within X miles of the city centre on a google map.
Incase this would need a geo location for each of my places to work, I could perhaps set up a script to use google maps api to use geocoding to get a geo location for all my places and update the database with the lat/lng. Then I would have a database full of lat and long locations to work from.
Once all the places have a lat/lng then maybe mysql can return the within range addresses?
This is not hard once you have lat / long data, and if somebody gives you the great circle distance formula in mySQL format.
#maggie gave a good reference. How to efficiently find the closest locations nearby a given location
Indexing strategy: Keep in mind that one minute of latitude (1/60 degree) is one nautical mile, or 1.1515 statute miles (approximately) all over the world. So index your latitude column and do your search like this. (If you're in the part of the world that uses km, you can convert; sorry for the Old-British-Empire-Centric answer, but they did define the nautical mile.)
WHERE mylat BETWEEN column.lat-(myradius*1.1515) AND column.lat+(myradius*1.1515)
AND (the big distance formula) <= myradius
This will give you both decent data base indexing AND reasonably accurate distance circles.
One extra refinement: You can index longitude too. The trouble is that ground distance isn't directly related to longitude. At the equator it is one nautical mile per minute, but it gets smaller, and at the poles there are singularities. So, you can add another term to your WHERE. It gives correct results but isn't as selective as latitude indexing. But it still helps the indexing lookup, especially if you have lots of rows to sift through. So you get:
WHERE mylat BETWEEN column.lat-(myradius*1.1515) AND column.lat+(myradius*1.1515)
AND mylon BETWEEN column.lon-(myradius*1.1515) AND column.lon+(myradius*1.1515)
AND (the big distance formula) < myradius
Most likely you want to use a space-filling-curve or a spatial index to reduce your 2D problem to a 1D problem. For example you can combine the lat/long pair with a z-curve or a hilbert curve. I use for myself a hilbert curve to search for postcodes. You can find my solution at phpclasses.org ( hilbert-curve ).