Android 存储 GPS 纬度和经度

发布于 2024-12-11 06:15:45 字数 1821 浏览 0 评论 0原文

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    db = new DbAdapter(getBaseContext());
    db.open();

    android_id = Secure.getString(getBaseContext().getContentResolver(), Secure.ANDROID_ID);
    locmgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Toast.makeText(getBaseContext(), "Waiting for location..." , Toast.LENGTH_SHORT).show();
}

  LocationListener onLocationChange=new LocationListener() {
        public void onLocationChanged(Location loc) {
            //sets and displays the lat/long when a location is provided
            String latlong = "Lat: " + loc.getLatitude() + " Long: " + loc.getLongitude();  
            Toast.makeText(getBaseContext(), latlong, Toast.LENGTH_SHORT).show();
                 db.insertGPSCoordinates(android_id, Double.toString(loc.getLatitude()), Double.toString(loc.getLongitude()));
        } 

        public void onProviderDisabled(String provider) {
        // required for interface, not used
        }

        public void onProviderEnabled(String provider) {
        // required for interface, not used
        }

        public void onStatusChanged(String provider, int status,
        Bundle extras) {
        // required for interface, not used
        }
    };
    //pauses listener while app is inactive
    @Override
    public void onPause() {
        super.onPause();
        locmgr.removeUpdates(onLocationChange);
    }

    //reactivates listener when app is resumed
    @Override
    public void onResume() {
        super.onResume();
        locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,10000.0f,onLocationChange);
    }

这段代码对我有用,但它只获得一次经度和纬度。我想知道“LocationListener”如何工作?我启动应用程序并散步,它只存储了一组。我做错了什么...

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    db = new DbAdapter(getBaseContext());
    db.open();

    android_id = Secure.getString(getBaseContext().getContentResolver(), Secure.ANDROID_ID);
    locmgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Toast.makeText(getBaseContext(), "Waiting for location..." , Toast.LENGTH_SHORT).show();
}

  LocationListener onLocationChange=new LocationListener() {
        public void onLocationChanged(Location loc) {
            //sets and displays the lat/long when a location is provided
            String latlong = "Lat: " + loc.getLatitude() + " Long: " + loc.getLongitude();  
            Toast.makeText(getBaseContext(), latlong, Toast.LENGTH_SHORT).show();
                 db.insertGPSCoordinates(android_id, Double.toString(loc.getLatitude()), Double.toString(loc.getLongitude()));
        } 

        public void onProviderDisabled(String provider) {
        // required for interface, not used
        }

        public void onProviderEnabled(String provider) {
        // required for interface, not used
        }

        public void onStatusChanged(String provider, int status,
        Bundle extras) {
        // required for interface, not used
        }
    };
    //pauses listener while app is inactive
    @Override
    public void onPause() {
        super.onPause();
        locmgr.removeUpdates(onLocationChange);
    }

    //reactivates listener when app is resumed
    @Override
    public void onResume() {
        super.onResume();
        locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,10000.0f,onLocationChange);
    }

This code is working for me but it gets Lats and Longs only once. I would like to know how "LocationListener" works ? I fired up the app and took a walk, it only stored 1 set. What am I doing wrong...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我是男神闪亮亮 2024-12-18 06:15:45
public void requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)

仅当您的位置差异为 10000m、10km 时,您的应用程序才会更新位置变化。尝试使用:

locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,onLocationChange);
public void requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)

Your application only will update location change when your difference of position be of 10000m, 10km. Try with:

locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,onLocationChange);
糖果控 2024-12-18 06:15:45

您可以将 GPS 事物放入计时器线程中以强制其更新。不同的设备对 GPS 侦听器的行为有所不同。

还要确保您的 insertDBCooperatives 不仅仅是每次都对同一行进行更新

You can put the GPS thing in a timer thread to force it to update. Different devices behave differently with the GPS listener.

Also make sure your insertDBCoordinates isn't simply doing an update to the same row every time

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文