I am take my current location for put marker on map its working properly but some time its not working properly i don't know why help me to find out my fault in this code. This is my code for take current location and put marker on map.
This is my java file MapActivity.java
try {
if (googleMap == null) {
googleMap = ((MapFragment) getActivity().getFragmentManager()
.findFragmentById(R.id.map)).getMap();
googleMap.getUiSettings().setZoomControlsEnabled(false);
}
GoogleMapOptions options = new GoogleMapOptions();
options.mapType(GoogleMap.MAP_TYPE_NORMAL).compassEnabled(false)
.rotateGesturesEnabled(false).tiltGesturesEnabled(false);
// this is for get current location
LocationManager locationManager = (LocationManager) getActivity()
.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
Location location = locationManager
.getLastKnownLocation(locationManager.getBestProvider(
criteria, true));
if (location != null) {
currentLatLng = (new LatLng(location.getLatitude(),
location.getLongitude()));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(), location
.getLongitude()), 13));
Marker marker = googleMap.addMarker(new MarkerOptions()
.position(currentLatLng));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(currentLatLng).zoom(12)
.bearing(90).tilt(40).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
CircleOptions circleOptions = new CircleOptions()
.center(currentLatLng).radius(200)
.fillColor(Color.TRANSPARENT)
.strokeColor(0x10000000).strokeWidth(5);
mCircle = googleMap.addCircle(circleOptions);
}
} catch (Exception e) {
e.printStackTrace();
}