问题
I am doing one application for Track routes of bike.
I am fetching all the latitude and longitude per second.
Now I want to store that latitude and longitudes in my local database or on server. But the problem is data becomes very large. So I want to store that data with optimization, means I want same result but I want to reduce data size.
Please help me, if anybody knows any algorithm or anything.
回答1:
If you want to do this device-side, there is an open-source maps utility library created by the Android Maps team that includes a method to encode a set of LatLng
objects into an encoded Polyline string.
Android Maps Utils library project: https://github.com/googlemaps/android-maps-utils
Sample app that shows how to use the library: https://github.com/googlemaps/hellomap-android
Use this library in your project, then call the PolyUtil.encode()
method to encode a set of LatLng
objects into a String, which you can store in your database:
https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/PolyUtil.java#L68
Then, call PolyUtils.decode()
method to decode a set of LatLng objects from the same String:
https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/PolyUtil.java#L29
回答2:
The easiest and more practical solution is to reduce the frequency of location updates:
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,refresh_freq, 0, locListener);
Try to update your position each 3 seconds (refresh_freq = 3000) instead of each one second (refresh_freq = 1000), this will reduce your data to the third. (Considering that the bike speed is not to high, this will not affect your precision)
EDIT: According to your comment in the previous answer, I can understand that you need only to get the speed, and distance. So why don't you only calculate them instantly and overwrite them in each update:
distance = (distance + old_location.distanceTo(new_location));
average_speed = (distance/(total_time/1000))*3.6;
instant_speed = (((old_location.distanceTo(new_location))/refresh_freq)/1000)
if (instant_speed>max_speed){
max_speed = instant_speed;
}
Insert all of these lines in your onLocationChanged (Location loc) method. Now you won't need to store your positions in a database.
来源:https://stackoverflow.com/questions/17810805/android-google-map-latitude-and-longitude-compression