如何在Android上获取当前位置?

时间:2011-12-07 06:32:50

标签: java android

我使用此代码获取当前位置:

LocationManager lm = (LocationManager) this
        .getSystemService(Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);

/*
 * Loop over the array backwards, and if you get an accurate location,
 * then break out the loop
 */
Location l = null;

for (int i = providers.size() - 1; i >= 0; i--) {

    l = lm.getLastKnownLocation(providers.get(i));
    if (l != null)
        break;
}

if (l==null) {
    return;
}
Toast.makeText(this, String.valueOf(l.getLatitude()), Toast.LENGTH_LONG).show(); 

但我从未看到过纬度,因为l总是为空。我的清单文件包含ACCESS_FINE_LOCATION和INTERNET权限。我哪里弄错了?

4 个答案:

答案 0 :(得分:3)

看看这个出色的解释:

http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html

目前,您只是向一些可用的提供商询问最后一个已知位置,并在第一个答案中打破。这可能不是您喜欢的提供商。

答案 1 :(得分:0)

只需调用LocationManager的requestLocationUpdates方法,并实现LocationListener来处理位置更新。

http://developer.android.com/reference/android/location/LocationManager.html

答案 2 :(得分:0)

我认为实施位置的最佳开源项目是谷歌代码上的foursquare: http://code.google.com/p/foursquared/

相关代码位于com.joelapenna.foursquared.loaction.BestLoactionListener

你可以看看他们如何处理Loaction,以及如何更新当前的loaction,这对我有很大帮助。

答案 3 :(得分:0)

以下代码适合我。我已经使用GPS以及网络服务来确定设备的当前位置。

private void findCurrentLocation()
{
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (locationManager == null)
    {
        Toast.makeText(CityActivity.this, "Location Manager Not Available",
        Toast.LENGTH_SHORT).show();
        return;
    }
    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (location == null)
        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    if (location != null)
    {
        lat = location.getLatitude();
        lng = location.getLongitude();
        myFunction(location);
    }
    else
    {
        locationListener = new LocationListener()
        {
            @Override
            public void onStatusChanged(String arg0, int arg1, Bundle arg2)
            {;}

            @Override
            public void onProviderEnabled(String arg0)
            {;}

            @Override
            public void onProviderDisabled(String arg0)
            {;}

            @Override
            public void onLocationChanged(Location l)
            {
                location = l;
                locationManager.removeUpdates(this);
                if (l.getLatitude() == 0 || l.getLongitude() == 0)
                {;}
                else
                {
                    lat = l.getLatitude();
                    lng = l.getLongitude();
                    myFunction(location);
                }
            }
        }
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, f, locationListener);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 10f, locationListener);
        locationtimer = new CountDownTimer(30000, 5000)
        {
            @Override
            public void onTick(long millisUntilFinished)
            {
                if (location != null)
                    locationtimer.cancel();
            }

            @Override
            public void onFinish()
            {
                if (location == null)
                {;}
            }
        };
        locationtimer.start();
    }
}