在哪里保存 Android GPS(纬度,经度)点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3604849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where to save Android GPS (Latitude , Longitude) points?
提问by Jovan
On onLocationChanged event I want to save my GPS route (Latitude , Longitude). Later I want to load this data and draw the route.
在 onLocationChanged 事件中,我想保存我的 GPS 路线(纬度、经度)。后来我想加载这些数据并绘制路线。
Who is the best way to do this, by using some array type (and save or load by using database) or XML files or something else?
谁是通过使用某种数组类型(并使用数据库保存或加载)或 XML 文件或其他方式来做到这一点的最佳方法?
Thanks.
谢谢。
回答by Juan T
The best way of storing double values in SharedPreferences without losing precision is:
在 SharedPreferences 中存储双精度值而不丢失精度的最佳方法是:
Transform to bit representation to store it as long:
prefsEditor.putLong("Latitude", Double.doubleToLongBits(location.getLatitude()));
To retrieve, transfrom from bit representation to double:
double latitude = Double.longBitsToDouble(prefs.getLong("Latitude", 0);
转换为位表示以将其存储为长时间:
prefsEditor.putLong("Latitude", Double.doubleToLongBits(location.getLatitude()));
要检索,从位表示转换为双:
double latitude = Double.longBitsToDouble(prefs.getLong("Latitude", 0);
However, I think that if you want to store a big amount of points is better using a SQLite database than saving each coordinate in a Key-Value pair in SharedPreferences or serialize the array in XML and write it to a file or to SharedPreferences. The database offers you the advantage of loading in memory only the points within the area you are displaying in the map, so you can save memory.
但是,我认为,如果您想存储大量点,使用 SQLite 数据库比将每个坐标保存在 SharedPreferences 中的键值对中或序列化 XML 中的数组并将其写入文件或 SharedPreferences 更好。该数据库为您提供了一个优势,即仅将您在地图中显示的区域内的点加载到内存中,因此您可以节省内存。
回答by Reed
Correct me if I'm wrong, but it might be better to convert the double values to strings and then whenever you call them convert them back to doubles. Sounds like a pain, but oracle's website (http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html) says:
如果我错了,请纠正我,但最好将双精度值转换为字符串,然后在调用它们时将它们转换回双精度值。听起来很痛苦,但 oracle 的网站 ( http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html) 说:
"use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency"
“如果您需要在大型浮点数数组中节省内存,请使用浮点数(而不是双精度数)。这种数据类型不应用于精确值,例如货币”
prefEditor.putString("Latitude", Double.valueOf(loc.getLatitude()).toString());
Then, to call the value
然后,调用值
String latitudeString = pref.getString("Latitude", "0");
double latitude = Double.parseDouble(latitudeString);
It definitely seems like more of a nuisance, but seems also more likely to return the exact value that you saved, also, instead of something slightly off. And I suppose that would depend on how completely accurate you need the values to be.
这绝对看起来更麻烦,但似乎也更有可能返回您保存的确切值,而不是稍微偏离的值。我想这将取决于您需要值的准确程度。
Though, as far as using an array, I'm clueless because I've never needed to use them and have no experience with them.
不过,就使用数组而言,我一无所知,因为我从来不需要使用它们,也没有使用它们的经验。
回答by magicman
Use SharedPreferences and Editor.
使用 SharedPreferences 和 Editor。
Check out this open source code, it will probably help you a lot: OsmandSettings.java
看看这个开源代码,它可能会对你有很大帮助: OsmandSettings.java
I'll explain the important parts of the code:
我将解释代码的重要部分:
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
// These settings are stored in SharedPreferences, replace com.osmand.settings
// with your own package name, or whatever String you want.
public static final String SHARED_PREFERENCES_NAME = "com.osmand.settings";
public static final String LATITUDE = "latitude";
public static final String LONGITUDE = "longitude";
To write to SharedPreferences:
写入 SharedPreferences:
public void onLocationChanged(Location location){
SharedPreferences prefs = Context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_WORLD_READABLE);
Editor editor = prefs.edit();
//Save it as a float since SharedPreferences can't deal with doubles
edit.putFloat(LATITUDE, (float) Location.getLatitude());
edit.putFloat(LONGITUDE, (float) Location.getLongitude());
edit.commit();
}
To read from SharedPreferences:
从 SharedPreferences 读取:
public void onLocationChanged(Location location){
SharedPreferences prefs = Context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_WORLD_READABLE);
double lat = (double)prefs.getFloat(LATITUDE, 0);
double lon = (double)prefs.getFloat(LONGITUDE, 0);
}
回答by Rakesh Gondaliya
I also had same problem while i was doing app on Map. In fact good question i would say.
我在 Map 上做应用程序时也遇到了同样的问题。事实上,我会说的好问题。
you can do like this.
你可以这样做。
1> On Click event on Map u have to show latitude and longitude of that point. here i m attaching code how u can find lat and long.
1> 在地图上的点击事件你必须显示该点的纬度和经度。我在这里附上代码,你如何找到经纬度。
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
//---when user lifts his finger---
if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
Toast.makeText(getBaseContext(),
p.getLatitudeE6() / 1E6 + "," +
p.getLongitudeE6() /1E6 ,
Toast.LENGTH_SHORT).show();
}
return false;
}
2> Now u can store this lat and long in KEY,value pair.
here u have to take value1 as lat and value 2 as long. and pass the object of
value(value1,value2) to key,value pair.
2> 现在你可以将这个经纬度存储在 KEY,value 对中。在这里,您必须将 value1 作为 lat 并将 value 2 作为长度。并将
value(value1,value2)的对象传递给 key,value 对。
3> Now u have the list of all the lat and long through which you have passed. then draw route from 1st index to last index.
3> 现在你有你经过的所有纬度和经度的列表。然后绘制从第一个索引到最后一个索引的路线。
This is what i think the best way to implement this app.
这是我认为实现此应用程序的最佳方式。
回答by Charles Mack
Think about the precision (not accuracy) you need to represent the data. Would a couple inches be close enough? Most handheld GPS receivers are lucky to be good to a couple meters. One degree-second of longitude at the equator moves you about 100 feet. If you use a signed 32-bit integer to represent your latitudes and longitudes, LatE6 & LonE6, you could store the largest longitude there is as +/-180,000,000 and the least significant digit is:
考虑表示数据所需的精度(而不是准确度)。几英寸会足够近吗?大多数手持式 GPS 接收器很幸运能达到几米。赤道经度 1 度秒会使您移动大约 100 英尺。如果您使用有符号的 32 位整数来表示您的纬度和经度,即 LatE6 和 LonE6,您可以将最大经度存储为 +/-180,000,000,最低有效数字为:
(1 / 1E6) * (3600 sec/deg) * (100 feet/sec) = 0.36 feet or 4 inches.
(1 / 1E6) * (3600 秒/度) * (100 英尺/秒) = 0.36 英尺或 4 英寸。
I've got to imagine my Garmin uses something like this, not a float with only 24 bits of precision and certainly not a 64 bit double. Conversion between integer and the double shouldn't cost much. You could even pack a lat/lon pair into a long.
我必须想象我的 Garmin 使用这样的东西,不是只有 24 位精度的浮点数,当然也不是 64 位双精度数。整数和双精度之间的转换不应该花费太多。你甚至可以把一个纬度/经度组合成一个长的。