【问题标题】:Huawei HMS map kit camera won't move/animate to given position华为 HMS 地图套件相机不会移动/动画到给定位置
【发布时间】:2020-09-14 18:39:58
【问题描述】:

我在我的应用程序中使用华为地图套件,我正在按照他们文档中提到的步骤进行操作..地图加载并且每次都将我移动到象牙海岸..我不明白为什么

我的代码

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 的手机)并使用几乎相同的方法,一切都与谷歌地图完美配合。 请帮忙

【问题讨论】:

标签: android google-maps kotlin huawei-mobile-services huawei-map-kit


【解决方案1】:

问题一:

hMap.getUiSettings().setMyLocationButtonEnabled(true) 不需要 效果。

在使用hMap.getUiSettings().setMyLocationButtonEnabled(true)之前,需要先使用hMap.setMyLocationEnabled(true)来开启地图上的定位功能。

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

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

更多信息请参考docs

问题 2:

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

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

另外moveCamera类用于直接移动相机,animateCamera类用于通过动画移动相机。您可以使用任一类来移动相机。

【讨论】:

    【解决方案2】:

    你可以使用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. To customize Maps with Custom Marker and Custom Info Window - Maps Kit
    2. Example of Drawing Directions for Driving, Bicycling and Walking - Maps Kit

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多