简体   繁体   中英

Can't add marker on User's Current Location Google Map V2

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. 谷歌地图

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

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

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

可能您可以使用 fusedLocationProviderClient 对象 getLastLocation() 方法更改 getMyLocaton() 方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM