安卓GPS获取当前位置

6

我开发了一个简单的Android应用程序,想要在其中获取GPS经纬度数据。它在模拟器中可以正常工作,但是当我在真实的Android设备上安装时却无法正常工作。请帮助我解决这个问题。以下是我的代码。谢谢。

public class getLocation extends AppCompatActivity {

private Button btnGetLocation;
private TextView textGPS;
private LocationManager locationManager;
private LocationListener locationListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_get_location);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    btnGetLocation = (Button) findViewById(R.id.btnGetLocation);
    textGPS = (TextView) findViewById(R.id.textViewGps);
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            textGPS.setText("lat: "+location.getLatitude()+" long: "+location.getLongitude());

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {
            startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));

        }
    };

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.INTERNET
            },10);
            return;
        }
    }else{
        configureButton();
    }

}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

    switch (requestCode){
        case 10:
            if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                configureButton();
            return;
    }
}

private void configureButton() {

    btnGetLocation.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            locationManager.requestLocationUpdates("gps", 0, 0, locationListener);
        }
    });
}

}

模拟器截图: 在此输入图片描述

移动设备截图: 在此输入图片描述


1
你只请求了GPS... 你的设备需要与卫星建立联系!走到室外测试一下吧!另外,你不应该硬编码使用"gps",而应该使用locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); - Daniel Nugent
1
谢谢,我走到户外它正常工作。 - Gulmuhammad Akbari
3个回答

5

您只请求了GPS... 您的设备需要与卫星建立联系!在太空中!走到户外测试吧!

当测试仅使用GPS的代码时,这是一个常见问题,它在室内工作效果不佳。

另外,您不应该硬编码使用"gps",而应该使用:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

使用FusedLocationProviderAPI会损失一些精度,但它确实可以提供更好的电池性能,因为它主要依赖于网络位置,并仅在绝对必要时使用GPS。有关如何使用它的完整示例,请参见这里: https://dev59.com/Zl0a5IYBdhLWcg3wfY06#30255219


它的功能很好,但是当我想将纬度和经度保存到数据库时,我无法获取它们。 - Gulmuhammad Akbari
@GulMuhammadAkbari 你还应该请求网络位置,这通常会更快地获取到位置(当附近有WiFi网络时)。总之,在将位置保存到数据库之前,你只需要等待直到获取到位置,然后在 onLocationChanged() 回调中保存到数据库。 - Daniel Nugent

2

在您的代码中添加以下内容。当此应用程序在室内运行时,返回上次已知位置。

        // Get the location from the given provider
        Location location = locationManager
                .getLastKnownLocation(provider);

        locationManager.requestLocationUpdates(provider, 1000, 1, this);

1

在为应用程序添加位置感知的方式上,Google Play服务位置API优于Android框架位置API(android.location)。

您可以在这里找到如何使用它的示例。

P.S. 不要忘记检查设备上是否授予权限。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接