Skip to content

Commit 5c701ea

Browse files
committed
热力图和MarkerIcon
1 parent af4fa2c commit 5c701ea

36 files changed

+467
-97
lines changed

README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ BaiduMapManager.initSDK('sIMQlfmOXhQmPLF1QMh4aBp8zZO9Lb2A');
115115
| onMapPoiClick | func | undefined|
116116

117117
#### Overlay 覆盖物
118-
const { Marker, Cluster, Arc, Circle, Polyline, Polygon, Text, InfoWindow } = Overlay;
118+
const { Marker, Cluster, Arc, Circle, Polyline, Polygon, InfoWindow, HeatMap } = Overlay;
119119

120120
##### 颜色取值说明
121121
6 位(RRGGBB)或 8 位(AARRGGBB)
@@ -175,20 +175,35 @@ BaiduMapManager.initSDK('sIMQlfmOXhQmPLF1QMh4aBp8zZO9Lb2A');
175175
| rotate | float | |
176176
| location | object|{latitude: 0, longitude: 0}
177177

178+
179+
##### MarkerIcon 使用 View 作为 marker 的 icon
180+
178181
##### InfoWindow Props 属性
179182
必须作为 Marker 的子组件
180183

181184
| Prop | Type | Default | Description
182185
| ----------------------- |:-----:| :-------:| -------
183186
| offsetY | int | 0 | 相对于 point 在 y 轴的偏移量,仅 Android
184187

188+
#### HeatMap Props 属性
189+
190+
| Prop | Type | Default | Description
191+
| ----------------------- |:-----:| :-------:| -------
192+
| points | array | |
193+
| gradient | object| { colors: ['66FF00', 'FF0000'], startPoints: [0.2, 1.0] } | 颜色渐变对象
194+
185195
```jsx
186196
<MapView>
187197
<Marker/>
188198
<Marker>
189-
<InfoWindow>
199+
<InfoWindow style={{ backgroundColor: 'red', width: 200, height: 100}}>
190200
<View />
191201
</InfoWindow>
202+
<MarkerIcon style={{ backgroundColor: 'red', width: 40, height: 40}}>
203+
<View>
204+
<Text>ABC</Text>
205+
</View>
206+
</MarkerIcon>
192207
</Marker>
193208
<Cluster>
194209
<Marker/>
@@ -197,7 +212,8 @@ BaiduMapManager.initSDK('sIMQlfmOXhQmPLF1QMh4aBp8zZO9Lb2A');
197212
<Circle />
198213
<Polyline />
199214
<Polygon />
200-
<Text />
215+
<Overlay.Text>text</Overlay.Text>
216+
<HeatMap />
201217
</MapView>
202218
```
203219

android/src/main/java/org/lovebing/reactnative/baidumap/BaiduMapPackage.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.lovebing.reactnative.baidumap.module.GetDistanceModule;
2828
import org.lovebing.reactnative.baidumap.module.MapAppModule;
2929
import org.lovebing.reactnative.baidumap.uimanager.*;
30+
import org.lovebing.reactnative.baidumap.view.OverlayMarkerIcon;
3031

3132

3233
/**
@@ -53,6 +54,7 @@ public List<ViewManager> createViewManagers(
5354
new MapViewManager(),
5455
new OverlayClusterManager(),
5556
new OverlayMarkerManager(),
57+
new OverlayMarkerIconManager(),
5658
new OverlayOverlayInfoWindowManager(),
5759
new OverlayArcManager(),
5860
new OverlayCircleManager(),

android/src/main/java/org/lovebing/reactnative/baidumap/listener/MapListener.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
package org.lovebing.reactnative.baidumap.listener;
99

10+
import android.util.Log;
11+
1012
import com.baidu.mapapi.map.BaiduMap;
1113
import com.baidu.mapapi.map.InfoWindow;
1214
import com.baidu.mapapi.map.MapPoi;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2016-present, lovebing.net.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package org.lovebing.reactnative.baidumap.uimanager;
9+
10+
import androidx.annotation.NonNull;
11+
import com.facebook.react.uimanager.ThemedReactContext;
12+
import com.facebook.react.uimanager.ViewGroupManager;
13+
import org.lovebing.reactnative.baidumap.view.OverlayMarkerIcon;
14+
15+
/**
16+
* @author lovebing
17+
* @date 2020-06-07
18+
*/
19+
public class OverlayMarkerIconManager extends ViewGroupManager<OverlayMarkerIcon> {
20+
21+
@NonNull
22+
@Override
23+
public String getName() {
24+
return "BaiduMapOverlayMarkerIcon";
25+
}
26+
27+
@NonNull
28+
@Override
29+
protected OverlayMarkerIcon createViewInstance(@NonNull ThemedReactContext reactContext) {
30+
return new OverlayMarkerIcon(reactContext);
31+
}
32+
}

android/src/main/java/org/lovebing/reactnative/baidumap/uimanager/OverlayMarkerManager.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.lovebing.reactnative.baidumap.util.LatLngUtil;
2020
import org.lovebing.reactnative.baidumap.view.OverlayInfoWindow;
2121
import org.lovebing.reactnative.baidumap.view.OverlayMarker;
22+
import org.lovebing.reactnative.baidumap.view.OverlayMarkerIcon;
2223

2324
public class OverlayMarkerManager extends ViewGroupManager<OverlayMarker> {
2425

@@ -87,6 +88,8 @@ public void setFlat(OverlayMarker overlayMarker, boolean flat) {
8788
public void addView(OverlayMarker parent, View child, int index) {
8889
if (child instanceof OverlayInfoWindow) {
8990
parent.setOverlayInfoWindow((OverlayInfoWindow) child);
91+
} else if (child instanceof OverlayMarkerIcon) {
92+
parent.setIconView(child);
9093
}
9194
super.addView(parent, child, index);
9295
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2016-present, lovebing.net.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package org.lovebing.reactnative.baidumap.util;
9+
10+
import android.graphics.Bitmap;
11+
import android.graphics.Canvas;
12+
import android.graphics.Matrix;
13+
import android.view.View;
14+
15+
import com.baidu.mapapi.map.BitmapDescriptor;
16+
import com.baidu.mapapi.map.BitmapDescriptorFactory;
17+
18+
/**
19+
* @author lovebing
20+
* @date 2020-05-25
21+
*/
22+
public class BitmapUtil {
23+
24+
public static BitmapDescriptor createBitmapDescriptor(View view) {
25+
return createBitmapDescriptor(view, 0, 0);
26+
}
27+
28+
public static BitmapDescriptor createBitmapDescriptor(View view, int width, int height) {
29+
if (width > 0 && height > 0) {
30+
view.layout(0, 0, width, height);
31+
} else if (view.getMeasuredWidth() == 0 || view.getMeasuredHeight() == 0) {
32+
view.layout(0, 0, view.getMeasuredWidth() > 0 ? view.getMeasuredWidth() : 50, view.getMeasuredHeight() > 0 ? view.getMeasuredHeight() : 100);
33+
}
34+
view.buildDrawingCache();
35+
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(view.getDrawingCache());
36+
view.destroyDrawingCache();
37+
return bitmapDescriptor;
38+
}
39+
40+
public static Bitmap resizeBitmap(Bitmap bitmap, int width, int height) {
41+
float scaleWidth = ((float) width) / bitmap.getWidth();
42+
float scaleHeight = ((float) height) / bitmap.getHeight();
43+
Matrix matrix = new Matrix();
44+
matrix.postScale(scaleWidth, scaleHeight);
45+
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
46+
}
47+
}

android/src/main/java/org/lovebing/reactnative/baidumap/view/OverlayHeatMap.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ public void addTopMap(BaiduMap baiduMap) {
6969

7070
@Override
7171
public void removeFromMap(BaiduMap baiduMap) {
72-
heatMap.removeHeatMap();
73-
heatMap = null;
72+
if (heatMap != null) {
73+
heatMap.removeHeatMap();
74+
heatMap = null;
75+
}
7476
}
7577
}

android/src/main/java/org/lovebing/reactnative/baidumap/view/OverlayInfoWindow.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
package org.lovebing.reactnative.baidumap.view;
99

1010
import android.content.Context;
11-
1211
import com.baidu.mapapi.map.BitmapDescriptor;
13-
import com.baidu.mapapi.map.BitmapDescriptorFactory;
1412
import com.baidu.mapapi.map.InfoWindow;
1513
import com.baidu.mapapi.model.LatLng;
1614
import com.facebook.react.views.view.ReactViewGroup;
1715

16+
import org.lovebing.reactnative.baidumap.util.BitmapUtil;
17+
1818
/**
1919
* @author lovebing Created on Dec 09, 2018
2020
*/
@@ -52,18 +52,11 @@ public void setOffsetY(int offsetY) {
5252

5353
private void updateInfoWindow(LatLng location) {
5454
if (infoWindow == null) {
55-
BitmapDescriptor bitmap;
56-
if (width > 0 && height > 0) {
57-
layout(0, 0, width, height);
58-
} else if (getMeasuredWidth() == 0 || getMeasuredHeight() == 0) {
59-
layout(0, 0, getMeasuredWidth() > 0 ? getMeasuredWidth() : 50, getMeasuredHeight() > 0 ? getMeasuredHeight() : 100);
60-
}
61-
buildDrawingCache();
62-
bitmap = BitmapDescriptorFactory.fromBitmap(getDrawingCache());
63-
if (bitmap == null) {
55+
BitmapDescriptor bitmapDescriptor = BitmapUtil.createBitmapDescriptor(this, width, height);
56+
if (bitmapDescriptor == null) {
6457
return;
6558
}
66-
infoWindow = new InfoWindow(bitmap, location, offsetY, new InfoWindow.OnInfoWindowClickListener() {
59+
infoWindow = new InfoWindow(bitmapDescriptor, location, offsetY, new InfoWindow.OnInfoWindowClickListener() {
6760
@Override
6861
public void onInfoWindowClick() {
6962

android/src/main/java/org/lovebing/reactnative/baidumap/view/OverlayMarker.java

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
import android.annotation.TargetApi;
1111
import android.content.Context;
1212
import android.graphics.Bitmap;
13-
import android.graphics.Matrix;
1413
import android.graphics.drawable.Animatable;
1514
import android.net.Uri;
1615
import android.util.AttributeSet;
1716

1817
import android.util.Log;
18+
import android.view.View;
1919
import android.view.ViewGroup;
2020
import android.widget.Button;
2121

@@ -41,6 +41,7 @@
4141

4242
import org.lovebing.reactnative.baidumap.R;
4343
import org.lovebing.reactnative.baidumap.model.IconInfo;
44+
import org.lovebing.reactnative.baidumap.util.BitmapUtil;
4445

4546
import java.util.Objects;
4647

@@ -60,6 +61,7 @@ public class OverlayMarker extends ViewGroup implements OverlayView, ClusterItem
6061
private OverlayInfoWindow overlayInfoWindow;
6162
private volatile boolean loadingImage = false;
6263
private InfoWindow titleInfoWindow;
64+
private View iconView;
6365

6466
private final ControllerListener<ImageInfo> imageControllerListener =
6567
new BaseControllerListener<ImageInfo>() {
@@ -157,6 +159,14 @@ public void setOverlayInfoWindow(OverlayInfoWindow overlayInfoWindow) {
157159
this.overlayInfoWindow = overlayInfoWindow;
158160
}
159161

162+
public void setIconView(View iconView) {
163+
this.iconView = iconView;
164+
if (marker != null) {
165+
iconBitmapDescriptor = BitmapUtil.createBitmapDescriptor(iconView);
166+
marker.setIcon(iconBitmapDescriptor);
167+
}
168+
}
169+
160170
public String getTitle() {
161171
return title;
162172
}
@@ -181,14 +191,6 @@ public void setPosition(LatLng position) {
181191
}
182192
}
183193

184-
public BitmapDescriptor getIconBitmapDescriptor() {
185-
return iconBitmapDescriptor;
186-
}
187-
188-
public void setIconBitmapDescriptor(BitmapDescriptor iconBitmapDescriptor) {
189-
this.iconBitmapDescriptor = iconBitmapDescriptor;
190-
}
191-
192194
public Float getRotate() {
193195
return rotate;
194196
}
@@ -200,21 +202,13 @@ public void setRotate(Float rotate) {
200202
}
201203
}
202204

203-
public Boolean getFlat() {
204-
return flat;
205-
}
206-
207205
public void setFlat(Boolean flat) {
208206
this.flat = flat;
209207
if (marker != null) {
210208
marker.setFlat(flat);
211209
}
212210
}
213211

214-
public Boolean getPerspective() {
215-
return perspective;
216-
}
217-
218212
public void setPerspective(Boolean perspective) {
219213
this.perspective = perspective;
220214
if (marker != null) {
@@ -253,23 +247,16 @@ public void setIcon(IconInfo iconInfo) {
253247
@Override
254248
public BitmapDescriptor getBitmapDescriptor() {
255249
BitmapDescriptor result;
256-
if (getIconBitmapDescriptor() != null) {
257-
result = getIconBitmapDescriptor();
250+
if (iconBitmapDescriptor != null) {
251+
result = iconBitmapDescriptor;
258252
} else {
259253
result = BitmapDescriptorFactory.fromResource(R.mipmap.icon_gcoding);
260254
}
261255
if (iconInfo != null
262256
&& iconInfo.getWidth() > 0
263257
&& iconInfo.getHeight() > 0) {
264-
int height = result.getBitmap().getHeight();
265-
int width = result.getBitmap().getWidth();
266-
float scaleWidth = ((float) iconInfo.getWidth()) / width;
267-
float scaleHeight = ((float) iconInfo.getHeight()) / height;
268-
Matrix matrix = new Matrix();
269-
matrix.postScale(scaleWidth, scaleHeight);
270-
271-
Bitmap newBitmap = Bitmap.createBitmap(result.getBitmap(), 0, 0, width, height, matrix, true);
272-
result = BitmapDescriptorFactory.fromBitmap(newBitmap);
258+
result = BitmapDescriptorFactory.fromBitmap(BitmapUtil.resizeBitmap(result.getBitmap(),
259+
iconInfo.getWidth(), iconInfo.getHeight()));
273260
}
274261
return result;
275262
}
@@ -297,6 +284,10 @@ public void removeFromMap(BaiduMap baiduMap) {
297284
if (marker != null) {
298285
marker.remove();
299286
marker = null;
287+
overlayInfoWindow = null;
288+
titleInfoWindow = null;
289+
iconView = null;
290+
iconBitmapDescriptor = null;
300291
}
301292
}
302293

@@ -315,12 +306,13 @@ public int hashCode() {
315306
}
316307

317308
private void addOverlay(BaiduMap baiduMap) {
318-
BitmapDescriptor icon = getBitmapDescriptor();
309+
if (iconView != null) {
310+
iconBitmapDescriptor = BitmapUtil.createBitmapDescriptor(iconView);
311+
}
319312
MarkerOptions option = new MarkerOptions()
320313
.position(position)
321-
.title(getTitle())
322314
.alpha(getAlpha())
323-
.icon(icon);
315+
.icon(getBitmapDescriptor());
324316
if (rotate != null) {
325317
option.rotate(rotate);
326318
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (c) 2016-present, lovebing.net.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package org.lovebing.reactnative.baidumap.view;
9+
10+
import android.content.Context;
11+
import com.facebook.react.views.view.ReactViewGroup;
12+
13+
/**
14+
* @author lovebing
15+
* @date 2020-06-07
16+
*/
17+
public class OverlayMarkerIcon extends ReactViewGroup {
18+
19+
public OverlayMarkerIcon(Context context) {
20+
super(context);
21+
}
22+
}

0 commit comments

Comments
 (0)