1

I am trying to show current location on google map on andorid.I have written some cod to do that.But when i launch the app on Map Activity it does not show current location on google map.But when i tap on current location button on google map then it shows the current location.Please why it does not show current location automatically?

 mMap.setMyLocationEnabled(true);
                mMap.getUiSettings().setMyLocationButtonEnabled(true);
                Log.i("MAP", "AFTER LOCATION");
                LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
                Criteria criteria = new Criteria();
                criteria.setAccuracy(Criteria.ACCURACY_FINE);
                String bestProvider = locationManager.getBestProvider(criteria, true);
                Location location = locationManager.getLastKnownLocation(bestProvider);
                if (location != null) {

                    onLocationChanged(location);
                }
                locationManager.requestLocationUpdates(bestProvider,0, 0, this);

enter image description here

TechChain
  • 8,404
  • 29
  • 103
  • 228

1 Answers1

0

Use this function, when map is ready:

private void centerMap(GoogleMap map, Double latitude, Double longitude){

    LatLng latLng = new LatLng(latitude, longitude);

    CameraPosition cp = new CameraPosition.Builder()
            .target(latLng)
            .zoom(13)
            .build();

    map.animateCamera(CameraUpdateFactory.newCameraPosition(cp));
}

You can get coordinates from location like this: location.getLatitude() and Location.getLongitude()

Panczur
  • 633
  • 1
  • 9
  • 26