【问题标题】:can not find current location via gps or network无法通过 gps 或网络找到当前位置
【发布时间】:2023-03-22 03:25:01
【问题描述】:

我需要通过网络或 GPS 找到当前位置。
我在互联网和 stackoverflow 中进行了搜索,但我找不到这段代码有什么问题。
我也在清单中设置了这个参数:

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;
}

无论我等了多久,应用程序都不会给我位置。
这段代码有什么问题,我该如何解决?

【问题讨论】:

  • 你是否使用了互联网用户权限?
  • @Araibkarim:不,有必要吗?
  • 通过查看此处:developer.android.com/reference/android/location/…, long, float, android.location.LocationListener) 我可以看到您没有给供应商(GPS 或网络)足够的时间来刷新位置.尝试locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2*1000*60, 0, networkLocationListener);//2 mins 并将 GPS 时间设置为最少 5 分钟。
  • @g00dy: 非常感谢,现在它可以通过 GPS 找到位置,但它无法通过网络(simcard)找到位置,我该如何解决这个问题?
  • 你的if语句(if(networkEnabled)if(gpsEnabled))是一个接一个,放在if(){} else{},这样你就只能得到一个。

标签: android networking gps location


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多