My problem is: When I'm trying to move around in the map or zoom in & out it immediately return to my current location. Also when I search for location, new marker added at the searched location but immediately goes back to my current location. I searched for solution but found nothing.. I hope somebody can figure it out
package com.hitman.locationrevision;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationRequest;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.hitman.locationrevision.databinding.ActivityMapsBinding;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
LocationManager locationManager;
LocationListener locationListener;
>function for current location information
public void locationInfo(Location location,String title){
if(location != null) {
LatLng currentLocation = new LatLng(location.getLatitude(), location.getLongitude());
mMap.clear();
mMap.addMarker(new MarkerOptions().position(currentLocation).title(title));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLocation,10));
}
}
>function to request permission from user
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
Location lastLocation=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
locationInfo(lastLocation,"Your lastLocation");
}
}
}
private GoogleMap mMap;
private ActivityMapsBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMapsBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
Intent intent=getIntent();
locationManager=(LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener=new LocationListener() {
@Override
public void onLocationChanged(@NonNull Location location) {
locationInfo(location,"Your Location");
}
};
>code to ask permission and check permission from user
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
}else{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
>code to find the last location and update the last location
Location
lastLocation=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
locationInfo(lastLocation,"Your lastlocation");
}
}
}
CodePudding user response:
Its because you have move camera when your location update. You just need to put condition whether its user call or not to see current location.
Here is the line, this will alway move camera and display your current location
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLocation,10));
You can do like this, whenever user required to see current location then change fromUser = true and then make to false back. Just like happen in Google Map when you click on location button.
var fromUser = false;
if(fromUser){
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLocation,10));
fromUser = false;
}
CodePudding user response:
Use this code for get current location.this method only get one time of user location.inside OnLocationResult callback handle your location logic
private void getCurrentLocation(){
LocationRequest locationRequest = new LocationRequest();
FusedLocationProviderClient fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setMaxWaitTime(1000);
locationRequest.setFastestInterval(500);
locationRequest.setInterval(500);
fusedLocationProviderClient.requestLocationUpdates(locationRequest, new LocationCallback() {
@Override
public void onLocationResult( LocationResult locationResult) {
if(locationResult.getLocations().size() != 0){
Location location = locationResult.getLocations().get(0);
LatLng latLng = new LatLng(location.getLatitude(),location.getLongitude());
//handle current user location logic
FusedLocationProviderClient.removeLocationUpdates(this);
}
}
}, Looper.getMainLooper());
}