如何在第一次服务时获得正确的位置?

时间:2016-04-24 09:05:01

标签: android

我有一个服务获取Android手机的当前位置:

LocationManager locationManager;
private static int LOCATION_INTERVAL = 0;
private static float LOCATION_DISTANCE = 0;
Location mLastLocation;
Location preLocal;

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    mLastLocation = location;
}


@Override
public void onCreate() {
    // TODO Auto-generated method stub
    // super.onCreate();
    if (locationManager == null) {
        locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
    }
    boolean gps_enabled = false, network_enabled = false;
    // SharedPreferences sh = getSharedPreferences("appShareLoc",
    // MODE_PRIVATE);
    // LOCATION_INTERVAL = sh.getInt("LocTime", 1);
    // LOCATION_DISTANCE = sh.getInt("LocDistance", 10);
    try {
        gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch (Exception ex) {
    }
    try {
        network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    } catch (Exception ex) {
    }
    if (gps_enabled) {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                this);
    }
    if (network_enabled) {
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL,
                LOCATION_DISTANCE, this);
    }
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    if (locationManager != null)
        locationManager.removeUpdates(this);
}

@Override
    public void onStart(Intent intent, int startId) {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

              if (mLastLocation == null) {
                boolean gps_enabled = false, network_enabled = false;
                ty {
                    network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
                    if (network_enabled) {
                        mLastLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    }
                } catch (Exception ex) {
                }

                if (mLastLocation == null) {
                    try {
                        gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
                        if (gps_enabled) {
                            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_INTERVAL,
                                    LOCATION_DISTANCE, this);
                            mLastLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                        }
                    } catch (Exception ex) {
                    }
                }

            }
            if (mLastLocation != null) {

//process location
             }
stopSelf();
}

我只在1米内更换手机但服务返回1新位置距离以前的位置是66米。 如何在第一次服务时获得正确的位置?

1 个答案:

答案 0 :(得分:1)

实现" OnMapReadyCallback" 和 试试这个

    @Override
public void onMapReady(final GoogleMap googleMap) {

    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
        G.customToast("Please Turn On GPS ...");
      //when gps turn on , your point in map automatically send to LocationListener .
    }


    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
          //  Log.i("maysam", " lat " + location.getLatitude() + " lon " + location.getLongitude());
            lat = location.getLatitude();
            lon = location.getLongitude();

            mMap = googleMap;
            LatLng sydney = new LatLng(lat, lon);
            mMap.addMarker(new MarkerOptions().position(sydney).title("مختصات  شما"));
            mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 12.0f));

            Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
            List<Address> addresses;
            try {
                addresses = gcd.getFromLocation(location.getLatitude(), location
                        .getLongitude(), 1);
                if (addresses.size() > 0) {
                    String StreetName = addresses.get(0).getThoroughfare();
                    txtCityName.setText(StreetName);
                }


            } catch (Exception e) {
            }

        }

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

        public void onProviderEnabled(String provider) {
        }

        public void onProviderDisabled(String provider) {
        }
    };
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}