0

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();
    }
Jaydip
  • 592
  • 5
  • 27
  • It's not completely clear where you're having problems. However, using `getLastKnownLocation()` is not very reliable. Best to explicitly request a location update when you need it, take a look at the code in this answer that uses FusedLocationProviderApi: http://stackoverflow.com/a/30255219/4409409 – Daniel Nugent Oct 31 '15 at 01:18
  • oky thnax let me see where i am doing mistake – Jaydip Oct 31 '15 at 02:04

2 Answers2

1

After calling animateCamera() ,you need to call setMyLocationEnabled(true) e.g

    googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                    new LatLng(location.getLatitude(), location
                            .getLongitude()), 13));
    googleMap.setMyLocationEnabled(true);
Shishupal Shakya
  • 1,632
  • 2
  • 18
  • 41
0

Have you tried the following line of code?

googleMap.setMyLocationEnabled(true);
user3280937
  • 135
  • 1
  • 3
  • 9