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?
Asked
Active
Viewed 6,129 times
1

Trusha Savsani
- 489
- 1
- 11
- 31
4 Answers
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();
-
what is animateCameraToMarker? – Trusha Savsani Feb 11 '15 at 11:25
-
and How to check my Device GPS is on or off? – Trusha Savsani Feb 11 '15 at 11:53
-
For Reference Check this link. http://stackoverflow.com/questions/4300572/android-gps-on-or-off-state – Jaydeep Feb 11 '15 at 12:08
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
i have a similar trouble, in my case work with this:
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(loc.getLatitude(), loc.getLongitude()), 4));
Regards

Daniel Huidobro
- 11
- 4