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! That’s how I did it!
I call the connection
private synchronized void callConnection() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
O 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 also 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;
}