GestureDemoSlice.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) Huawei Technologies Co., Ltd. 2008-2021. All rights reserved.
  3. */
  4. package com.fujica.abk.slice;
  5. import com.huawei.hms.maps.harmony.MapView;
  6. import com.huawei.hms.maps.harmony.model.LatLng;
  7. import com.huawei.hms.maps.harmony.HuaweiMap;
  8. import com.huawei.hms.maps.harmony.OnMapReadyCallback;
  9. import com.huawei.hms.maps.harmony.OnMapClickListener;
  10. import com.huawei.hms.maps.harmony.CommonContext;
  11. import ohos.aafwk.ability.AbilitySlice;
  12. import ohos.aafwk.content.Intent;
  13. import ohos.agp.colors.RgbColor;
  14. import ohos.agp.components.ComponentContainer;
  15. import ohos.agp.components.PositionLayout;
  16. import ohos.agp.components.element.ShapeElement;
  17. import ohos.agp.window.dialog.ToastDialog;
  18. public class GestureDemoSlice extends AbilitySlice {
  19. private HuaweiMap mHuaweiMap;
  20. /**
  21. * Declare a MapView object.
  22. */
  23. private MapView mMapView;
  24. @Override
  25. public void onStart(Intent intent) {
  26. super.onStart(intent);
  27. CommonContext.setContext(this);
  28. // Initialize MapView Object.
  29. mMapView = new MapView(this);
  30. mMapView.onCreate();
  31. // Obtains the HuaweiMap object.
  32. mMapView.getMapAsync(new OnMapReadyCallback() {
  33. @Override
  34. public void onMapReady(HuaweiMap huaweiMap) {
  35. mHuaweiMap = huaweiMap;
  36. // If mHuaweiMap is null, the program stops running.
  37. if (null == mHuaweiMap) {
  38. return;
  39. }
  40. mHuaweiMap.setOnMapClickListener(new OnMapClickListener() {
  41. @Override
  42. public void onMapClick(LatLng latLng) {
  43. new ToastDialog(CommonContext.getContext()).setText("onMapClick ").show();
  44. }
  45. });
  46. // Set whether to enable the compass.
  47. mHuaweiMap.getUiSettings().setCompassEnabled(true);
  48. // Set whether to enable the zoom controls.
  49. mHuaweiMap.getUiSettings().setZoomControlsEnabled(true);
  50. // Set whether to enable the zoom gestures.
  51. mHuaweiMap.getUiSettings().setZoomGesturesEnabled(true);
  52. // Set whether to enable the scroll gestures.
  53. mHuaweiMap.getUiSettings().setScrollGesturesEnabled(true);
  54. // Set whether to enable the tilt gestures.
  55. mHuaweiMap.getUiSettings().setTiltGesturesEnabled(true);
  56. // Set whether to enable the rotation gestures.
  57. mHuaweiMap.getUiSettings().setRotateGesturesEnabled(true);
  58. }
  59. });
  60. // Create a layout.
  61. ComponentContainer.LayoutConfig config = new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT);
  62. PositionLayout myLayout = new PositionLayout(this);
  63. myLayout.setLayoutConfig(config);
  64. ShapeElement element = new ShapeElement();
  65. element.setShape(ShapeElement.RECTANGLE);
  66. element.setRgbColor(new RgbColor(255, 255, 255));
  67. // Load the MapView object.
  68. myLayout.addComponent(mMapView);
  69. super.setUIContent(myLayout);
  70. }
  71. @Override
  72. protected void onActive() {
  73. super.onActive();
  74. if (mMapView != null) {
  75. mMapView.onResume();
  76. }
  77. }
  78. @Override
  79. protected void onInactive() {
  80. super.onInactive();
  81. if (mMapView != null) {
  82. mMapView.onPause();
  83. }
  84. }
  85. @Override
  86. protected void onBackground() {
  87. super.onBackground();
  88. if (mMapView != null) {
  89. mMapView.onStop();
  90. }
  91. }
  92. @Override
  93. protected void onForeground(Intent intent) {
  94. super.onForeground(intent);
  95. if (mMapView != null) {
  96. mMapView.onStart();
  97. }
  98. }
  99. @Override
  100. protected void onStop() {
  101. super.onStop();
  102. if (mMapView != null) {
  103. mMapView.onDestroy();
  104. }
  105. }
  106. }