Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

centering the camera in Google Maps V2

In my android app I have the following code...

CameraUpdate center= CameraUpdateFactory.newLatLng(new LatLng(Lat,Lon));
DebugLog.debugLog("centered camera on " + Lat + " and " + Lon, false);
    CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
map.moveCamera(center);
    map.animateCamera(zoom);
map.addMarker(new MarkerOptions()
        .position(new LatLng(Lat, Lon))
        .title("Phone Location")
            );

Lat is 31.7898 Lon is -111.0354

The marker is exactly at the proper location. However the camera is centered about 5 miles north of that location on the v2 map. Why? Thanks Gary

like image 574
Dean Blakely Avatar asked Jan 13 '23 06:01

Dean Blakely


2 Answers

After reading some additional docs, I found that the following code worked...

    CameraPosition cameraPosition = new CameraPosition.Builder()
        .target(new LatLng(Lat, Lon))
        .zoom(15)
        .bearing(0)
        .tilt(45)
        .build();
    map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    map.addMarker(new MarkerOptions()
    .position(new LatLng(Lat, Lon))
    .title("Phone Location")
        );  

I think my original code should have also worked. Gary

like image 137
Dean Blakely Avatar answered Jan 21 '23 12:01

Dean Blakely


Keep the perspective the same, but center camera at the currently clicked marker:

    obj_map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
    @Override
    public boolean onMarkerClick(Marker arg0) {
        CameraPosition obj_cam = new CameraPosition.Builder()
                .target(new LatLng(arg0.getPosition().latitude, arg0.getPosition().longitude))
                .zoom(obj_map.getCameraPosition().zoom)
                .tilt(obj_map.getCameraPosition().tilt)
                .bearing(obj_map.getCameraPosition().bearing)
                .build();
        obj_map.animateCamera(CameraUpdateFactory.newCameraPosition(obj_cam));
        return true;
    }
like image 33
Kevin Delaney Avatar answered Jan 21 '23 13:01

Kevin Delaney