我正在尝试获取
android的经度和纬度,这是我的代码:
LocationManager locationManager = (LocationManager) this_activity.getSystemService(Context.LOCATION_SERVICE); LocationListener locationListener = new MyLocationListener(); locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER,5000,10,locationListener); public class MyLocationListener implements LocationListener { @Override public void onLocationChanged(Location loc) { longitude = Double.toString(loc.getLongitude()); latitude = Double.toString(loc.getLatitude()); } @Override public void onProviderDisabled(String provider) {} @Override public void onProviderEnabled(String provider) {} @Override public void onStatusChanged(String provider,int status,Bundle extras) {} }
清单Permissions:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
经度和纬度我总是得到0.0秒
解决方法
除了@AshishPedhadiya提到你只听GPS提供商的权限.
LocationListener locationListener = new MyLocationListener(); locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER,locationListener);
GPS无法在里面工作.因此,当您测试应用程序时,它几乎总是会返回(0,0)位置.您可能也想收听NETWORK_PROVIDER,如果没有GPS位置,它会估计来自手机信号塔和WiFi点的位置.
locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER,locationListener);