| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- /*
- * Copyright (c) Huawei Technologies Co., Ltd. 2008-2021. All rights reserved.
- */
- package com.fujica.abk.slice;
- import com.fujica.abk.ResourceTable;
- import com.fujica.abk.utils.ScreenUtil;
- import com.huawei.hms.maps.harmony.HuaweiMap;
- import com.huawei.hms.maps.harmony.MapView;
- import com.huawei.hms.maps.harmony.OnMapReadyCallback;
- import com.huawei.hms.maps.harmony.CameraUpdateFactory;
- import com.huawei.hms.maps.harmony.CommonContext;
- import com.huawei.hms.maps.harmony.model.LatLng;
- import ohos.aafwk.ability.AbilitySlice;
- import ohos.aafwk.content.Intent;
- import ohos.agp.colors.RgbColor;
- import ohos.agp.components.Button;
- import ohos.agp.components.Component;
- import ohos.agp.components.ComponentContainer;
- import ohos.agp.components.PositionLayout;
- import ohos.agp.components.LayoutScatter;
- import ohos.agp.components.element.ShapeElement;
- import com.huawei.hms.maps.harmony.model.Polyline;
- import com.huawei.hms.maps.harmony.model.PolylineOptions;
- import ohos.agp.utils.Color;
- public class PolylineDemoSlice extends AbilitySlice {
- private HuaweiMap mHuaweiMap;
- /**
- * Declare a MapView object.
- */
- private MapView mMapView;
- /**
- * Declare a Polyline object.
- */
- private Polyline mPolyline;
- private boolean strokeColorStatus = true;
- /**
- * the layout
- */
- private PositionLayout rootLayout;
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- initPositionLayout();
- initMapView();
- addButtons();
- super.setUIContent(this.rootLayout);
- }
- private void initPositionLayout() {
- rootLayout = new PositionLayout(this);
- this.rootLayout.setContentPosition(0, 0);
- this.rootLayout.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
- this.rootLayout.setHeight(ComponentContainer.LayoutConfig.MATCH_PARENT);
- ShapeElement element = new ShapeElement();
- element.setShape(ShapeElement.RECTANGLE);
- element.setRgbColor(new RgbColor(255, 255, 255));
- this.rootLayout.setBackground(element);
- }
- private void initMapView() {
- CommonContext.setContext(this);
- // Initialize MapView Object.
- mMapView = new MapView(this);
- mMapView.onCreate();
- // Obtains the HuaweiMap object.
- mMapView.getMapAsync(new OnMapReadyCallback() {
- @Override
- public void onMapReady(HuaweiMap huaweiMap) {
- mHuaweiMap = huaweiMap;
- // If mHuaweiMap is null, the program stops running.
- if (null == mHuaweiMap) {
- return;
- }
- // If mPolyline is not null, remove it from the map and then set it to null.
- if (null != mPolyline) {
- mPolyline.remove();
- mPolyline = null;
- }
- // Add a polyline to a map.
- mPolyline = mHuaweiMap.addPolyline(new PolylineOptions()
- // polyline coordinate
- .add(new LatLng(47.893478, 2.334595), new LatLng(48.993478, 3.434595),
- new LatLng(48.693478, 2.134595), new LatLng(48.793478, 2.334595))
- // Polyline Color
- .color(Color.BLUE.getValue())
- // Polyline Width
- .width(3));
- // move camera
- mHuaweiMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(48.893478, 2.334595), 6));
- }
- });
- this.rootLayout.addComponent(mMapView);
- }
- private void addButtons() {
- int height = ScreenUtil.getScreenHeight(this);
- Button buttonStrokeColor = createButton();
- buttonStrokeColor.setText("StrokeColor");
- buttonStrokeColor.setContentPosition(50, (float) height / 2);
- this.rootLayout.addComponent(buttonStrokeColor);
- buttonStrokeColor.setClickedListener(new Component.ClickedListener() {
- @Override
- public void onClick(Component component) {
- if (null == mPolyline) {
- return;
- }
- if (strokeColorStatus) {
- // Set the color of the polyline (mPolyline) to red.
- mPolyline.setColor(Color.RED.getValue());
- } else {
- // Set the color of the polyline (mPolyline) to green.
- mPolyline.setColor(Color.GREEN.getValue());
- }
- // Set the width of the polyline (mPolyline) to 10 pixels.
- mPolyline.setWidth(10);
- strokeColorStatus = !strokeColorStatus;
- }
- });
- }
- /**
- * Create button
- *
- * @return Button
- */
- private Button createButton() {
- Component component = LayoutScatter.getInstance(getContext())
- .parse(ResourceTable.Layout_button_layout, null, false);
- return (Button) component;
- }
- @Override
- protected void onActive() {
- super.onActive();
- if (mMapView != null) {
- mMapView.onResume();
- }
- }
- @Override
- protected void onInactive() {
- super.onInactive();
- if (mMapView != null) {
- mMapView.onPause();
- }
- }
- @Override
- protected void onBackground() {
- super.onBackground();
- if (mMapView != null) {
- mMapView.onStop();
- }
- }
- @Override
- protected void onForeground(Intent intent) {
- super.onForeground(intent);
- if (mMapView != null) {
- mMapView.onStart();
- }
- }
- @Override
- protected void onStop() {
- super.onStop();
- if (mMapView != null) {
- mMapView.onDestroy();
- }
- }
- }
|