Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 虽然谷歌地图显示了当前位置,但无法获取GPS位置_Android_Google Maps_Gps - Fatal编程技术网

Android 虽然谷歌地图显示了当前位置,但无法获取GPS位置

Android 虽然谷歌地图显示了当前位置,但无法获取GPS位置,android,google-maps,gps,Android,Google Maps,Gps,我的代码中没有GPS位置,而Google maps显示当前位置,甚至更新位置,我启动了一个服务,在服务内部向locationManager注册locationListener,处理onLocationChanged回调方法,甚至在AndroidManifest.xml中输入。此时将显示创建方法的日志。 下面是我的代码,你们能告诉我哪里做错了 public class LocationService extends Service implements LocationListener { pri

我的代码中没有GPS位置,而Google maps显示当前位置,甚至更新位置,我启动了一个服务,在服务内部向locationManager注册locationListener,处理onLocationChanged回调方法,甚至在AndroidManifest.xml中输入。此时将显示创建方法的日志。
下面是我的代码,你们能告诉我哪里做错了

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

您的locationManager仅使用GPS提供商调用RequestLocationUpdate。

我想你的设备在门状态下无法捕捉GPS卫星信号。
因此,您也应该使用网络提供商调用RequestLocationUpdate。

下面两个链接可能对您有所帮助。


您是说我可以同时为其他提供商注册locationListener?是的。。类LocationService扩展服务{LocationListener gpsListener=new Location Listener{}LocationListener networkListener=new Location Listener{}}检查此选项是否使用类似Google maps的融合位置提供程序。。