Google映射当前位置并不总是正确的

时间:2017-07-10 15:48:18

标签: google-maps-android-api-2

我一直在开发一个Android应用程序,用于跟踪用户使用谷歌地图行进的距离。我面临的一个主要问题是地图显示的当前位置有时是完全错误的。它被困在我1小时前的某个地方,并且该地点不会更新。

我使用GPS_PROVIDER作为位置监听器。

因此,应用随机停止提供位置更新,有时在尝试开始跟踪时,当前位置是错误的。我也多次在超级应用上观察到这个问题。超级应用程序显示我在另一个地方,当我打开另一个使用地图的应用程序时,位置也会在超级应用程序上得到纠正。

请让我知道我做错了什么。

这是我用来获取位置更新的代码。

public class SendLocationService extends Service {

private static final String TAG = "BOOMBOOMTESTGPS";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 2000;
private static final float LOCATION_DISTANCE = 50;
private String country;
private String area;

private class LocationListener implements android.location.LocationListener {
    Location mLastLocation;

    public LocationListener(String provider) {
        Log.e(TAG, "LocationListener " + provider);
        mLastLocation = new Location(provider);
    }

    @Override
    public void onLocationChanged(Location location) {

        //float distance = mLastLocation.distanceTo(location);
        SharedPreferences sharedPref = getApplicationContext().getSharedPreferences(
                getApplicationContext().getString(R.string.preference_file_key), Context.MODE_PRIVATE);
        int userId = Integer.parseInt(sharedPref.getString("userId", "0"));
        int campaignId = Integer.parseInt(sharedPref.getString("campaignId", "0"));

        // if(location.getAccuracy()<=15) {
        TripDao tripDao = new TripDao();
        tripDao.updateTripCurrentLocation(SendLocationService.this, campaignId, userId, location.getLatitude(), location.getLongitude(), getAddressFromLatLng(location.getLatitude(), location.getLongitude()), country, area, location.getAccuracy());
        //}
        Log.e(TAG, "onLocationChanged: " + location);
        mLastLocation = location;


        // tripDao.startTrip(SendLocationService.this, userId, 
location.getLatitude(), location.getLongitude());
    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.e(TAG, "onProviderDisabled: " + provider);
    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.e(TAG, "onProviderEnabled: " + provider);
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.e(TAG, "onStatusChanged: " + provider);
    }
}

LocationListener[] mLocationListeners = new LocationListener[]{
        new LocationListener(LocationManager.GPS_PROVIDER)/*,
        new LocationListener(LocationManager.NETWORK_PROVIDER)*/
};

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.e(TAG, "onStartCommand");
    super.onStartCommand(intent, flags, startId);
    return START_STICKY;
}

@Override
public void onCreate() {
    Log.e(TAG, "onCreate");
    initializeLocationManager();

    try {
        mLocationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                mLocationListeners[0]);
    } catch (SecurityException ex) {
        Log.i(TAG, "fail to request location update, ignore", ex);
    } catch (IllegalArgumentException ex) {
        Log.d(TAG, "gps provider does not exist " + ex.getMessage());
    }


}

@Override
public void onDestroy() {
    Log.e(TAG, "onDestroy");
    super.onDestroy();
    if (mLocationManager != null) {
        for (int i = 0; i < mLocationListeners.length; i++) {
            try {
                //  mLocationManager.removeUpdates(mLocationListeners[i]);
            } catch (Exception ex) {
                Log.i(TAG, "fail to remove location listners, ignore", ex);
            }
        }
    }
}

private void initializeLocationManager() {
    Log.e(TAG, "initializeLocationManager");
    if (mLocationManager == null) {
        mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
    }
}

private String getAddressFromLatLng(double latitude, double longitude) {

    Geocoder geo = new Geocoder(this.getApplicationContext(), Locale.getDefault());

    String address = "";
    List<Address> addresses = null;
    try {
        addresses = geo.getFromLocation(latitude, longitude, 1);
        if (addresses != null) {
            Address returnedAddress = addresses.get(0);
            StringBuilder strReturnedAddress = new StringBuilder("");

            for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
                strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
            }

            country = returnedAddress.getCountryName();
            address = strReturnedAddress.toString();
            area = returnedAddress.getSubLocality();
            Log.w("My Current loction add", "" + strReturnedAddress.toString());
        } else {
            Log.w("My Current loction add", "No Address returned!");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return address;
}

}

0 个答案:

没有答案