Reputation: 16398
I have a SherlockFragmentActivity
that displays a GoogleMap using Google Maps Android API v2. I want the map to zoom to my current location as soon as the fragment launches. I couldn't find such a method in the documentation. How to achieve this?
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ui_settings_demo);
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map))
.getMap();
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.setMyLocationEnabled(true);
//How to zoom to my current location?
}
Upvotes: 2
Views: 1644
Reputation: 1006539
Unless you already know your location, that is not possible, as it will take time to find the location.
setMyLocationEnabled(true)
merely allows the user to elect to center/zoom the map on their location. If you want to force it yourself, you will have to find the location yourself using LocationManager
and set the camera position accordingly. Or combine the two, as I did in this sample project. But, again, this will take some time.
Upvotes: 1