<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://schemas.android.com/apk/res-auto" android:id="@+id/mapfragment_camera_gesturetype" class="com.huawei.hms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" map:cameraTargetLat="48.893478" map:cameraTargetLng="2.334595" map:cameraZoom="10" /> </androidx.constraintlayout.widget.ConstraintLayout>
public class HwMapCameraGestureTypeActivity extends AppCompatActivity implements OnMapReadyCallback,
HuaweiMap.OnCameraMoveStartedListener, HuaweiMap.OnCameraMoveListener, HuaweiMap.OnCameraIdleListener {
public static String LOG_TAG_MAP = "HW_MAP_LOG";
private SupportMapFragment mSupportMapFragment;
//HuaweiMap对象
private HuaweiMap huaweiMap;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 初始化SDK
MapsInitializer.initialize(this);
setContentView(R.layout.activity_hwmap_camera_gesturetype);
//加载地图
mSupportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapfragment_camera_gesturetype);
mSupportMapFragment.getMapAsync(this);
}
//地图初始化成功的回调方法
@Override
public void onMapReady(HuaweiMap huaweiMap) {
this.huaweiMap = huaweiMap;
//设置地图相机的移动侦听事件
this.huaweiMap.setOnCameraMoveStartedListener(this);
this.huaweiMap.setOnCameraMoveListener(this);
this.huaweiMap.setOnCameraIdleListener(this);
}
//地图相机开始移动的回调
@Override
public void onCameraMoveStarted(int i) {
Log.w(LOG_TAG_MAP, "地图相机开始移动");
}
//地图相机移动过程中的回调
@Override
public void onCameraMove() {
}
//地图相机移动结束的回调
@Override
public void onCameraIdle() {
Log.w(LOG_TAG_MAP, "地图相机结束移动");
}
}/**
* 初始值对象
* initZoomValue-屏幕中心附近的缩放级别初始值。
* initTiltValue-相机角度与垂直于地球表面的线的夹角初始值。
* initBearingValue-相机指向的方向初始值。
*/
private float initZoomValue, initTiltValue, initBearingValue;
//地图相机开始移动的回调
@Override
public void onCameraMoveStarted(int i) {
Log.w(LOG_TAG_MAP, "地图相机开始移动");
if (null != huaweiMap && null != huaweiMap.getCameraPosition()) {
//记录相机移动时的相机各个属性的初始值
initZoomValue = huaweiMap.getCameraPosition().zoom;
initTiltValue = huaweiMap.getCameraPosition().tilt;
initBearingValue = huaweiMap.getCameraPosition().bearing;
}
}//地图相机移动结束的回调
@Override
public void onCameraIdle() {
//计算用户与相机的交互手势类型
if (null != huaweiMap && null != huaweiMap.getCameraPosition()) {
//通过计算zoom的值是否发生变化 判断用户与地图交互类型是平移还是旋转
if (initZoomValue != huaweiMap.getCameraPosition().zoom) {
Log.w(LOG_TAG_MAP, "地图相机结束移动,移动类型为:缩放");
} else {
Log.w(LOG_TAG_MAP, "地图相机结束移动,移动类型为:平移");
}
//通过计算Tilt的值是否发生变化 判断用户与地图交互类型是否是倾斜手势
if (initTiltValue != huaweiMap.getCameraPosition().tilt) {
Log.w(LOG_TAG_MAP, "地图相机结束移动,移动类型为:倾斜");
}
//通过计算Bearing的值是否发生变化 判断用户与地图交互类型是否是旋转手势
if (initBearingValue != huaweiMap.getCameraPosition().bearing) {
Log.w(LOG_TAG_MAP, "地图相机结束移动,移动类型为:旋转");
}
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70003787/viewspace-2966656/ ,如需转载,请注明出处,否则将追究法律责任。