简体   繁体   English

Android GPS获取当前位置

[英]Android GPS get current location

I developed a simple Android application and in this simple application I want to get GPS Latitued and Longitued data, it works correctly in my emulator but when I installed on real Android device it does not work. 我开发了一个简单的Android应用程序,在这个简单的应用程序中我想获得GPS Latitued和Longitued数据,它在我的模拟器中正常工作但是当我安装在真正的Android设备上时它不起作用。 Please help me to solve this problem and the follow is my code. 请帮我解决这个问题,以下是我的代码。 Thanks in advance 提前致谢

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);
        }
    });
}

}

Screen shot from emulator: 模拟器的屏幕截图: 在此输入图像描述

Screen shot from mobile: 移动屏幕截图: 在此输入图像描述

You're requesting only GPS... Your device needs to make contact with satellites! 您只需要GPS ...您的设备需要与卫星联系! In space! 在太空! Walk outside and test it! 走到外面测试吧! This is a common issue when testing code that uses only GPS, it doesn't work very well indoors. 在测试仅使用GPS的代码时,这是一个常见问题,但在室内使用效果不佳。

Also, you shouldn't use "gps" hard coded, you should use: 另外,你不应该使用硬编码的“gps”,你应该使用:

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

As for using the FusedLocationProviderAPI, you will lose some accuracy, but it does offer much improved battery performance, since it relies heavily on Network Location, and only uses GPS if absolutely necessary. 至于使用FusedLocationProviderAPI,你会失去一些准确性,但它确实提供了大大提高的电池性能,因为它严重依赖于网络位置,并且只在绝对必要时才使用GPS。 For a complete example of how to use it, see here: https://stackoverflow.com/a/30255219/4409409 有关如何使用它的完整示例,请参见此处: https//stackoverflow.com/a/30255219/4409409

add to your code this. 添加到您的代码中。 when this app run in indoor return last known location. 当这个应用程序在室内运行返回最后的已知位置。

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

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

The Google Play services location APIs are preferred over the Android framework location APIs (android.location) as a way of adding location awareness to your app. Google Play服务位置API优先于Android框架位置API(android.location),作为向您的应用添加位置感知的一种方式。

You can find the example how to use it right here. 您可以在此处找到如何使用它的示例

PS Don't forget to check if permissions granted on your device. PS不要忘记检查您的设备上是否授予了权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM