的LocationManager发送最后位置的所有时间 [英] LocationManager is sending Last location all time

查看:119
本文介绍了的LocationManager发送最后位置的所有时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经参与了选址问题。我得到我的第一次位置,如果我试图为下一个位置,它给我最后位置的所有时间。我改变了我的位置可能时间,但所有的时间,我得到最后的经度和纬度。我认为,GPS不要刷新。
如果我重新启动我的电话,然后再试一次。它显示到正确的位置,如果我再次尝试,它显示了我最后一个位置。

I have involved with location problems. I am getting my first time location and if i tried for next location, it is giving me Last location all time. I changed my location may times but all time, I am getting Last latitude and longitude. I think, GPS don't refresh. If i restarted to my phone and try again. it display to correct location and if i tried again, it is displaying me Last location.

我分享我的源$ C ​​$ C

I am sharing my source code

@Override
protected void onResume() {
    super.onResume();

    if(mMap!= null)
        mMap.clear();

    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);                
    mFragment = (SupportMapFragment) this.getSupportFragmentManager().findFragmentById(R.id.map);
    mMap = mFragment.getMap(); 
    //if(loc != null)
        //loc.reset();

    loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if(loc != null) {
        LatLng geo = HelperUtil.getLatLng(loc);
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(geo, 18));
        mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
    }
    setListener();

}

private void setListener(){ 
    if(locationListener != null){
        clearListener();
    }
    Toast toast = Toast.makeText(this, this.getResources().getString(R.string.maps_loading), Toast.LENGTH_LONG);
    toast.show();                   
    locationListener = new CustomLocationListener();
    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);                
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    Handler handler=new Handler();
    handler.postDelayed(new TimeoutTask(locationListener), 15*1000);
}

private void clearListener(){
    if(locationListener != null){
        LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        locationManager.removeUpdates(locationListener);
        locationListener = null;
    }
}

public class CustomLocationListener implements LocationListener {
    private boolean isCompleted = false;

    public CustomLocationListener() {}

    @Override
    public void onLocationChanged(Location location) {
        isCompleted=true;
          // Called when a new location is found by the network location provider.
          if(isBetterLocation(location, loc)){
              gotLocation(location);
          }
          clearListener();
          setUserPoint();
    }

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

    @Override
    public void onProviderEnabled(String provider) {}

    @Override
    public void onProviderDisabled(String provider) {}

    public boolean isCompleted() {
        return isCompleted;
    }
}   


     public class TimeoutTask implements Runnable {

    private CustomLocationListener listener;

    public TimeoutTask(CustomLocationListener listener) {
        this.listener=listener;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        if (listener == null || listener.isCompleted()) {
            Log.e("provider", "completed");
            System.out.println("Lat in completed= "+loc.getLatitude());
            System.out.println("Long in completed = "+loc.getLongitude());
        }
        else {
            Log.e("provider", "timeout");
            clearListener();
            setUserPoint();
        }
    }
}

protected boolean isBetterLocation(Location location, Location currentBestLocation) {
    if (currentBestLocation == null) {
        // A new location is always better than no location
        return true;
    }

    // Check whether the new location fix is newer or older
    long timeDelta = location.getTime() - currentBestLocation.getTime();
    boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
    boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
    boolean isNewer = timeDelta > 0;

    // If it's been more than two minutes since the current location, use the new location
    // because the user has likely moved
    if (isSignificantlyNewer) {
        return true;
    // If the new location is more than two minutes older, it must be worse
    } else if (isSignificantlyOlder) {
        return false;
    }

    // Check whether the new location fix is more or less accurate
    int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
    boolean isLessAccurate = accuracyDelta > 0;
    boolean isMoreAccurate = accuracyDelta < 0;
    boolean isSignificantlyLessAccurate = accuracyDelta > 200;

    // Check if the old and new location are from the same provider
    boolean isFromSameProvider = isSameProvider(location.getProvider(),
            currentBestLocation.getProvider());

    // Determine location quality using a combination of timeliness and accuracy
    if (isMoreAccurate) {
        return true;
    } else if (isNewer && !isLessAccurate) {
        return true;
    } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
        return true;
    }
    return false;
}

private void gotLocation(Location location){
    loc = location;
}

请高手帮帮我。我也注意到,有时我不能够得到的位置和它说超时。你的时间将是对我来说非常有帮助。

Please expert help me. I also noted that sometime i am not able to get location and it say timeout. Your time will be very helpful for me.

推荐答案

您正在使用的网络位置不提供正确的位置,而不是使用网络位置,你应该使用当前和精确定位的GPS位置。

you are using network location which doesn't provide the correct location rather than using network location you should use the GPS location for current and accurate location.

getLastKnowLocation()方法,这并不提供正确的,当前位置可以参考此<一个href=\"http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation%28java.lang.String%29\"相对=nofollow>链接。

getLastKnowLocation() method doesn't provide the correct , current location for this you can refer this link.

而不是使用网络位置使用GPS定位,你可以找到从给定链路GPS定位的很好的例子。

rather than using network location use GPS location and you can find the good example of GPS location from the given link.

http://www.androidhive.info/2012 / 07 / Android的GPS定位管理器教程/

这篇关于的LocationManager发送最后位置的所有时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆