0

I have a local database with over 2000 locations that I am trying to search through based on the users location. I only want to display a few markers in the Map Fragment nearby the user, but have so far been unable to find a way to do it.

Currently the database is in the form of a csv file, so I can change it to a different type easily. But I was waiting to do so until I could find a utility that can search the coordinates based on a radius around the user. Anybody have any suggestions?

D-Kent
  • 142
  • 8
  • You might consider using [`android-map-extensions`](https://code.google.com/p/android-maps-extensions/) and letting it coalesce markers when you have too many for the current zoom level. Then, you can add all the markers and not worry about finding the nearby ones. Beyond that, http://stackoverflow.com/questions/3695224/android-sqlite-getting-nearest-locations-with-latitude-and-longitude may be useful. – CommonsWare Oct 01 '13 at 17:20
  • Thank you a lot. I'll check it out to see if it can work, looks promising. – D-Kent Oct 01 '13 at 17:23

2 Answers2

0

You can define a LatLngBounds object for some box around the user, and then use bounds.contains(LatLng) to determine which of your locations are in that radius. I've used this method for collections of about the same size as yours and it was fast enough for our purposes. You will be searching a rectangle, not a radius, but since the map is a rectangle, maybe that makes more sense anyway?

GLee
  • 5,003
  • 5
  • 35
  • 39
  • I'm thinking that I will try this approach first as it seems like a bit less code than the other answer, though it does pretty much the same exact thing. Hopefully it is pretty quick. – D-Kent Oct 03 '13 at 20:16
  • Yes it worked out great. The only downside is that I didn't notice a way to pull the coordinates out of the object with something like getLatitude(). But it's simple enough to work around that anyways – D-Kent Oct 07 '13 at 18:51
  • Cool. Did you try using marker.getPosition()? That returns the LatLng. Also, I used the snippet to hold my internal representation of the name of the marker. It's a bit of a hack, and you have to make sure it doesn't display, but it works. – GLee Oct 11 '13 at 16:58
0

This is a tricky topic and solution is not straight forward. I am briefly listing down the steps I have used and this works for well over 100k assets in my case.

  1. Assuming you already have logic to get your current location and lets call if as myLocation.
  2. Add your location data in sqlite file and treat location coordinates as nothing more than float.
  3. Use offset as 0.001 which is roughly around 100 m and use the following in the query.

    (myTable.lat > (myLocation.lat - offset) AND myTable.lat < (myLocation.lat + offset)) AND (myTable.lon > (myLocation.lon - offset) AND myTable.lon < (myLocation.lon + offset))

  4. This should give you the list of assets within a small radius

The trick here is to treat location as float and not really location.

For Offset: http://en.wikipedia.org/wiki/Decimal_degrees

PravinCG
  • 7,688
  • 3
  • 30
  • 55