华为HMS地图套件相机不会移动/动画到给定位置



我在我的应用程序中使用华为地图套件,我正在遵循他们文档中提到的步骤。。地图加载后,每次都会把我带到象牙海岸。。我不明白为什么

我的代码

private HuaweiMap hMap;
private MapView mMapView;
double lat;
double lng;

private static final String MAPVIEW_BUNDLE_KEY = "MapViewBundleKey";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
Log.i("TAG", "onCreate");
Intent i=getIntent();
double lat=i.getExtras().getDouble("lat");
double lng=i.getExtras().getDouble("lng");

mMapView = findViewById(R.id.mapView);
Bundle mapViewBundle = null;
if (savedInstanceState != null) {
mapViewBundle = savedInstanceState.getBundle(MAPVIEW_BUNDLE_KEY);
}
MapsInitializer.setApiKey("the api key");
mMapView.onCreate(mapViewBundle);
//get map instance
mMapView.getMapAsync(this);

}
@Override
public void onMapReady(HuaweiMap map) {
//get map instance in a callback method
Log.d("TAG", "onMapReady: ");
hMap = map;
hMap.getUiSettings().setZoomControlsEnabled(true);
hMap.getUiSettings().setZoomGesturesEnabled(true);
hMap.getUiSettings().setMyLocationButtonEnabled(true); //this button doesn't show on screen either
LatLng location = new LatLng(lat, lng);
hMap.addMarker(new MarkerOptions().position(location));
CameraPosition cameraPosition = new CameraPosition(location,8,2.2f,31.5f);
hMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
hMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}

我在同一个应用程序中使用谷歌地图(适用于使用gms的手机(,使用几乎相同的方法,谷歌地图一切都很好。。请帮助

问题1:

hMap.getUiSettings((.setMyLocationButtonEnabled(true(不接受效应

在使用hMap.getUiSettings((.setMyLocationButtonEnabled(true(之前,您需要使用hMap.setMyLocation Enabled(true。

  • hMap.setMyLocationEnabled(true(的函数:设置是否在地图上使用定位函数。(默认值为false。(
  • hMap.getUiSettings((.setMyLocationButtonEnabled(true(的函数:设置是否在地图上显示我的位置图标。(默认值为true。(

如果setMyLocationEnabled设置为false,则无论是否设置了setMyLocationButtonEnabled我的位置图标都不会显示,并且位置功能也不可用。因此,建议您使用hMap.setMyLocationEnabled(true(来显示我的位置图标。

有关更多信息,请参阅文档。

问题2:

地图相机总是移动到象牙海岸。

您的用法是正确的。建议您打印相关的经度和纬度,看看它们是否保持不变。

此外,moveCamera类用于直接移动摄影机,animateCamera类则用于通过动画移动摄影机。可以使用任意一个类来移动摄影机。

您可以使用animateCamera((,使用此方法,我们可以将相机从当前位置移动到我们定义的位置。

CameraPosition build = new CameraPosition.Builder()
.target(new LatLng(location.lat, location.lng))
.zoom(15)
.bearing(90)
.tilt(30)
.build();
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(build);
hMap.animateCamera(cameraUpdate);

要了解更多关于地图的信息,请访问文章

  1. 使用自定义标记和自定义信息窗口自定义地图-地图套件
  2. 绘制驾驶、骑自行车和步行方向的示例-地图套件

最新更新