6

对不起我的英语不好。我正在尝试从 GPS 获取单个位置以放置全局变量纬度、经度。GPS 已打开,但在从 GPS 检索数据之前活动会继续进行。

换句话说,我的需求......方法 getCurrentLocation() 只有在找到一个位置并且填充了经度和纬度变量时才能完成,所以我可以在其他方法中使用它们。我知道...用户必须等待...我会解决这个问题,在屏幕上显示一些东西。我应该怎么办?谢谢

我想我正在跳过停止在某个地方收听 GPS。哪里比较好?

代码如下:

//Method to retrieve coordinates
public void getCurrentLocation() {
    //Use GPS if possible
    if(manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        //assign Listener to GPS
        manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
        Toast.makeText(this, LocationManager.GPS_PROVIDER, Toast.LENGTH_SHORT).show();
    }
    else if(manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){//toherwise, use NETWORK
        //assign Listener to NETWORK
        manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
        Toast.makeText(this, LocationManager.NETWORK_PROVIDER, Toast.LENGTH_SHORT).show();
    }
}

//Class to store the location recived in two variables
final class MyLocationListener implements LocationListener {

    @Override
    public void onLocationChanged(Location location) {
        //coordinates storing
        latitude = String.valueOf(location.getLatitude());
        longitude = String.valueOf(location.getLongitude());
        Toast.makeText(getApplicationContext(), latitude + longitude, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
    }
}
4

4 回答 4

2

If you want android activity to wait until GPS location obtained, you might try to use AsyncTask for that. See Android find GPS location once, show loading dialog for the answers. Basically in onPreExecute you can start dialog( it starts before the doInBackground is called). It means you are waiting till the time you can location and showing the dialog. Then in doInBackground you can get the location. After that finishes onPostExecute is called. You can stop is from inside onPostExecute. You can check if the location is not null and then call some other function from inside onPostExecute also if you want.

This might be one way. You can learn a basic example from AsyncTask Android example . You can also start by reading the documentation here.

Some other similar helpful questions:

Wait for current location - GPS - Android Dev

getting location instantly in android

Hope this helps.

于 2013-08-23T02:25:47.077 回答
2

使用 LocusService 3rd 方库! Goto LocusService

通过调用简单的函数,您将能够轻松获得当前的 GPS 或网络位置....

要解决您的问题,请使用此代码!

LocusService locusService = new LocusService(this);
locusService.startRealtimeGPSListening(1000);   //Set intervel

locusService.setRealTimeLocationListener(new LocusService.RealtimeListenerService() {
        @Override
        public void OnRealLocationChanged(Location location) {
                if(location!=null){
                  //Do your Stuff here
                  finish();
                }
        }
    });
于 2018-04-11T13:10:33.443 回答
1

我遇到了同样的问题,

通过使用 Activity 生命周期方法解决了它。


onCreate(Bundle b)1)方法内的GPS捕捉功能

2)onStart()方法中需要GPS定位的意图或功能。


因此,活动从onCreate(bundle)将捕获 GPS 位置的方法开始。这里 GPS 高度、纬度、经度值将分配给全局变量。

执行该onStart()方法后,将调用 GPS 位置全局变量以执行必要的操作。


希望这对你有帮助

于 2018-03-30T17:14:02.533 回答
1

最好使用 getLastKnownLocation() 方法。这是一个代码示例:

将您的经纬度设为全局变量

double longitude;
double latitude;
protected void onCreate(Bundle savedBundle) {
       super.onCreate(savedBundle);
       setContentView(R.layout.activity_receipt_result);
       Objects.requireNonNull(getSupportActionBar()).setElevation(0);

       locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);

       if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    Activity#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for Activity#requestPermissions for more details.
        ActivityCompat.requestPermissions(this,new String[] {Manifest.permission.ACCESS_FINE_LOCATION},1);
    }else{
        Location location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
        latitude = location.getLatitude();
        longitude = location.getLongitude();
        Log.d("Location: ", "long-lat" + longitude + "-"+ latitude);
    }
}

在您可以在其余代码中使用经度和纬度变量之后。

于 2019-11-30T06:51:59.000 回答