20
20
* SOFTWARE.
21
21
*/
22
22
23
- import { RNViewBase, Tag, RNComponentContext, ComponentBuilderContext} from '@rnoh/react-native-openharmony';
24
-
23
+ import { RNViewBase, Tag, RNComponentContext } from '@rnoh/react-native-openharmony';
25
24
import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl';
26
25
import common from '@ohos.app.ability.common';
27
- import {map, mapCommon, MapComponent} from '@kit.MapKit'
26
+ import { map, mapCommon, MapComponent } from '@kit.MapKit'
28
27
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
29
28
import { bundleManager } from '@kit.AbilityKit';
30
-
31
29
import { mapsComponentFactoryBuilder } from '../MapsComponentFactory'
32
- import { Camera, DEFAULT_ZOOM, EdgePadding, LatLng, Region, TAG } from '../sharedTypes';
30
+ import {
31
+ Camera,
32
+ DEFAULT_ZOOM,
33
+ HmMapStyleElement,
34
+ MapStyleElement,
35
+ Region
36
+ } from '../sharedTypes';
33
37
import { MapsManager } from '../MapsManager';
34
38
import { AIRMapDescriptor } from './AIRMapDescriptorTypes';
35
39
import { MapsTurboManager } from '../MapsTurboManager';
@@ -46,6 +50,7 @@ export struct AIRMap {
46
50
tag: number = 0
47
51
@BuilderParam public renderChildren: () => void
48
52
@State descriptor: AIRMapDescriptor = {} as AIRMapDescriptor
53
+ @State loadingEnabled: boolean = false;
49
54
protected cleanUpCallbacks: (() => void)[] = []
50
55
private callback?: AsyncCallback<map.MapComponentController>;
51
56
private mapController?: map.MapComponentController = undefined;
@@ -56,8 +61,35 @@ export struct AIRMap {
56
61
this.descriptor = this.ctx.descriptorRegistry.getDescriptor<AIRMapDescriptor>(this.tag)
57
62
this.cleanUpCallbacks.push(this.ctx.descriptorRegistry.subscribeToDescriptorChanges(this.tag,
58
63
(newDescriptor) => {
59
- this.descriptor = (newDescriptor as AIRMapDescriptor)
60
- LWLog('AIRMap.subscribeToDescriptorChanges=' + JSON.stringify(newDescriptor))
64
+ this.descriptor = (newDescriptor as AIRMapDescriptor);
65
+
66
+ // 更新属性
67
+ this.mapController?.setMapType(this.getMapType());
68
+ this.mapController?.setPadding(this.descriptor.rawProps.mapPadding);
69
+ this.mapController?.setMinZoom(this.descriptor.rawProps.minZoomLevel);
70
+ this.mapController?.setMaxZoom(this.descriptor.rawProps.maxZoomLevel);
71
+ this.mapController?.setZoomGesturesEnabled(this.descriptor.rawProps.zoomEnabled);
72
+ this.mapController?.setZoomControlsEnabled(this.descriptor.rawProps.zoomControlEnabled);
73
+ this.mapController?.setRotateGesturesEnabled(this.descriptor.rawProps.rotateEnabled);
74
+ this.mapController?.setScrollGesturesEnabled(this.descriptor.rawProps.scrollEnabled);
75
+ this.mapController?.setScaleControlsEnabled(this.descriptor.rawProps.showsScale);
76
+ this.mapController?.setAlwaysShowScaleEnabled(this.descriptor.rawProps.showsScale);
77
+ this.mapController?.setTiltGesturesEnabled(this.descriptor.rawProps.pitchEnabled);
78
+ this.mapController?.setBuildingEnabled(this.descriptor.rawProps.showsBuildings);
79
+ this.mapController?.setMyLocationEnabled(this.descriptor.rawProps.showsUserLocation);
80
+ this.mapController?.setMyLocationControlsEnabled(this.descriptor.rawProps.showsMyLocationButton);
81
+ this.mapController?.setCompassControlsEnabled(this.descriptor.rawProps.showsCompass);
82
+ this.mapController?.setTrafficEnabled(this.descriptor.rawProps.showsTraffic);
83
+ this.mapController?.setDayNightMode(this.descriptor.rawProps.userInterfaceStyle === 'dark' ? mapCommon.DayNightMode.NIGHT : mapCommon.DayNightMode.DAY);
84
+ if (this.descriptor.rawProps.compassOffset) {
85
+ this.mapController?.setCompassPosition({
86
+ positionX: this.descriptor.rawProps.compassOffset.x,
87
+ positionY: this.descriptor.rawProps.compassOffset.y,
88
+ });
89
+ }
90
+ if (this.descriptor.rawProps.customMapStyle) {
91
+ this.mapController?.setCustomMapStyle({ styleContent: JSON.stringify(this.customMapStyleConversion(this.descriptor.rawProps.customMapStyle)) });
92
+ }
61
93
}
62
94
));
63
95
@@ -90,29 +122,49 @@ export struct AIRMap {
90
122
if (!err) {
91
123
// 获取地图的控制器类,用来操作地图
92
124
this.mapController = _mapController;
93
- this.mapController.setMyLocationEnabled(this.descriptor.rawProps.showsUserLocation)
125
+ this.loadingEnabled = false;
126
+
127
+ // 设置初始化结束后,需要设置的属性
128
+ this.mapController.setTrafficEnabled(this.descriptor.rawProps.showsTraffic); // 路况图层
129
+ this.mapController.setMyLocationEnabled(this.descriptor.rawProps.showsUserLocation);
130
+ this.mapController.setBuildingEnabled(this.descriptor.rawProps.showsBuildings);
131
+ if (this.descriptor.rawProps.compassOffset) {
132
+ this.mapController.setCompassPosition({
133
+ positionX: this.descriptor.rawProps.compassOffset.x,
134
+ positionY: this.descriptor.rawProps.compassOffset.y,
135
+ });
136
+ }
137
+ if (this.descriptor.rawProps.customMapStyle) {
138
+ this.mapController.setCustomMapStyle({ styleContent: JSON.stringify(this.customMapStyleConversion(this.descriptor.rawProps.customMapStyle)) });
139
+ }
140
+
94
141
if (this.mapController.isMyLocationEnabled()) {
95
142
let applyResult = 0;
96
- //检查权限
143
+ // 检查权限
97
144
for (let permission of permissions) {
98
145
let grantStatus: abilityAccessCtrl.GrantStatus = await this.checkAccessToken(permission);
99
- LWLog('AIRMap.aboutToAppear----->permission=' + permission + ' status=' + grantStatus);
100
- //abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED
146
+ LWLog('AIRMap.aboutToAppear----->permission=' + permission + ' status=' + grantStatus);
101
147
applyResult += grantStatus;
102
148
}
103
- //没有权限去申请权限
149
+ // 没有权限去申请权限
104
150
if (applyResult !== 0) {
105
151
applyResult = await this.reqPermissionsFromUser(permissions);
106
152
}
107
153
if (applyResult === 0) {
108
- LWLog('AIRMap.aboutToAppear----->权限申请通过')
154
+ LWLog('AIRMap.aboutToAppear----->权限申请通过')
109
155
// 启用我的位置图层
110
156
this.mapController?.setMyLocationEnabled(true);
157
+ this.mapController?.setMyLocationStyle({
158
+ displayType: this.descriptor.rawProps.followsUserLocation
159
+ ? mapCommon.MyLocationDisplayType.FOLLOW
160
+ : mapCommon.MyLocationDisplayType.DEFAULT
161
+ });
162
+
111
163
// 我的位置按钮
112
164
this.mapController?.setMyLocationControlsEnabled(this.descriptor.rawProps.showsMyLocationButton);
165
+
113
166
// 首次设置我的位置
114
167
geoLocationManager.getCurrentLocation().then((result) => {
115
- console.log('current location: ' + JSON.stringify(result));
116
168
// 设置用户的位置
117
169
let position: geoLocationManager.Location = {
118
170
"latitude": result.latitude,
@@ -137,7 +189,7 @@ export struct AIRMap {
137
189
}
138
190
MapsManager.getInstance().initMapComponentController(this.mapController)
139
191
140
- //mapview
192
+ // mapview
141
193
this.mapController.on("mapLoad", () => {
142
194
this.ctx.rnInstance.emitComponentEvent(this.descriptor.tag, AIR_MAP_TYPE, { type: "onMapReady" });
143
195
});
@@ -149,7 +201,7 @@ export struct AIRMap {
149
201
LWLog('AIRMap.aboutToAppear.callback.mapLongClick');
150
202
this.ctx.rnInstance.emitComponentEvent(this.descriptor.tag, AIR_MAP_TYPE, { type: "onLongPress", coordinate: latLng });
151
203
});
152
- //marker
204
+ // marker
153
205
this.mapController.on("markerClick", (marker) => {
154
206
LWLog('AIRMap.aboutToAppear.callback.markerClick');
155
207
this.ctx.rnInstance.emitComponentEvent(this.descriptor.tag, AIR_MAP_TYPE, { type: "onMarkerPress", coordinate: marker.getPosition() });
@@ -194,7 +246,7 @@ export struct AIRMap {
194
246
});
195
247
});
196
248
this.mapController.on("cameraMove", () => {
197
- //LWLog('AIRMap.aboutToAppear.callback.cameraMove');
249
+ // LWLog('AIRMap.aboutToAppear.callback.cameraMove');
198
250
});
199
251
this.mapController.on("poiClick", (poi) => {
200
252
LWLog('AIRMap.aboutToAppear.callback.poiClick', JSON.stringify(poi));
@@ -207,6 +259,7 @@ export struct AIRMap {
207
259
}
208
260
};
209
261
262
+ this.loadingEnabled = this.descriptor.rawProps.loadingEnabled ?? false;
210
263
this.mapOptions = {
211
264
mapType: this.getMapType(),
212
265
position: {
@@ -225,11 +278,12 @@ export struct AIRMap {
225
278
scaleControlsEnabled: this.descriptor.rawProps.showsScale,
226
279
alwaysShowScaleEnabled: this.descriptor.rawProps.showsScale,
227
280
scrollGesturesEnabled: this.descriptor.rawProps.scrollEnabled,
228
- tiltGesturesEnabled: true ,
281
+ tiltGesturesEnabled: this.descriptor.rawProps.pitchEnabled ,
229
282
zoomGesturesEnabled: this.descriptor.rawProps.zoomEnabled,
230
283
zoomControlsEnabled: this.descriptor.rawProps.zoomControlEnabled,
231
284
myLocationControlsEnabled: this.descriptor.rawProps.showsMyLocationButton,
232
285
compassControlsEnabled: this.descriptor.rawProps.showsCompass,
286
+ dayNightMode: this.descriptor.rawProps.userInterfaceStyle === 'dark' ? mapCommon.DayNightMode.NIGHT : mapCommon.DayNightMode.DAY,
233
287
padding: {
234
288
left: this.descriptor.rawProps.mapPadding?.left,
235
289
top: this.descriptor.rawProps.mapPadding?.top,
@@ -240,20 +294,40 @@ export struct AIRMap {
240
294
}
241
295
242
296
aboutToDisappear() {
243
- this.mapController?.off('mapLoad', ()=> {});
244
- this.mapController?.off('mapClick', ()=> {});
245
- this.mapController?.off('mapLongClick', ()=> {});
246
- this.mapController?.off('markerClick', ()=> {});
247
- this.mapController?.off('markerDrag', ()=> {});
248
- this.mapController?.off('markerDragEnd', ()=> {});
249
- this.mapController?.off('markerDragStart', ()=> {});
250
- this.mapController?.off('cameraChange', ()=> {});
251
- this.mapController?.off('cameraMove', ()=> {});
252
- this.mapController?.off('poiClick', ()=> {});
297
+ this.mapController?.off('mapLoad', () => {});
298
+ this.mapController?.off('mapClick', () => {});
299
+ this.mapController?.off('mapLongClick', () => {});
300
+ this.mapController?.off('markerClick', () => {});
301
+ this.mapController?.off('markerDrag', () => {});
302
+ this.mapController?.off('markerDragEnd', () => {});
303
+ this.mapController?.off('markerDragStart', () => {});
304
+ this.mapController?.off('cameraChange', () => {});
305
+ this.mapController?.off('cameraMove', () => {});
306
+ this.mapController?.off('poiClick', () => {});
253
307
this.cleanUpCallbacks.forEach(cb => cb())
254
308
MapsManager.getInstance().initMapComponentController(undefined);
255
309
}
256
310
311
+ customMapStyleConversion(resource: MapStyleElement[]): HmMapStyleElement[] {
312
+ const arr2obj = (arr: object[]) => {
313
+ const obj: object = new Object();
314
+ arr.forEach(arrItem => {
315
+ Object.keys(arrItem).forEach(key => {
316
+ obj[key] = arrItem[key];
317
+ });
318
+ });
319
+ return obj;
320
+ };
321
+ return resource.map(item => {
322
+ return {
323
+ mapFeature: item.featureType,
324
+ options: item.elementType,
325
+ paint: arr2obj(item.stylers),
326
+ visibility: 'inherit',
327
+ } as HmMapStyleElement;
328
+ });
329
+ }
330
+
257
331
public animateToRegion(region: Region, duration: number) {
258
332
let mapCamera = {
259
333
target: { latitude: region.latitude, longitude: region.longitude },
@@ -274,13 +348,14 @@ export struct AIRMap {
274
348
case 'terrain':
275
349
return mapCommon.MapType.TERRAIN;
276
350
default:
277
- //others not support
278
351
return undefined;
279
352
}
280
353
}
281
354
282
355
getCameraOption() {
283
- let lat: number = 39.9, lng: number = 118.9, zoom: number = DEFAULT_ZOOM;
356
+ let lat: number = 39.9089236;
357
+ let lng: number = 116.3991428;
358
+ let zoom: number = DEFAULT_ZOOM;
284
359
if (this.descriptor.rawProps.camera) {
285
360
lat = this.descriptor.rawProps.camera.center.latitude;
286
361
lng = this.descriptor.rawProps.camera.center.longitude;
@@ -295,8 +370,15 @@ export struct AIRMap {
295
370
} else if (this.descriptor.rawProps.initialRegion) {
296
371
lat = this.descriptor.rawProps.initialRegion.latitude;
297
372
lng = this.descriptor.rawProps.initialRegion.longitude;
373
+ } else {
298
374
}
299
- return { center: {latitude: lat!!, longitude: lng}, zoom: MapsTurboManager.getInstance().getCameraZoom(zoom), pitch: 1, heading: 1, altitude: 1} as Camera;
375
+ return {
376
+ center: { latitude: lat ?? 39.9089236, longitude: lng ?? 116.3991428 },
377
+ zoom: MapsTurboManager.getInstance().getCameraZoom(zoom),
378
+ pitch: 0,
379
+ heading: 0,
380
+ altitude: 0
381
+ } as Camera;
300
382
}
301
383
302
384
async reqPermissionsFromUser(permissions: Array<Permissions>): Promise<number> {
@@ -352,6 +434,12 @@ export struct AIRMap {
352
434
tag,
353
435
this.ctx.rnInstance.getComponentNameFromDescriptorType(this.ctx.descriptorRegistry.getDescriptor(tag)?.type))
354
436
}, (tag: Tag) => tag.toString())
437
+ if (this.loadingEnabled) {
438
+ LoadingProgress()
439
+ .color(this.descriptor.rawProps.loadingIndicatorColor)
440
+ .backgroundColor(this.descriptor.rawProps.loadingBackgroundColor)
441
+ .width('100%')
442
+ }
355
443
}
356
444
}
357
445
}
0 commit comments