gpt4 book ai didi

android - 在应用程序启动时更改缩放级别 Google Maps Api v2

转载 作者:太空狗 更新时间:2023-10-29 15:41:09 25 4
gpt4 key购买 nike

是否可以在 map 准备好后立即更改缩放级别?当我打开应用程序时,它会显示 map 和我所在位置的蓝点。但是,缩放级别是默认的 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 位置。通过这种方式,您可以使用给定的缩放级别将 map 置于该点的中心。

完整方法:

@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 可能为 null。

其他解决方案

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

声明它 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

关于android - 在应用程序启动时更改缩放级别 Google Maps Api v2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31408204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com