华为 HMS 地图套件相机不会 move/animate 到给定位置
Huawei HMS map kit camera won't move/animate to given position
我在我的应用程序中使用华为地图工具包,我正在按照他们的文档中提到的步骤进行操作..地图加载并且每次都将我带到象牙海岸..我不明白为什么
我的代码
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));
}
我在同一个应用程序中使用 google 地图(适用于使用 gms 的手机)并使用几乎相同的方法,并且在 google 地图上一切正常。
请帮忙
您可以使用 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:
hMap.getUiSettings().setMyLocationButtonEnabled(true) does not take
effect.
在使用hMap.getUiSettings().setMyLocationButtonEnabled(true)之前,需要先使用hMap.setMyLocationEnabled(true)启用地图定位功能。
- 的功能hMap.setMyLocationEnabled(true):设置是否在地图上使用定位功能。 (默认值为 false。)
- hMap.getUiSettings().setMyLocationButtonEnabled(true)的作用:设置是否在地图上显示my-location图标。 (默认值为 true。)
如果setMyLocationEnabled设置为false,则my-location图标将不显示,定位功能不可用是否设置了 setMyLocationButtonEnabled。因此,建议您使用hMap.setMyLocationEnabled(true)来显示my-location图标。
详情请参考docs。
问题 2:
The map camera always moves to Ivory Coast.
您的用法是正确的。建议打印相关的经纬度,看是否不变。
此外,moveCamera class is used to directly move the camera, and the animateCamera class 用于通过动画移动相机。您可以使用任一 classes 来移动相机。
我在我的应用程序中使用华为地图工具包,我正在按照他们的文档中提到的步骤进行操作..地图加载并且每次都将我带到象牙海岸..我不明白为什么
我的代码
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));
}
我在同一个应用程序中使用 google 地图(适用于使用 gms 的手机)并使用几乎相同的方法,并且在 google 地图上一切正常。 请帮忙
您可以使用 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:
hMap.getUiSettings().setMyLocationButtonEnabled(true) does not take effect.
在使用hMap.getUiSettings().setMyLocationButtonEnabled(true)之前,需要先使用hMap.setMyLocationEnabled(true)启用地图定位功能。
- 的功能hMap.setMyLocationEnabled(true):设置是否在地图上使用定位功能。 (默认值为 false。)
- hMap.getUiSettings().setMyLocationButtonEnabled(true)的作用:设置是否在地图上显示my-location图标。 (默认值为 true。)
如果setMyLocationEnabled设置为false,则my-location图标将不显示,定位功能不可用是否设置了 setMyLocationButtonEnabled。因此,建议您使用hMap.setMyLocationEnabled(true)来显示my-location图标。
详情请参考docs。
问题 2:
The map camera always moves to Ivory Coast.
您的用法是正确的。建议打印相关的经纬度,看是否不变。
此外,moveCamera class is used to directly move the camera, and the animateCamera class 用于通过动画移动相机。您可以使用任一 classes 来移动相机。