【问题标题】:general class for get current location by gps,network and not connected mode and stop after found通过 gps、网络和未连接模式获取当前位置并在找到后停止的通用类
【发布时间】:2014-04-11 21:49:46
【问题描述】:

当然我的问题是重复的。但我无法为我的解决方案找到好的答案。 我想编写一个自定义通用类,它可以通过位置管理器找到当前位置,并在设备未启用 gps 或网络时自己检测 gps 启用或网络启用或使用缓存和存储的位置。最重要的是在以任何方式找到第一个位置后停止工作。但我为它做了这些工作:1-创建两个位置监听器类(GPSListener 和 networkListner):

public class GPSListener implements LocationListener {

LocationManager locationManager=null;
public Boolean found=false;
public Location mainLocation=null;
@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    found=true;
    mainLocation= location;
}

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

}

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

}

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

}



}

public class NetworkListener implements LocationListener {

LocationManager locationManager=null;
public Boolean found=false;
public Location mainLocation=null;
@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    found=true;
    mainLocation=location;

}

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

}

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

}

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

}

然后我在我的活动中使用这些类:

LatLng result = null;
    final LocationManager locationManager = (LocationManager) this
            .getSystemService(Context.LOCATION_SERVICE);


    if (DeviceHelper.IsGPSEnabled()) {

        GPSListener gpsListener = new GPSListener();

        // if(gpsListener.found)
        // {
        if (gpsListener.mainLocation != null) {
            ToastHelper.Show(this,
                    "GPS : " + gpsListener.mainLocation.toString());
            gpsListener = null;
            result = new LatLng(gpsListener.mainLocation.getLatitude(),
                    gpsListener.mainLocation.getLongitude());
        }
        // }
        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 0, 0, gpsListener);
    }

    else if (DeviceHelper.HasConnect()) {

        NetworkListener netListener = new NetworkListener();

        // if(netListener.found)
        // {
        if (netListener.mainLocation != null) {
            ToastHelper.Show(this,
                    "NETWORK : " + netListener.mainLocation.toString());
            netListener = null;
            result = new LatLng(netListener.mainLocation.getLatitude(),
                    netListener.mainLocation.getLongitude());
        }
        // }
        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 0, 0, netListener);
    } else {
        ;
        Location loc = LocationHelper.getLastKnownLoaction(false);
        if (loc != null) {
            ToastHelper.Show(this, "LAST : " + loc.toString());
            result = new LatLng(loc.getLatitude(), loc.getLongitude());
        }
    }


    if (result != null) {
        LogHelper.Add("LL", String.valueOf(result.latitude));
        LocationHelper.MyLatLng = result;
        StaticObjects.mainMap.addMarker(new MarkerOptions().position(result));
        MapHelper.CameraGo( result, 15);
    } else {
        LogHelper.Add("ERR:", "NULL");

}

我知道这行不通,因为在我显示找到的位置之前找不到经理的好时机。完全我想写一个通用类来查找当前位置,当找到第一个位置时可以停止。

【问题讨论】:

  • 你有什么问题?
  • 如何编写一个通用类来通过 gps、网络和断开模式查找我的位置,并在提供者找到第一个位置时停止管理器?

标签: android location locationmanager


【解决方案1】:

首先,您不需要 2 个类来收听 GPS-Glonass 和网络更新。您可以只使用一个对象作为两个提供者的侦听器——因为可以从Location 对象轻松获得提供者:

if(location.getProvider().equals(LocationManager.GPS_PROVIDER)){
    // it is GPS
} else if (location.getProvider().equals(LocationManager.NETWORK_PROVIDER)){
    // it is Network
}

其次,要了解提供者的状态,您应该实现LocationListeneronStatusChanged() 回调。如果在您订阅 LocationListener 时提供者不可用,则立即调用此回调:

 @Override
 public void onStatusChanged(String provider, int status, Bundle extras) {
     if (provider.equals(LocationManager.GPS_PROVIDER) 
         && (status == LocationProvider.OUT_OF_SERVICE || status == LocationProvider.TEMPORARILY_UNAVAILABLE)){
         // gps is unavailable 
     } else if  (provider.equals(LocationManager.NETWORK_PROVIDER) 
         && (status == LocationProvider.OUT_OF_SERVICE || status == LocationProvider.TEMPORARILY_UNAVAILABLE)){
         // network provider is unavailable 
     }
 }

第三,要取消订阅您的对象的位置更新,请使用:

 locationManager.removeUpdates(locationListener);

如果没有其他应用需要,这将停止获取位置更新并关闭 GPS-Glonass 定位服务。

【讨论】:

  • 谢谢。我应该何时何地使用第三条指令?
  • @PProg 当您获得第一个让您满意的位置时,只需取消订阅位置侦听器以获取更多更新。
猜你喜欢
  • 2023-03-22
  • 1970-01-01
  • 2020-01-30
  • 1970-01-01
  • 1970-01-01
  • 2017-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多