How to get more accuracy in GPS using google location on android

0

In my project, I need to get a current user location and address and constantly update my position because of greater accuracy. The problem is that when I got the location (Latitude and Longitude), using the Geocode class to get the address and display with a marker, it shows the wrong address, and a few seconds later shows the right address, I was wondering if the problem is My Android device or my code! This is how I did it!

Call the connection

private synchronized void callConnection() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    mGoogleApiClient.connect();

}

OnConnected

@Override
public void onConnected(@Nullable Bundle bundle) {
    Log.i("LOG", "onConnected(" + bundle + ")");
    try{
        Location l = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if(l != null){
            Log.i("LOG","latitude: " + l.getLatitude());
            Log.i("LOG","longitude: " + l.getLongitude());

            lat =  l.getLatitude();
            lon = l.getLongitude();
            mapUpdate(lat,lon);
        }
        address = getAddress(lat,lon);
        Log.i("LOG","city: " + address.getLocality());
        Log.i("LOG","state: " + address.getAdminArea());
        Log.i("LOG","country: " + address.getCountryName());
        Log.i("LOG","address: " + address.getAddressLine(0));
        Log.i("LOG","getThoroughfare" + address.getThoroughfare());
        Log.i("LOG","FT" + address.getFeatureName());
        ads = address.getAddressLine(0);
        startLocationUpdate();
    }catch (SecurityException ex){
        Toast.makeText(this, ex.toString(), Toast.LENGTH_SHORT).show();
    }catch (IOException e) {
        e.printStackTrace();
    }
}

Method to return the address

private Address getAddress(double lat, double lon) throws IOException{
    Geocoder geocoder;
    Address address = null;
    List<Address> addressList;

    geocoder = new Geocoder(getApplicationContext());
    addressList = geocoder.getFromLocation(lat,lon,1);
    if(addressList.size() > 0){
        address = addressList.get(0);
    }
    return address;
}

And the method to update the map

private void mapUpdate(final Double lat,final Double lon){
    mapFragment.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap googleMap) {
            Log.i("LOG",googleMap.toString());
            mMap = googleMap;

            mMap.getUiSettings().setMapToolbarEnabled(false);
            LatLng myCurrentLocation = new LatLng(lat, lon);
            if(marker == null){
                marker = mMap.addMarker(new MarkerOptions().position(myCurrentLocation).title(ads));
                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myCurrentLocation, 20));
                marker.showInfoWindow();

            }else {
                marker.setPosition(myCurrentLocation);
                marker.setTitle(getLastAddress(lat,lon));
                marker.showInfoWindow();

            }
        }
    });
}

Now I update the location because the user can move and have better quality

private void initLocationRequest(){

    mlocationRequest = new LocationRequest();
    mlocationRequest.setInterval(5000);
    mlocationRequest.setFastestInterval(2000);
    mlocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
private void startLocationUpdate(){

    initLocationRequest();
    try{
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mlocationRequest,MainActivity.this);
    }catch (SecurityException ex){
        Toast.makeText(this, ex.toString(), Toast.LENGTH_SHORT).show();
    }
}
@Override
public void onLocationChanged(Location location) {
    Log.i("LOG","latitude(" + location.getLatitude());
    Log.i("LOG","longitude" + location.getLongitude());
    Log.i("LOG","speed" + location.getSpeed());

    lat =  location.getLatitude();
    lon = location.getLongitude();
    mapUpdate(lat,lon);
    //stopLocation();
}

Every time I change, I change the address and step inside the Maker!

private String getLastAddress(double lat, double lon){
    try {
        address = getAddress(lat,lon);
        ads = address.getAddressLine(0);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return ads;
}
    
asked by anonymous 13.06.2017 / 18:11

1 answer

0

The method

LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

Always return to the most current location that was registered to the device by GooglePlayServices first. If you want to get the current location first, you should skip this first location and always use the most recent one received in your startLocationUpdate

Reference: link

    
21.06.2017 / 22:39