【问题标题】:Not getting GPS location, though Google map is showing current location未获取 GPS 位置,但 Google 地图显示当前位置
【发布时间】:2014-08-07 05:39:16
【问题描述】:

我没有在我的代码中获取 GPS 位置,而 Google 地图正在显示当前位置甚至更新,我正在启动一个服务,在服务内将 locationListener 注册到 locationManager,处理 onLocationChanged 回调方法,在 AndroidManifest 中进行条目。 xml 甚至。正在显示日志 onCreate 方法。
下面是我的代码,你们能告诉我我哪里做错了......

public class LocationService extends Service implements LocationListener {
private static final long MIN_TIME_INTERVAL_FOR_GPS_LOCATION = 100;
private static final float MIN_DISTANCE_INTERVAL_FOR_GPS_LOCATION = 1.0f;
private static String TAG = LocationService.class.getSimpleName();
private LocationManager locationManager;
private static Location mCurrentLocation;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    Log.i(TAG, "onCreate...");

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_INTERVAL_FOR_GPS_LOCATION, MIN_DISTANCE_INTERVAL_FOR_GPS_LOCATION, this);
    mCurrentLocation = getBestLocation();
}

private Location getBestLocation() {
    Log.i(TAG, "getBestLocation...");
    Location location_gps = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    Location location_network = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    // If both are available, get the most recent
    if (location_gps != null && location_network != null) {
        return (location_gps.getTime() > location_network.getTime()) ? location_gps : location_network;
    } else if (location_gps == null && location_network == null) {
        return null;
    } else {
        return (location_gps == null) ? location_network : location_gps;
    }
}

@Override
public void onLocationChanged(Location location) {
    Log.i(TAG, "onLocationChanged...");
    Toast.makeText(LocationService.this, "Location Found", Toast.LENGTH_SHORT).show();
    mCurrentLocation = location;
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

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

public static Location getmCurrentLocation() {
    return mCurrentLocation;
}

public static void setmCurrentLocation(Location mCurrentLocation) {
    LocationService.mCurrentLocation = mCurrentLocation;
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.i(TAG, "onDestroy...");
    locationManager.removeUpdates(this);
}
}

【问题讨论】:

  • 使用像谷歌地图这样的融合位置提供者..

标签: android google-maps gps


【解决方案1】:

您的 locationManager 仅使用 GPS 提供程序调用 requestLocationUpdates。

我认为您的设备无法捕捉到 GPS 卫星信号,室内状态。
因此,您也应该使用网络提供商调用 requestLocationUpdates。

接下来的两个链接可能对你有帮助。
http://developer.android.com/guide/topics/location/strategies.html
http://developer.android.com/training/location/receive-location-updates.html

【讨论】:

  • 所以你是说我也可以同时为其他提供者注册 locationListener ?
  • 是.. 类 LocationService 扩展服务 { LocationListener gpsListener = new Location Listener { } LocationListener networkListener = new Location Listener { } }
  • 查看link
猜你喜欢
  • 1970-01-01
  • 2013-12-02
  • 2011-02-11
  • 2016-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-01
  • 1970-01-01
相关资源
最近更新 更多