简体   繁体   中英

Android:How to find latitude and longitude range with a lat/lon location as center?

I have a data set of different locations, and want to show the nearest locations (within 5 km).

How can I determine the minimum/maximum of latitude and longitude?

fe: I need to fill my car up, and am looking for all gas stations in my neighborhood so I can go to the nearest. How do I do this on an Android phone?

I'd like to avoid iterating through all of the locations as well, because I've got about 2500 locations and rising. Any suggestions on that?

Thank you guys in advance for the advice on this!

Update: Thank you for the feedback you guys gave me, I solved my issue by iterating through all locations on the server and using the Google Distance Matrix API to calculate the distances: http://code.google.com/intl/nl/apis/maps/documentation/distancematrix/

Simplifying, latitude is the angle over/under the equator, longitude is angle right/left of greenwich meridian.

So to calculate (on average) how much for example 1º latitude is, you convert it to radians (multiply by PI/180) , and then multiply by Earth's mean radius (6,371.0 km) .

For your question, the process is the inverse one: take 5 km and convert it to degrees:

  1. Divide it by Earth's radius
  2. Multiply by 180/PI

This way you will get delta degrees, that is, how much degrees are 5 kms (on average, if you want exactitude, you will need the exact Earth radius differentials over those 5 kms) with which you can build a circle around the given location (just like a compass would).

All the calcuations give and methods are approximations but well within tolerances for what you require.

The earth circumference is approx 40076000 metres.

the distance traveled per degree of latitude is allways the same and is simply a proportion of the earth circ.

the distance travel per degree in longitude however changes depending upon your latitude ( this rings on the glode get smaller nearer the poles ).

so for a given distance m, the corresponing Latitude and Longitude values are

earthcirc = 40076000;
// at Lat and Lon for distance m (in meters)
LatDelta = (m * 360) / earthcirc;
LonDelta = (m * 360) / abs(eathcirc*cos(lat));

This gives you your square lat long deltas for a simple search of your data. but on fingin a candidate your should then do a full distance calc as the corner of the square is quite a bit more than 5 KM away.

distance between 2 lat/longs

distLat = (lat1-lat2) * earthcirc) / 360;
distLong = (long1-long2) * earthcirc * cos((lat1+lat2)/2) / 360;
dist = sqrt( sqr(distLat) + sqr(distLong) );

I know most compilers/languages use radians for cos/sin functions but its easire to explain in degrees.

as for searching your data the simplest way is to order in be either lat or long then you can do a binary search to find the possible location to check instead of a full scan. There are better ways to order the data ( quad trees ) but for 2500 ish entries i wouldnt bother

There are two issues here, 1) how to calculate the distance between two pairs of lat/lon and 2) how to find the point with shortest distance to a given point.

  1. There are formulas on the net, some more accurate than others, for example http://www.movable-type.co.uk/scripts/latlong.html

  2. This is a (geo) spatial indexing problem (http://en.wikipedia.org/wiki/Spatial_index#Spatial_Index ). You can use for example a quad tree with lat/lng as X/Y (I assume your points are not too close to the polars, which complicate things but still doable). The quad tree let you find in Log(N) time the neighborhood of your car without having to iterate over all points.

Not exact code but hopefully it will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM