0

I am trying to fetch user's current location on Genymotion emulator.I already set the custom GPS longitute and latitute on Genymotion. Whenever i trying to open Google Maps the Current location can't show in it. Here is my Code snippet.

  googleMap = ((MapFragment) getFragmentManager().findFragmentById(
    R.id.map1)).getMap();
  googleMap.setMyLocationEnabled(true);
  LocationManager locManager = (LocationManager) context
    .getSystemService(context.LOCATION_SERVICE);
  Criteria criteria = new Criteria();
  String locProvider = locManager.getBestProvider(criteria, false);
  Location location = locManager.getLastKnownLocation(locProvider);
  Location myLocation = googleMap.getMyLocation();
  if (myLocation != null) {
   double latitude = location.getLatitude();
   double longitude = location.getLongitude();
   LatLng latLng = new LatLng(latitude, longitude);
   googleMap.addMarker(new MarkerOptions()
     .position(latLng)
     .title("rajkot")
     .icon(BitmapDescriptorFactory
       .fromResource(R.drawable.ic_launcher)));
   googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
   googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));

  } else {
   Toast.makeText(context, "unable to find location", 20).show();
  }

This is my screenshot. Google Map

Please help me i can't find the user's current location

I also Check it on real device It's not working

viratpuar
  • 524
  • 1
  • 5
  • 22

2 Answers2

0

Try This.

private GoogleMap mMap; // Might be null if Google Play services APK is not available.



mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {


            @Override
            public void onMyLocationChange(Location location) {

                double latitude = location.getLatitude();
                double longitude = location.getLongitude();

                LatLng latestlatLng = new LatLng(latitude, longitude);

                Marker myself = mMap.addMarker(new MarkerOptions().position(latestlatLng).title("It's Me!"));
              //  myself.setDraggable(true);
                  mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude())));
            }
        });
Avinash Verma
  • 2,572
  • 1
  • 18
  • 22
0

may be you could change the getMyLocaton() method with a fusedLocationProviderClient objects getLastLocation() method.