Search code examples
androidgoogle-mapsandroid-gps

How to show my current location in maps in android as soon as the app starts the map activity


What to do so that as soon as i open the map activity of my app it will show me my current location on the google maps and fills the text view with the current street address.I searched a lot but didn't got any way to do this.even the runtime permissions is not working.The button which gets the marker to my current location giving no response.No errors shown. Here is my map activity.

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    GoogleMap.OnMarkerDragListener,
    GoogleMap.OnMapLongClickListener, View.OnClickListener{

private GoogleMap mMap;
private double vlatitude, vlongitude;
private GoogleApiClient vgoogleApiClient;
private Button getButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // 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);

    //Initializing googleapi client
    vgoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

    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;
    }

    mMap.setMyLocationEnabled(true);
    getButton = (Button) findViewById(R.id.buttongetme);
    getButton.setOnClickListener(this);
}

@Override
protected void onStart() {
    vgoogleApiClient.connect();
    super.onStart();
}

@Override
protected void onStop() {
    vgoogleApiClient.disconnect();
    super.onStop();
}
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.mipmap.tiltpink)).position(sydney).draggable(true).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    mMap.setOnMarkerDragListener(this);
    mMap.setOnMapLongClickListener(this);
    mMap.getUiSettings().setMyLocationButtonEnabled(true);
}

private void getCurrentLocation() {

    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;
    }
    Location location = LocationServices.FusedLocationApi.getLastLocation(vgoogleApiClient);
    if (location != null){
        vlongitude = location.getLongitude();
        vlatitude = location.getLatitude();
        moveMap();
    }
}

private void moveMap() {
    LatLng latLng = new LatLng(vlatitude,vlongitude);
    mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.mipmap.tiltpink)).position(latLng).draggable(true).title("Current Location"));

    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    getCurrentLocation();

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

@Override
public void onMapLongClick(LatLng latLng) {

    mMap.clear();
    mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.mipmap.tiltpink)).position(latLng).draggable(true).title("This is my loc"));

}

@Override
public void onMarkerDragStart(Marker marker) {

}

@Override
public void onMarkerDrag(Marker marker) {

}

@Override
public void onMarkerDragEnd(Marker marker) {

    vlatitude = marker.getPosition().latitude;
    vlongitude = marker.getPosition().longitude;
    moveMap();

}

@Override
public void onClick(View view) {

    if (view == getButton){
        getCurrentLocation();
        moveMap();
    }

}

Solution

  • This method solves the problem.

     @Override
    public void onLocationChanged(Location location) {
        if (location != null) {
            LatLng latLang = new LatLng(location.getLatitude(),
                    location.getLongitude());
            animateCameraToMarker(latLang, false);
            setMarker(latLang);
            getAddressFromLocation(latLang, source);
        }
    
       // stop location updates
        if (mGoogleApiClient != null) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        }
    
    }