在 Android 中获取更新的位置

发布于 2024-09-06 02:19:51 字数 2424 浏览 0 评论 0原文

我使用下面显示的代码在每次单击按钮时获取位置的更新值。当我的活动恢复时,我每秒都会收到更新,因此当我调用 getLastKnownLocation 时,我希望得到一个在最后一秒更新的位置。

这是正确的方法吗?

我希望每次执行“geo fix”命令时都会触发 onLocationChanged 事件(或者最多在 1 秒后触发,因为我请求每 1 秒更新一次),但它仅在第一次触发。为什么?

欢迎任何帮助/建议!

谢谢

package org.digitalfarm.atable;

...

public class Atable extends Activity {
    private Button mSearchButton;
    private TextView mytext;
    private LocationManager locationManager;    


    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);   

        setContentView(R.layout.main);

        mSearchButton = (Button)this.findViewById(R.id.button);

        mytext = (TextView) findViewById(R.id.dude);

        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        final Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);               


        mSearchButton.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {

                  String provider = locationManager.getBestProvider(criteria, true);

                  Location location = locationManager.getLastKnownLocation(provider);             

          }
        });
    }

     //Start a location listener
    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();   
            mytext.setText(latlong);
        }

        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();
        locationManager.removeUpdates(onLocationChange);
    }

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

I'm using the code shown below to get an updated value for location every time a button is clicked. When my activity is resumed I get an update every second, so that when I call getLastKnownLocation I expect to have a location that have been updated in the last second.

Is that the correct way to do that?

I would expect the onLocationChanged event to be triggered every time I execute a 'geo fix' command (or max after 1s since I request update every 1s), but it's only triggered the first time. Why?

Any help/suggestion welcome!

Thanks

package org.digitalfarm.atable;

...

public class Atable extends Activity {
    private Button mSearchButton;
    private TextView mytext;
    private LocationManager locationManager;    


    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);   

        setContentView(R.layout.main);

        mSearchButton = (Button)this.findViewById(R.id.button);

        mytext = (TextView) findViewById(R.id.dude);

        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        final Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);               


        mSearchButton.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {

                  String provider = locationManager.getBestProvider(criteria, true);

                  Location location = locationManager.getLastKnownLocation(provider);             

          }
        });
    }

     //Start a location listener
    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();   
            mytext.setText(latlong);
        }

        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();
        locationManager.removeUpdates(onLocationChange);
    }

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

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

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

发布评论

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

评论(1

南巷近海 2024-09-13 02:19:51

requestLocationUpdates 中指定的时间是最多发生一次更新的最短可能时间间隔。因此,您每秒最多注册一次更新,但实际更新可能需要更多时间才能发生,而且,对此不提供任何保证。

从文档中:

minTime - 通知的最小时间间隔,以毫秒为单位。此字段仅用作节省电量的提示,位置更新之间的实际时间可能大于或小于此值。

尝试将该时间间隔设置为 0,如果没有其他问题(尽管对我来说一切似乎都很好),您应该开始“尽可能频繁地”获取更新。

The time, specified in requestLocationUpdates is the shortest possible time interval for which a maximum of one update will occur. So you have registered for at most one update every second, but the actual update may take more that that to occur, and, furthermore, no guarantee is given for that.

From the documentation:

minTime - the minimum time interval for notifications, in milliseconds. This field is only used as a hint to conserve power, and actual time between location updates may be greater or lesser than this value.

Try to set that time interval to 0 and if there are no other problems (although it all seems fine to me), you should start getting the updates "as frequently as possible".

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