1

I implement map in my device successfully.
but now I want to add functionality
When I click on Button then I want to get Current Location in map.
How can I do that?

enter image description here

Trusha Savsani
  • 489
  • 1
  • 11
  • 31

4 Answers4

2

You can add listener on button click.

Button btnMyLocation = (Button) findViewById(R.id.btnMyLocation); 
CameraUpdate cameraUpdate = null;

btnMyLocation.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Location loc = map.getMyLocation();
                if (loc != null) {
                    LatLng latLng = new LatLng(loc.getLatitude(), loc
                            .getLongitude());
                    cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 17);
                    map.animateCamera(cameraUpdate);

                }

            }
        });

for GPS :

You require following permission in Manifest file

android.permission.ACCESS_FINE_LOCATION

and here is code :

final LocationManager manager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE );

  if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) )
    Toast.makeText(context, "GPS is disable!", Toast.LENGTH_LONG).show(); 
  else
    Toast.makeText(context, "GPS is Enable!", Toast.LENGTH_LONG).show();
Tlaloc-ES
  • 4,825
  • 7
  • 38
  • 84
Jaydeep
  • 110
  • 8
0

Just start a location Update listener on the click of that button. and when new location arrives move the camera in mapObject (got in OnMapReady() method) to that location with animations.

Ankit Bansal
  • 1,801
  • 17
  • 34
0

In the OnClick method, get the currentGPS location (latitude longitude co-ordinates) through LocationManager. More info on howto is here

Community
  • 1
  • 1
Vasanth
  • 60
  • 4
0

i have a similar trouble, in my case work with this:

map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(loc.getLatitude(), loc.getLongitude()), 4));

Regards