android - Android获取经纬度的方法

标签 android android-location

我想找到我当前位置的经度和纬度,但我总是得到NULL

double lat = loc.getLatitude(); //Cause the result is null, so can't know longitude and latitude
double lng = loc.getLongitude();


 LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);  
    String provider = LocationManager.GPS_PROVIDER;  
    Location location = locationManager.getLastKnownLocation(provider); //result is null!

这是获取 GPS 状态的代码。它工作正常:

public void onGpsStatusChanged(int event) { // get the GPS statue  
                LocationManager locationManager = (LocationManager) GpsActivity.this.getSystemService(Context.LOCATION_SERVICE);  
                GpsStatus status = locationManager.getGpsStatus(null);  
                String satelliteInfo = updateGpsStatus(event, status);
                myTextView.setText(satelliteInfo);//work fine ,searched satellite:16
    }
    };
        private String updateGpsStatus(int event, GpsStatus status) {  
            StringBuilder sb2 = new StringBuilder("");  
            if (status == null) {  
                sb2.append("searched satellite number" +0);  
            } else if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {  
                int maxSatellites = status.getMaxSatellites();  
                Iterator<GpsSatellite> it = status.getSatellites().iterator();  
                numSatelliteList.clear();  
                int count = 0;  
                while (it.hasNext() && count <= maxSatellites) {  
                    GpsSatellite s = it.next();  
                    numSatelliteList.add(s);  
                    count++;  
                }  
                sb2.append("searched satellite number:" + numSatelliteList.size());  
            }
            return sb2.toString();  
        }

最佳答案

getLastKnownLocation() 仅返回最近的 GPS 定位(如果可用)。您需要实现 LocationListener 并使用 LocationManager#requestLocationUpdates()获取新位置。


基本实现:

public class Example extends Activity implements LocationListener {
    LocationManager mLocationManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

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

        Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if(location != null) {
            // Do something with the recent location fix
            //  otherwise wait for the update below
        }
        else {
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        if (location != null) {
            Log.v("Location Changed", location.getLatitude() + " and " + location.getLongitude());
        }
    }
    // etc..
}

关于android - Android获取经纬度的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13371932/

相关文章:

android - Fused Location Provider - 非固定 wifi

android - 如何在 getMyLocationAddress 方法中的 onLocationChanged 中访问我的当前位置?

java - 想要将 Android 设备应用程序(Java 编程)连接到 NetBeans 8.0

list 中的 Android 应用程序与 Activity

android - 如何将图像导入或复制到 Android Studio 中的 "res"文件夹?

java - 崩溃 onLocationChanged() Android

android - 从 Android 的 LocationManager 请求 getLastKnownLocation 时的空指针

android - 位置管理器删除更新权限

java - 如何在 Canvas 剪辑中获取内接矩形

android - 在EditText后隐藏提示不为null