Reputation: 31
now i am trying to get my current location but i cant get it only one location 37.4219983,-122.084 this is location from any where i cant get my true location and this is my code
private void getDeviceLocation(){
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
try{
if(mLocationPermissionsGranted){
final Task location = mFusedLocationProviderClient.getLastLocation();
location.addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if(task.isSuccessful()){
Location currentLocation = (Location) task.getResult();
current_lat=currentLocation.getLatitude();
current_lng=currentLocation.getLongitude();
Log.e("currentLocation",current_lat+"....."+current_lng);
moveCamera(new LatLng(current_lat,current_lng),
15f);
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
mMap.clear();
MarkerOptions markerOptions=new MarkerOptions();
markerOptions.position(new LatLng(latLng.latitude,latLng.longitude)).title("disance");
float[]results=new float[10];
Location.distanceBetween(current_lat,current_lng,latLng.latitude,latLng.longitude,results);
markerOptions.snippet("ditance ="+results[0]);
mMap.addMarker(markerOptions);
}
});
mMap.clear();
}else{
Toast.makeText(add_center_map.this, "unable to get current location", Toast.LENGTH_SHORT).show();
}
}
});
}
}catch (SecurityException e){
}
}
private void moveCamera(LatLng latLng, float zoom){
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoom));
}
Upvotes: 3
Views: 1584
Reputation: 227
You are probably testing this in emulator. Its location is set to
Longitude: -122.0840 Latitude: 37.4220 Altitude: 0.0
by default.
You can test on a real device or configure other coordinates for your virtual device in its Extended controls (three dots beside the AVD).
Upvotes: 3