3 Android GPS Questions - android

1) For some reason, the longitude and latitude are slightly different after storing them. For example, when i first find them, they are:
25.171057657111998 and 35.013447496224636 but after I store them , they are
25.1710586547852 and 35.0134468078613. Why is this happening? I store them as floats in an sqlite database, retrieve them with Cursor.getFloat, and print them with String.format of 13 digit accuracy.Can this difference affect the end results in a significant way? I am working with distances <100m (328 feet)
2)I am trying to find the center of a location cluster. Here
http://www.geomidpoint.com/calculation.html
method C says I can just take a simple average if I work with <400km. Has anyone tried it? Is it working? Or should I go for the first, more accurate method?
3) After finding the center, do I need anything else to create a new location object for distance purposes?

To solve this problem you may store longitude and latitude in your sqlite database by this way:
longitude*10^10 , latitude*10^10
when you get them you will divide them by 10^10.
because with 5 decimal is accurate

Related

Android Google Map Finding Nearby Pre-Listed Locations

I have an ever increasing list of places identified by lat/long stored in my database. Now at the UI front there is a screen with a place auto-complete textbox means if I type a location there a drop down will appear with places and when I select the place the camera will move there. Also it is required that I display locations nearby (locations as stored in my database). Now the question is how to do this? I can use
SphericalUtil.computeDistanceBetween()
But the problem is I can't fetch all lat/long from my database and calculate distance without hampering performance.
You would need to compute the distances in your database. There are two basic options:
Implement your own distance method in your database:
If your positions are close to each other and close to the test lat/lng you can use Pythagoras's theorem (a ver basic approach that will not be correct in all the cases)
To address all the possible cases you can implement yor own haversine formula (the haversine formula gives great-circle distances between two points on a sphere from their longitudes and latitudes and is used to compute distances in the SphericalUtil.computeDistanceBetween() method).
Use a GIS database. You can use SpatiaLite for Android and use the ST_Distance function to filter your positions:
SELECT *
FROM yourtable
WHERE ST_DISTANCE(Geometry, MakePoint(yourXcoordinate, yourYcoordinate)) < yourdistance

How to figure out when a gps coordinate is valid in Android

I'm developing an Android app and I need some help to save GPS coordinates for a route... coordinates are very important for my app, so: how can I do in order to get very good coordinates (especially at a first try)? How can I understand whether the position is correct or not? How can I use getAccuracy method or similar to figure out whether the position is wrong so that I have to reject it?
For example: I get a 1st LatLng coordinate but the 2nd LatLng is located 100 meters away from the 1st coordinate, so I guess that is very unlikely that a user can move 100 meters away in a few seconds... how can I create such an alghoritm?
Android Defining a Model for the Best Performance to get location
http://developer.android.com/guide/topics/location/strategies.html#BestPerformance

Display nearby markers in Map Fragment based on location from over 2000 locations?

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?
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?
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.
Assuming you already have logic to get your current location and lets call if as myLocation.
Add your location data in sqlite file and treat location coordinates as nothing more than float.
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))
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

Android SQLite query for rows with distance between my location and a given distance value

I have a SQLite database in Android with a table that contains longitude, latitude and other columns.. now I want to get all records that have the distance smaller or equal to a given distance.
For example, I know my current coordinates(lat,long) and I want to get from my table all records that are at the maximum distance of 10km from me.
I found some links on stack but nothing too solid. Is there someone that knows an optimized solution for this problem?
I have thought that I could get all records that have latitude smaller than my lat + distance and greater than my lat - distance and longitude smaller than my long + distance and greater than long - distance. After this query I should check for some unwanted records since the query is not returning only the wanted records..
Is this a good idea?
You probably want to do this in two parts,
1) Run a query where you find all records that are within a certain value + or - of the current lat/lng of your location, the where clause might look like:
where (#latitude > (lat - .001) and #latitude > (lat - .001)) and
(#longitude> (lng- .001) and #longitude> (longitude- .001))
2) with the rough results from above, use the great circle/haversine method to determine what the actual distance between each location is (great circle/haversinse is already part of the android maps api).
Just for the record, it can be useful.
I don't know what you are trying to do, or if you have a backend or a server. But, if you DO have a server which stores these locations, you can (and should) use MongoDB to store it. It has a great support for geospatial information.
Here is an example of how you do what you want using MongoDB: http://www.mongodb.org/display/DOCS/Geospatial+Indexing#GeospatialIndexing-Querying

Get next N nearest Geo-Points

I have in my android application a database table with geo pointes (lat and lon are decimal degree values), about 1000 points. And I need to select 20 nearest point to some given geo point.
I've found at Stackoverflow the answer how to compute distance between two geo points and was very happy, till I tried to write my query. I've found out, that it's not possible to use trignometrical functions in built-in sqlite of android.
But then I've got an Idea. I don't really need to compute a distance. The near a point is to another one the smaller difference in their geo coordinates should be.
How could I use this fact? Would it be enough to order saved points by (lat_0 - lat_n)^2 + (lon0-lon_n)^2, where lat_0 and lon_0 are geo coordinates of a given point?
Thank you,
Mur
UPD
So, the best way to get an answer for my question was to test approach I describe above.
It works pretty well but not really exactly compared to exact distance.
So if you just need to compute a distance, this solution is ok, but in my case I also needed to order stations by distance and couldn't use this solution.
My thanks go on John at CashCommons and Philip. Thank you guys
If your points are separated within a city (more or less), that approximation will work fine. The approximation falls apart if you go worldwide, though.
EDIT: Based on Philip's comment below, you should scale one of the components. Germany is about 50 degrees north latitude, so multiplying the longitude by (cos 50 deg) will do better.
Yes. :-) The actual distance is sqrt( (lat_0 - lat_n)^2 + (lon0-lon_n)^2 ) but ordering by (lat_0 - lat_n)^2 + (lon0-lon_n)^2 is sufficient.
Hmm... I'm not sure how that ordering would work? Wouldn't you need a different order for each point to indicate it's neighbors.
The simplest solution is to just iterate through all the points and compute the geometrical distance between the points. For 1000 points this should happen fairly fast.
The most optimized solution (in terms of retrieval speed) is to compute the neighbors of each point when you insert them in the database. For example you can keep the list of ids as a comma separate string and insert in the database?. Then when you need someones neighbors you do directly to them. However this is going to become a pain if you need to insert new points. Basically you'll need to re-compute the neighbors.

Categories

Resources