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

I have a dataset from different places and want to show the nearest locations (within 5 km).

How to determine the minimum / maximum latitude and longitude?

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

I would like to avoid iteration in all places, because I have about 2500 places and growing. Any suggestions on this?

Thanks guys for the advice on this!

Update: Thank you for the feedback you gave me, I solved the problem by iterating over all locations on the server and using the Google API to determine the distances: http://code.google.com/intl/nl/apis/maps / documentation / distancematrix /

+4
source share
3 answers

Simplification, latitude - the angle above / below the equator, longitude - the angle to the right / left of the green meridian.

So, to calculate (on average), for example, 1º latitude, you convert it to radian (multiply by PI / 180) , and then multiply by the radius of the Earth (6,371.0 km) .

On your question, the process is the opposite: take 5 km and convert it to degrees:

  • Divide it by the radius of the earth
  • Multiply by 180 / PI

Thus, you will get delta degrees, that is, how many degrees is 5 km (on average, if you want accuracy, you will need the exact radii of the Earth's radius over these 5 km), with which you can build a circle around a given location (exactly same as compass ).

+2
source

All calculations give, and methods - approximations, but within the tolerances for what you need.

The circumference of the earth is about 40,076,000 meters.

the distance traveled per degree of latitude is always the same and is simply part of the earth’s circle.

the distance in degrees in longitude varies depending on your latitude (this ring on glode gets closer to the poles).

therefore, for a given distance m, the corresponding latitude and longitude

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 wide deltas of long latitude for easy retrieval of your data. but on the fingers of the candidate, you have to do a full distance calculation, since the angle of the square is quite a bit more than 5 km.

distance between 2 lat / long

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

I know that most compilers / languages ​​use radians for cos / sin functions, but its simplicity is explained in degrees.

since the search for your data is the easiest way to order either lat or long, then you can perform a binary search to find a possible place to check instead of a full scan. There are better ways to organize the data (ATVs), but for 2500 ish records I would not worry

0
source

There are two questions: 1) how to calculate the distance between two pairs of lat / lon and 2) how to find the point with the shortest distance to this point.

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

  • This is the (geo) spatial indexing of the 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 that your points are not too close to the polar, which complicates the situation, but still doable). The square tree allows you to find in Log (N) the time of your car's neighborhood without having to go through all the points.

Not exact code, but hopefully this helps.

0
source

Source: https://habr.com/ru/post/1397470/


All Articles