Android - Lat long值为0.0和0.0

时间:2015-03-31 23:28:21

标签: android geolocation latitude-longitude

我正在尝试获取设备的应用程序位置。代码为纬度和经度返回0.0。我需要改变什么? Stack Overflow不会让我在没有另一句话的情况下发布这个问题。希望这么多文字就够了。

//Getting the location
        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll = new LocationListener() {

            @Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
                if(location !=null){
                    plat = location.getLatitude();
                    plong = location.getLongitude();
                }

            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub

            }


        };
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
        Log.d("lat: ",Double.toString(plat));
        Log.d("long: ",Double.toString(plong)); 

2 个答案:

答案 0 :(得分:1)

RequestLocationUpdates是异步的 - 需要时间。在调用onLocationChanged之前,plat和plong的值不会准备就绪。所以在那之前你不能使用它们。

答案 1 :(得分:0)

移动

Log.d("lat: ",Double.toString(plat));
Log.d("long: ",Double.toString(plong));

public void onLocationChanged(Location location)

并确保已启用GPS