Frage

Jeder Benutzer in meinem db zu einer Stadt zugeordnet ist (mit seiner Längen- und Breitengrad)

Wie würde ich mich darum, herauszufinden, welche Städte sind nahe beieinander?

d. in England, Cambridge ist ziemlich nah an London.

Also, wenn ich einen Benutzer, der in Cambridge lebt. Benutzer, die ihnen nahe wäre Benutzer in der Nähe umliegenden Städten leben, wie London, Hertford etc.

Irgendwelche Ideen, wie ich über diese gehen könnte? Und auch, wie würde ich definieren, was ist in der Nähe? das heißt in Großbritannien würde die Nähe viel näher sein, als wenn es in den USA waren wie die USA mehr weit ausgebreitet.

Ideen und Vorschläge. Außerdem haben Sie irgendwelche Dienste wissen, dass diese Art von Funktionalität zur Verfügung stellen?

Danke

War es hilfreich?

Lösung

Überprüfen Sie dieses Thema anschauen:

MySQL Großkreisentfernung (Haversine Formel)

Da haben Sie eine SQL-Abfrage in der Nähe von Städten auf Breiten- und Längengrad zu berechnen.

Prost.

Andere Tipps

Wenn Sie einen externen Web-Service aufrufen können, können Sie die GeoNames API zur Lokalisierung der Nähe von Städten innerhalb eines gewissen Radius verwenden können, dass Sie definieren:

http://www.geonames.org/export/web-services.html

Getting coordinates from City names is called reverse geo coding. Google maps has a nice Api fot that.

There is also the Geonames project where you get huge databases of cities, zip codes etc and their cooridnates

However if you already have the coordinates, its a simple calculation to get the distance.

The tricky thing is to get a nice performant version of it. You probably have it stored in a mysql database, so you need to do it there and fast.

It is absolutely possible. I once did a project including that code, I will fetch it and post it here.

However to speed things up I would recommend first doing a rectangular selection around the center coordinates. This is very, very fast using bee tree indexes or even better stuff like multidimensional range search. Then inside that you can then calculate the exact distances on a limited set of data. Outside that recangular selection the directions are so vast that it does not need to be displayed or calculated so accurately. Or just display the country, continent or something like that.

I am still at the office but when i get home i can fetch the codes for you. Int he meantime it would be good if you could inform me how you store your data.

Edit: in the mean time here you have a function which looks right to me (i did it without a function in one query...)

   CREATE FUNCTION `get_distance_between_geo_locations`(`lat1` FLOAT, `long1` FLOAT, `lat2` FLOAT, `long2` FLOAT)
  RETURNS FLOAT
  LANGUAGE SQL
  DETERMINISTIC
  CONTAINS SQL
  SQL SECURITY DEFINER
  COMMENT ''
BEGIN
DECLARE distance FLOAT DEFAULT -1;
DECLARE earthRadius FLOAT DEFAULT 6371.009;
-- 3958.761 --miles
-- 6371.009 --km
DECLARE axis FLOAT;

IF ((lat1 IS NOT NULL) AND (long1 IS NOT NULL) AND (lat2 IS NOT NULL) AND (long2 IS NOT NULL)) THEN -- bit of protection against bad data

  SET axis = (SIN(RADIANS(lat2-lat1)/2) * SIN(RADIANS(lat2-lat1)/2) + COS(RADIANS(lat1)) * COS(RADIANS(lat2)) * SIN(RADIANS(long2-long1)/2) * SIN(RADIANS(long2-long1)/2));
  SET distance = earthRadius * (2 * ATAN2(SQRT(axis), SQRT(1-axis)));

END IF;

RETURN distance;
END;

i quoted this from here: http://sebastian-bauer.ws/en/2010/12/12/geo-koordinaten-mysql-funktion-zur-berechnung-des-abstands.html

and here is another link: http://www.andrewseward.co.uk/2010/04/sql-function-to-calculate-distance.html

The simplest way to do this would be to calculate a bounding box from the latitude and longitude of the city and a distance (by converting the distance to degrees of longitude).

Once you have that box (min latitude, max latitude, min longitude, max longitude), query for other cities whose latitude and longitude are inside the bounding box. This will get you an approximate list, and should be quite fast as it will be able to use any indexes you might have on the latitude and longitude columns.

From there you can narrow the list down if desired using a real "distance between points on a sphere" function.

You need a spatial index or GIS functionality. What database are you using? MySQL and PostgreSQL both have GIS support which would allow you to find the N nearest cities using an SQL query.

Another option you might want to consider would be to put all of the cities into a spatial search tree like a kd-tree. Kd-trees efficiently support nearest-neighbor searches, as well as fast searches for all points in a given bounding box. You could then find nearby cities by searching for a few of the city's nearest neighbors, then using the distance to those neighbors to get an estimate size for a bounding box to search in.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top