0

below is my code which give hardcoded lat long of user i want to show dynamic latlong where ever user go map show current location of user how to do this ?

       protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_support_map_fragment);


    FragmentManager fmanager = getSupportFragmentManager();
    Fragment fragment = fmanager.findFragmentById(R.id.map);
 SupportMapFragment supportmapfragment = (SupportMapFragment)fragment;
   GoogleMap supportMap = supportmapfragment.getMap();



   LatLng latLng = new LatLng(24.89337, 67.02806);
   supportMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
   supportMap.addMarker(new MarkerOptions()
           .position(latLng)
           .title("My Spot")
           .snippet("This is my new spot!")

 .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
   supportMap.getUiSettings().setCompassEnabled(true);
   supportMap.getUiSettings().setZoomControlsEnabled(true);
   supportMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 20));



}
smart guy
  • 35
  • 2
  • 6

2 Answers2

1

Try adding this line:

supportMap.setMyLocationEnabled(true);
frogmanx
  • 2,620
  • 1
  • 18
  • 20
0

You obviously need to get the current location of the user. You need to use LocationListener to request continuous updates. You can specify the time interval or the distance interval between the location updates. This with give you a Location object from which you can extract the latitude and longitude. You can then use these 2 values to point the current location on the map. There are numerous questions and great answers on How to get the current location. You can start by reading:

Location Strategies Google's documentation

Some of the previous questions are:

Hope this helps.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124