在应用程序启动时更改缩放级别 Google 地图 Api v2

Change zoom level Google Maps Api v2 on app start

是否可以在地图准备好后立即更改缩放级别?当我打开应用程序时,它会显示地图和我所在位置的蓝点。但是,缩放级别是默认的 3。我该如何更改它?我知道如何在单击“MyLocationButton”时执行此操作,但不知道在应用程序启动时执行此操作。

这是我的class

public class MainPhoneActivity extends FragmentActivity implements ConnectionCallbacks, OnConnectionFailedListener, LocationSource, LocationListener, OnMyLocationButtonClickListener, OnMapReadyCallback{

    private GoogleApiClient mGoogleApiClient;
    private OnLocationChangedListener mMapLocationListener = null;

    // location accuracy settings
    private static final LocationRequest REQUEST = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_phone);

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.mapView);
        mapFragment.getMapAsync(this);

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mGoogleApiClient.connect();
    }

    @Override
    public void onPause() {
        super.onPause();
        mGoogleApiClient.disconnect();
    }

    @Override
    public void onMapReady(GoogleMap map) {
        map.setLocationSource(this);
        map.setMyLocationEnabled(true);
        map.setOnMyLocationButtonClickListener(this);
    }

    public void showMyLocation(View view) {
        if (mGoogleApiClient.isConnected()) {
            String msg = "Location = "
                    + LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        if (mMapLocationListener != null) {
            mMapLocationListener.onLocationChanged(location);
        }
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, REQUEST,this);
    }

    @Override
    public void onConnectionSuspended(int cause) {
        // Do nothing
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        // Do nothing
    }

    @Override
    public boolean onMyLocationButtonClick() {
        return false;
    }

    @Override
    public void activate(OnLocationChangedListener onLocationChangedListener) {
        mMapLocationListener = onLocationChangedListener;
    }

    @Override
    public void deactivate() {
        mMapLocationListener = null;
    }
}

您可以像下面这样设置缩放级别:

map.animateCamera(CameraUpdateFactory.newLatLngZoom(point,15));

其中 point 是您的 LatLng 职位。通过这种方式,您可以使用给定的缩放级别将地图置于该点的中心。

完成方法:

@Override
    public void onMapReady(GoogleMap map) {
        map.setLocationSource(this);
        map.setMyLocationEnabled(true);
        map.setOnMyLocationButtonClickListener(this);
        //newLatLngZoom(LatLng , ZoomLevel) -> choose your zoom level
        // and change my 'point' with yours
        map.animateCamera(CameraUpdateFactory.newLatLngZoom(point,15));
    }

编辑:

如果你想得到点坐标,你可以试试这个:

Location loc = map.getMyLocation();
LatLng point = new LatLng(loc.getlatitude() , loc.getLongitude());

并以此点为中心。

完成方法:

@Override
        public void onMapReady(GoogleMap map) {
            map.setLocationSource(this);
            map.setMyLocationEnabled(true);
            map.setOnMyLocationButtonClickListener(this);
            Location loc = map.getMyLocation();
            if(loc != null){
                LatLng point = new LatLng(loc.getLatitude() , loc.getLongitude());
                map.animateCamera(CameraUpdateFactory.newLatLngZoom(point,15));
            }
        }

这可能会导致 NullPointerException 因为 loc 可能为空。

其他解决方案

如果您只想获得第一次坐标,您应该在 onLocationChanged 中工作,使用布尔变量设置第一次调用。

声明它CRDS_CALL = false;

  @Override
        public void onLocationChanged(Location location) {
            if (mMapLocationListener != null) {
                mMapLocationListener.onLocationChanged(location);
                if(!CRDS_CALL){
                    LatLng point = new LatLng(location.getLatitude(), location.getLognitude());
                    map.animateCamera(CameraUpdateFactory.newLatLngZoom(point,15));
                    CRDS_CALL = true;
                }
        }
    }

在这个答案中我使用 map,但你必须使用你的 mapFragment,但如果你想在 onCreate 之上的其他方法中使用它,你必须在外部声明

onCreate

之前添加这个
SupportMapFragment mapFragment;

在里面,像下面这样使用它:

mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.mapView);
        mapFragment.getMapAsync(this);

这样,你可以在其他方法中使用mapFragment

你应该使用 this。你应该把这个请求放在 onMapReady() 回调中。像这样:

@Override
public void onMapReady(GoogleMap map) {
    map.setLocationSource(this);
    map.setMyLocationEnabled(true);
    map.setOnMyLocationButtonClickListener(this);
    map.animateCamera(CameraUpdateFactory.zoomTo(14), 2000, null);
}

您已经完成了新的 "MapAsync" 方法,因此您向结果迈进了一步。 为了缩放,您可以使用 googleMap 对象的 animateCamera 方法缩放到特定位置: https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.html#animateCamera(com.google.android.gms.maps.CameraUpdate) 例如,放大:

map.animateCamera(CameraUpdateFactory.zoomIn());

或者如果您已经知道 "where":

map.animateCamera(CameraUpdateFactory.newLatLngZoom(knownlocation,17));

并且您将非常接近该位置。 如果您需要在第一个 "locationUpdate" 做,您应该在第一次收到位置时将标志保持为 false 并设置为 true,然后执行缩放到位置。

编辑:如果您只想在第一次接收位置更新(从您的代码中不是很清楚)时缩放,您可以这样做,假设您有一个 class 变量,例如:

private boolean FIRST_TIME = true;

以下代码:

if(FIRST_TIME){
myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(location,17));
FIRST_TIME = false;
}

最好的选择是删除侦听器,但您似乎将位置更新用作 "blue dot" 更新。

解决方案

解决方案是执行@MikeKeepsOnShine 所说的所有操作,但删除

   Location loc = map.getMyLocation();
        LatLng point = new LatLng(loc.getLatitude() , loc.getLongitude());
        map.animateCamera(CameraUpdateFactory.newLatLngZoom(point,15));

部分来自 onMapReady。现在绝对完美!

对我有用的是:

 mMap.setMinZoomPreference(6.0f);
 mMap.setMaxZoomPreference(20.0f);

您可以根据自己的喜好修改缩放首选项值。