地图准备好后,是否可以立即更改缩放级别?当我打开应用程序时,它显示我的位置的地图和蓝点。但是,缩放级别是默认值3.如何更改?我知道如何点击“MyLocationButton”而不是应用程序启动时。
这是我的班级
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;
}
}
答案 0 :(得分:2)
解决方案是做所有事情@MikeKeepsOnShine说但是删除了
public enum CustomerTypeEnum {
CUSTOMER, SUBSCRIBER, NONE;
}
部分来自onMapReady。现在绝对完美!
答案 1 :(得分:1)
您应该使用this。你应该把这个请求放在onMapReady()回调中。像这样:
@Override
public void onMapReady(GoogleMap map) {
map.setLocationSource(this);
map.setMyLocationEnabled(true);
map.setOnMyLocationButtonClickListener(this);
map.animateCamera(CameraUpdateFactory.zoomTo(14), 2000, null);
}
答案 2 :(得分:0)
您已经完成了新的“MapAsync”方式,因此您向前迈进了一步。 要进行缩放,您可以使用googleMap对象的animateCamera方法缩放到特定位置: https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.html#animateCamera(com.google.android.gms.maps.CameraUpdate) 例如,一个zoomin:
map.animateCamera(CameraUpdateFactory.zoomIn());
或者如果您已经知道“where”:
map.animateCamera(CameraUpdateFactory.newLatLngZoom(knownlocation,17));
你会非常接近这个位置。 如果您需要在第一个“locationUpdate”执行此操作,则应将标志设置为false,并在第一次收到位置时设置为true,此时您将执行缩放到位置。
编辑:如果你想第一次缩放,你收到位置更新的地方(你的代码中并不是很清楚),你可以这样做,假设你有一个类变量,如:
private boolean FIRST_TIME = true;
以下代码:
if(FIRST_TIME){
myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(location,17));
FIRST_TIME = false;
}
最好的选择是删除侦听器,但似乎您使用位置更新作为“蓝点”更新。
答案 3 :(得分:0)
对我有用的是:
mMap.setMinZoomPreference(6.0f);
mMap.setMaxZoomPreference(20.0f);
您可以根据自己的喜好修改缩放偏好值。