PolylineDemoSlice.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (c) Huawei Technologies Co., Ltd. 2008-2021. All rights reserved.
  3. */
  4. package com.fujica.abk.slice;
  5. import com.fujica.abk.ResourceTable;
  6. import com.fujica.abk.utils.ScreenUtil;
  7. import com.huawei.hms.maps.harmony.HuaweiMap;
  8. import com.huawei.hms.maps.harmony.MapView;
  9. import com.huawei.hms.maps.harmony.OnMapReadyCallback;
  10. import com.huawei.hms.maps.harmony.CameraUpdateFactory;
  11. import com.huawei.hms.maps.harmony.CommonContext;
  12. import com.huawei.hms.maps.harmony.model.LatLng;
  13. import ohos.aafwk.ability.AbilitySlice;
  14. import ohos.aafwk.content.Intent;
  15. import ohos.agp.colors.RgbColor;
  16. import ohos.agp.components.Button;
  17. import ohos.agp.components.Component;
  18. import ohos.agp.components.ComponentContainer;
  19. import ohos.agp.components.PositionLayout;
  20. import ohos.agp.components.LayoutScatter;
  21. import ohos.agp.components.element.ShapeElement;
  22. import com.huawei.hms.maps.harmony.model.Polyline;
  23. import com.huawei.hms.maps.harmony.model.PolylineOptions;
  24. import ohos.agp.utils.Color;
  25. public class PolylineDemoSlice extends AbilitySlice {
  26. private HuaweiMap mHuaweiMap;
  27. /**
  28. * Declare a MapView object.
  29. */
  30. private MapView mMapView;
  31. /**
  32. * Declare a Polyline object.
  33. */
  34. private Polyline mPolyline;
  35. private boolean strokeColorStatus = true;
  36. /**
  37. * the layout
  38. */
  39. private PositionLayout rootLayout;
  40. @Override
  41. public void onStart(Intent intent) {
  42. super.onStart(intent);
  43. initPositionLayout();
  44. initMapView();
  45. addButtons();
  46. super.setUIContent(this.rootLayout);
  47. }
  48. private void initPositionLayout() {
  49. rootLayout = new PositionLayout(this);
  50. this.rootLayout.setContentPosition(0, 0);
  51. this.rootLayout.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
  52. this.rootLayout.setHeight(ComponentContainer.LayoutConfig.MATCH_PARENT);
  53. ShapeElement element = new ShapeElement();
  54. element.setShape(ShapeElement.RECTANGLE);
  55. element.setRgbColor(new RgbColor(255, 255, 255));
  56. this.rootLayout.setBackground(element);
  57. }
  58. private void initMapView() {
  59. CommonContext.setContext(this);
  60. // Initialize MapView Object.
  61. mMapView = new MapView(this);
  62. mMapView.onCreate();
  63. // Obtains the HuaweiMap object.
  64. mMapView.getMapAsync(new OnMapReadyCallback() {
  65. @Override
  66. public void onMapReady(HuaweiMap huaweiMap) {
  67. mHuaweiMap = huaweiMap;
  68. // If mHuaweiMap is null, the program stops running.
  69. if (null == mHuaweiMap) {
  70. return;
  71. }
  72. // If mPolyline is not null, remove it from the map and then set it to null.
  73. if (null != mPolyline) {
  74. mPolyline.remove();
  75. mPolyline = null;
  76. }
  77. // Add a polyline to a map.
  78. mPolyline = mHuaweiMap.addPolyline(new PolylineOptions()
  79. // polyline coordinate
  80. .add(new LatLng(47.893478, 2.334595), new LatLng(48.993478, 3.434595),
  81. new LatLng(48.693478, 2.134595), new LatLng(48.793478, 2.334595))
  82. // Polyline Color
  83. .color(Color.BLUE.getValue())
  84. // Polyline Width
  85. .width(3));
  86. // move camera
  87. mHuaweiMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(48.893478, 2.334595), 6));
  88. }
  89. });
  90. this.rootLayout.addComponent(mMapView);
  91. }
  92. private void addButtons() {
  93. int height = ScreenUtil.getScreenHeight(this);
  94. Button buttonStrokeColor = createButton();
  95. buttonStrokeColor.setText("StrokeColor");
  96. buttonStrokeColor.setContentPosition(50, (float) height / 2);
  97. this.rootLayout.addComponent(buttonStrokeColor);
  98. buttonStrokeColor.setClickedListener(new Component.ClickedListener() {
  99. @Override
  100. public void onClick(Component component) {
  101. if (null == mPolyline) {
  102. return;
  103. }
  104. if (strokeColorStatus) {
  105. // Set the color of the polyline (mPolyline) to red.
  106. mPolyline.setColor(Color.RED.getValue());
  107. } else {
  108. // Set the color of the polyline (mPolyline) to green.
  109. mPolyline.setColor(Color.GREEN.getValue());
  110. }
  111. // Set the width of the polyline (mPolyline) to 10 pixels.
  112. mPolyline.setWidth(10);
  113. strokeColorStatus = !strokeColorStatus;
  114. }
  115. });
  116. }
  117. /**
  118. * Create button
  119. *
  120. * @return Button
  121. */
  122. private Button createButton() {
  123. Component component = LayoutScatter.getInstance(getContext())
  124. .parse(ResourceTable.Layout_button_layout, null, false);
  125. return (Button) component;
  126. }
  127. @Override
  128. protected void onActive() {
  129. super.onActive();
  130. if (mMapView != null) {
  131. mMapView.onResume();
  132. }
  133. }
  134. @Override
  135. protected void onInactive() {
  136. super.onInactive();
  137. if (mMapView != null) {
  138. mMapView.onPause();
  139. }
  140. }
  141. @Override
  142. protected void onBackground() {
  143. super.onBackground();
  144. if (mMapView != null) {
  145. mMapView.onStop();
  146. }
  147. }
  148. @Override
  149. protected void onForeground(Intent intent) {
  150. super.onForeground(intent);
  151. if (mMapView != null) {
  152. mMapView.onStart();
  153. }
  154. }
  155. @Override
  156. protected void onStop() {
  157. super.onStop();
  158. if (mMapView != null) {
  159. mMapView.onDestroy();
  160. }
  161. }
  162. }