无法通过gps或网络找到当前位置

时间:2013-07-24 10:27:11

标签: android networking gps location

我需要找到当前位置,无论是网络还是gps 我通过互联网和stackoverflow搜索,但我找不到这个代码有什么问题 我还在manifest中设置了这个参数:

uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"  
uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"  

所以这是我的代码:

LocationManager locationManager;
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    findCurrentLocation();
    //some other stuff here
}

public void findCurrentLocation()
{           
    boolean networkEnabled = false;
    boolean gpsEnabled = false;

    currentLocation = getLastKnownLocation();

    if(currentLocation == null)
    {
        currentLocation = new Location("sometext");

        locationFindDialog = ProgressDialog.show(this, "Find Your Location", "Please Wait", false);

        try
        {
            networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        }
        catch(Exception e)
        {

        }

        try
        {
            gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        }
        catch(Exception e)
        {

        }

        if((networkEnabled == false) && (gpsEnabled == false))
        {
            locationFindDialog.dismiss();
            showMessage("we need gps or sim card(or network) to find your location");
            //show message
            //you need at least one provider
            return;
        }

        if(networkEnabled)
        {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, networkLocationListener);                
        }

        if(gpsEnabled)
        {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpsLocationListener);
        }
    }

    private final LocationListener networkLocationListener = new LocationListener()
{

    @Override
    public void onLocationChanged(Location location) 
    {

        currentLocation.set(location);          

        //unregister listener
        locationManager.removeUpdates(this);
        locationManager.removeUpdates(gpsLocationListener);         
        locationFindDialog.dismiss();           
    }

    @Override
    public void onProviderDisabled(String provider) 
    {

    }

    @Override
    public void onProviderEnabled(String provider) 
    {

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) 
    {

    }       
};

private final LocationListener gpsLocationListener = new LocationListener()
{

    @Override
    public void onLocationChanged(Location location) 
    {
        currentLocation.set(location);          

        //unregister listener
        locationManager.removeUpdates(this);
        locationManager.removeUpdates(networkLocationListener);         
        locationFindDialog.dismiss();
    }

    @Override
    public void onProviderDisabled(String provider) 
    {

    }

    @Override
    public void onProviderEnabled(String provider) 
    {

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) 
    {

    }

};

private Location getLastKnownLocation()
{
    Location location = null;
    List<String> providers = locationManager.getAllProviders();

    for(String provider : providers)
    {
        location = locationManager.getLastKnownLocation(provider);
        if(location != null) 
        {               
            return location;
        }
    }       
    return null;
}

无论我等多少,应用程序都没有给我这个位置 这段代码有什么问题,以及我如何修复它?

1 个答案:

答案 0 :(得分:0)

最后我找到了解决这个问题的方法,想法是手动找到cellId和lac,然后将它们发送到谷歌秘密API,找到细胞塔的纬度和经度,它不太准确(因为它的细胞塔位置不是用户位置),但对我来说足够了。

请参阅此地址以获取更多信息:
http://sunil-android.blogspot.com/2013/01/convert-celllocation-to-real-location.html