简体   繁体   中英

Zooming Google Maps to the current Position

I'm having issues when i want to zoom to the current position. I have a gps button that zooms me in, this works fine: But actually i cant find out how to zoom automatically when the Map is loaded. Here is my MainActivity Code:

public class MainActivity extends Activity {

// Constant for defining latitude and longitude
//static final LatLng DerekPos = new LatLng(40 , -79);

// GoogleMap class
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


// verify we can interact with the Google Map
try {
    if (googleMap == null) {
        googleMap = ((MapFragment) getFragmentManager().
                findFragmentById(R.id.map)).getMap();
    }
    // Show a satellite map with roads
    /* MAP_TYPE_NORMAL: Basic map with roads.
    MAP_TYPE_SATELLITE: Satellite view with roads.
    MAP_TYPE_TERRAIN: Terrain view without roads.
    */
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    // Place dot on current location
    googleMap.setMyLocationEnabled(true);

    // Turns traffic layer on
    googleMap.setTrafficEnabled(true);

    // Enables indoor maps
    googleMap.setIndoorEnabled(true);

    // Turns on 3D buildings
    googleMap.setBuildingsEnabled(true);

    // Show Zoom buttons
    googleMap.getUiSettings().setZoomControlsEnabled(true);

    // Create a marker in the map at a given position with a title
    //Marker marker = googleMap.addMarker(new MarkerOptions().
      //      position(DerekPos).title("Sie sind hier!"));

    //googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(DerekPos,16));


} catch (Exception e) {
    e.printStackTrace();
}
}

I get a button provided with:

googleMap.setMyLocationEnabled(true);

When i hit this button it moves me to my current position. Can someone exlain me how this happens? What i would like to do is to zoom in when the Location is obtained not pressing this button.

I did that method to do that:

public static void ZoomLocMap(GoogleMap googleMap, SupportMapFragment map, double latitude, double Longitude, int nivelZoom) {

        try
        {
            googleMap = map.getMap();

            // Enabling MyLocation Layer of Google Map
            googleMap.setMyLocationEnabled(true);

            //acercar a localizacion animada
            LatLng latLng = new LatLng(latitude, Longitude);

            CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, nivelZoom);
            googleMap.animateCamera(cameraUpdate);

        }catch (NullPointerException e)
        {
            Log.i("LogsAndroid", "NullPointerException");
        }
    }

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