开发者

Logging GPS locations to file in Android

I have an Android application 开发者_JS百科that periodically gets GPS updates. If I wanted to store the lat, lon, speed, altitude etc. in a file, what is the best practice for doing this?


I'd suggest you use a database - they were made for tasks like this. Here is an Android database tutorial: http://www.vogella.de/articles/AndroidSQLite/article.html


Every time you receive a geo fix, store the location into a file. In the method onLocationChanged call something like

protected void storeLastKnownLocation(Location lastKnownLocation) {
    //save last known location
    SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
    editor.putFloat(LAST_KNOWN_LNG_KEY, (float) lastKnownLocation..getLongitude());
    editor.putFloat(LAST_KNOWN_LAT_KEY, (float) lastKnownLocation.getLatitude());
    editor.commit();
}

When the Activity starts, in the onCreate method you'll retrieve those values as

float lastKnownLng = getPreferences(MODE_PRIVATE).getFloat(LAST_KNOWN_LNG_KEY, 0f);
float lastKnownLat = getPreferences(MODE_PRIVATE).getFloat(LAST_KNOWN_LAT_KEY, 0f);
...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜