无法获取用户位置的经度和纬度

时间:2016-01-30 21:29:25

标签: android android-studio

目前,我的manifest.xml设置为以下访问位置的权限:

load"a.txt";

我的mainActivity.java文件有以下代码用于LocationManager和requestLocationUpdates():

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

和@Override方法:

    locationmanager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

    locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationlistener);

代码没有错误。但是,对于经度和纬度,上述代码的结果均为0.00。

PS:只从MainActivity文件中选取特定的代码段。如果需要更多细节,将准备编辑问题。

1 个答案:

答案 0 :(得分:0)

确保locationlistener的定义如下所示。在mainActivity.java

中添加此课程
class locationlistener implements LocationListener {


            @Override
            public void onLocationChanged(Location location) {

                   //play with location

            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {
                switch (i) {
                    case LocationProvider.AVAILABLE:
                        Log.i("LocationListener", "GPS Available");
                        break;
                    case LocationProvider.OUT_OF_SERVICE:
                        Log.e("LocationListener", "GPS Out Of Service");
                        break;
                    case LocationProvider.TEMPORARILY_UNAVAILABLE:
                        Log.w("LocationListener", "GPS Temporarily Unavailable");
                        break;
                    default:
                        Log.e("LocationListener", "Status changed");
                }

            }

            @Override
            public void onProviderEnabled(String s) {
                Log.e("Location", "Location was enabled");
            }

            @Override
            public void onProviderDisabled(String s) {
                Log.e("Location", "Location was disabled");
            }
        }