Skip to content

Commit

Permalink
icon image 复用; Marker.animateType; BaiduMapManager.hasLocationPermission
Browse files Browse the repository at this point in the history
  • Loading branch information
lovebing committed Jun 14, 2020
1 parent d008535 commit a0c8030
Show file tree
Hide file tree
Showing 18 changed files with 167 additions and 41 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,14 @@ BaiduMapManager.initSDK('sIMQlfmOXhQmPLF1QMh4aBp8zZO9Lb2A');
| Prop | Type | Default | Description
| ----------------------- |:-----:| :-------:| -------
| title | string| null | 如果没有 InfoWindow,将会根据 title 生成 InfoWindow
| titleOffsetY | int | -80 | title 作为 InfoWindow 展示的 y 轴偏移量,仅 Android
| titleOffsetY | int | -80 | title 作为 InfoWindow 展示的 y 轴偏移量,仅 Android
| location | object| {latitude: 0, longitude: 0} |
| perspective | bool | null | 仅 Android
| flat | bool | null | 仅 Android
| rotate | float | 0 | 旋转角度,仅 Android
| icon | any | null | icon图片,同 <Image> 的 source 属性
| alpha | float | 1 | 透明度,仅 Android
| animateType | string| | 动画效果:drop/grow/jump (iOS 仅支持 drop)
| pinColor | string| red | red/green/purple,大头针颜色,仅 iOS
| onClick | func | | 点击事件回调
##### Cluster 点聚合
Expand Down Expand Up @@ -240,6 +241,13 @@ Cluster 示例
</MapView>
```

#### BaiduMapManager

| Method | Description | Result
| ------------------------- | ---------------- | -------
| void initSDK(string apiKey) | iOS 初始化 SDK |
| Promise hasLocationPermission | 是否有定位权限 |

#### Geolocation Methods

| Method | Description | Result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@

package org.lovebing.reactnative.baidumap.module;

import android.Manifest;
import android.util.Log;
import androidx.annotation.NonNull;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactMethod;

import org.lovebing.reactnative.baidumap.support.AppUtils;

/**
* @author lovebing
* @date 2019/10/30
Expand All @@ -33,4 +38,8 @@ public void initSDK(String key) {
Log.i("initSDK", key);
}

@ReactMethod
public void hasLocationPermission(Promise promise) {
promise.resolve(AppUtils.hasPermission(context.getCurrentActivity(), Manifest.permission.ACCESS_FINE_LOCATION));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public String getName() {
}

private void initLocationClient(String coorType) {
if(context.getCurrentActivity()!=null) {
if(context.getCurrentActivity() != null) {
AppUtils.checkPermission(context.getCurrentActivity(), Manifest.permission.ACCESS_FINE_LOCATION);
}
LocationClientOption option = new LocationClientOption();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ public class AppUtils {
public static void checkPermission(Activity activity, String permission) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (activity.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(activity, permission)
!= PackageManager.PERMISSION_GRANTED) {
if (!hasPermission(activity, permission)) {
ActivityCompat.requestPermissions(activity, new String[]{permission}, RequestCode.CODE_ASK_PERMISSIONS);
}
}
}
}

public static boolean hasPermission(Activity activity, String permission) {
return ContextCompat.checkSelfPermission(activity, permission) == PackageManager.PERMISSION_GRANTED;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public static LatLng convert(LocationData locationData) {
}

public static <T> T convert(ReadableMap readableMap, Class<T> targetClass) {
if (readableMap == null) {
return null;
}
if (!FILED_MAP.containsKey(targetClass)) {
FILED_MAP.put(targetClass, targetClass.getDeclaredFields());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public void setTitle(OverlayMarker overlayMarker, String title) {
overlayMarker.setTitle(title);
}

@ReactProp(name = "animateType")
public void setAnimateType(OverlayMarker overlayMarker, String animateType) {
overlayMarker.setAnimateType(animateType);
}

@ReactProp(name = "titleOffsetY")
public void setTitleOffsetY(OverlayMarker overlayMarker, int titleOffsetY) {
overlayMarker.setTitleOffsetY(titleOffsetY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,18 @@
import org.lovebing.reactnative.baidumap.model.IconInfo;
import org.lovebing.reactnative.baidumap.util.BitmapUtil;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

public class OverlayMarker extends ViewGroup implements OverlayView, ClusterItem {

// TODO 1. 下载中的情况。 2. 清理零引用的 key
private static final Map<String, BitmapDescriptor> BITMAP_DESCRIPTOR_MAP = new ConcurrentHashMap<>();

private String title;
private int titleOffsetY = -100;
private MarkerOptions.MarkerAnimateType animateType = MarkerOptions.MarkerAnimateType.none;
private LatLng position;
private Float rotate;
private Boolean flat;
Expand All @@ -66,10 +72,8 @@ public class OverlayMarker extends ViewGroup implements OverlayView, ClusterItem
private final ControllerListener<ImageInfo> imageControllerListener =
new BaseControllerListener<ImageInfo>() {
@Override
public void onFinalImageSet(
String id,
final ImageInfo imageInfo,
Animatable animatable) {
public void onFinalImageSet(String id, final ImageInfo imageInfo, Animatable animatable) {
Log.i("onFinalImageSet", id);
CloseableReference<CloseableImage> imageReference = null;
try {
imageReference = dataSource.getResult();
Expand All @@ -81,6 +85,7 @@ public void onFinalImageSet(
if (bitmap != null) {
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
iconBitmapDescriptor = BitmapDescriptorFactory.fromBitmap(bitmap);
BITMAP_DESCRIPTOR_MAP.put(iconInfo.getUri(), iconBitmapDescriptor);
}
}
}
Expand Down Expand Up @@ -167,8 +172,26 @@ public void setIconView(View iconView) {
}
}

public String getTitle() {
return title;
public void setAnimateType(String animateType) {
if (animateType == null) {
return;
}
switch (animateType) {
case "drop":
this.animateType = MarkerOptions.MarkerAnimateType.drop;
break;
case "grow":
this.animateType = MarkerOptions.MarkerAnimateType.grow;
break;
case "jump":
this.animateType = MarkerOptions.MarkerAnimateType.jump;
break;
default:
this.animateType = MarkerOptions.MarkerAnimateType.none;
}
if (marker != null) {
marker.setAnimateType(this.animateType.ordinal());
}
}

public void setTitle(String title) {
Expand Down Expand Up @@ -220,6 +243,10 @@ public void setIcon(IconInfo iconInfo) {
if (iconInfo.getUri() == null || iconInfo.getUri().length() == 0) {
return;
}
if (BITMAP_DESCRIPTOR_MAP.containsKey(iconInfo.getUri())) {
iconBitmapDescriptor = BITMAP_DESCRIPTOR_MAP.get(iconInfo.getUri());
return;
}
Log.i("download", iconInfo.getUri());
this.iconInfo = iconInfo;
String uri = iconInfo.getUri();
Expand Down Expand Up @@ -312,6 +339,7 @@ private void addOverlay(BaiduMap baiduMap) {
MarkerOptions option = new MarkerOptions()
.position(position)
.alpha(getAlpha())
.animateType(animateType)
.icon(getBitmapDescriptor());
if (rotate != null) {
option.rotate(rotate);
Expand Down
80 changes: 57 additions & 23 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ declare namespace ReactNativeBaiduMap {
longitude: number;
}

export interface Stroke {
width: number;
color: string;
}

/**
* 颜色渐变对象
*/
export interface Gradient {
colors: Array<string>;
startPoints: Array<number>
}

type CommonCallBack<T> = (l: T) => void;

/**
Expand Down Expand Up @@ -176,6 +189,11 @@ declare namespace ReactNativeBaiduMap {
*/
icon?: ImageSourcePropType;

/**
* 动画效果:drop/grow/jump (iOS 仅支持 drop)
*/
animateType?: string;

/**
* @default 0
*/
Expand All @@ -190,19 +208,19 @@ declare namespace ReactNativeBaiduMap {

export interface ArcProps extends ViewProps {
/**
* @default 'AA00FF00'
* @default {width: 5, color: 'AA000000'}
*/
color?: string;
stroke: Stroke;

/**
* @default 4
* 数值长度必须为 3
*/
width?: number;
points: [Location, Location, Location];

/**
* 数值长度必须为 3
* 是否为虚线,仅 iOS
*/
poins: [Location, Location, Location];
dash: boolean;
}

export class Arc extends Component<ArcProps> {
Expand All @@ -227,23 +245,18 @@ declare namespace ReactNativeBaiduMap {
center: Location;
}

export interface CircleProps extends ViewProps {}
export interface CircleProps extends ViewProps {
radius: number;
fillColor: string;
stroke: Stroke;
center: Location;
}

export class Circle extends Component<CircleProps> {}

export interface PolylineProps extends ViewProps {
points: Location[];

/**
* 8位(AARRGGBB)
* @default 'AAFF0000'
*/
strokeColor?: string;

/**
* @default 1
*/
lineWidth?: number;
stroke: Stroke;
}

export class Polyline extends Component<PolylineProps> {}
Expand Down Expand Up @@ -283,15 +296,23 @@ declare namespace ReactNativeBaiduMap {
export class Text extends Component<TextProps> {}

export interface InfoWindowProps extends ViewProps {
location: Location;

/**
* @default false
* 相对于 point 在 y 轴的偏移量,仅 Android
*/
visible?: boolean;
offsetY: number;
}

export class InfoWindowProps extends Component<TextProps> {}
export class InfoWindow extends Component<InfoWindowProps> {}

export interface HeatMapProps extends ViewProps {
points: Array<Location>;
gradient: Gradient;
}

/**
* 自定义热力图
*/
export class HeatMap extends Component<HeatMapProps> {}
}

export namespace Geolocation {
Expand Down Expand Up @@ -431,6 +452,19 @@ declare namespace ReactNativeBaiduMap {
*/
export function openWalkNavi(sl: OpenLocation, el: OpenLocation): void;
}

export namespace BaiduMapManager {
/**
* iOS 初始化 SDK
* @param appKey
*/
export function initSDK(appKey: string): void;

/**
* 是否有定位权限
*/
export function hasLocationPermission(): Promise<boolean>;
}
}

export = ReactNativeBaiduMap;
2 changes: 0 additions & 2 deletions ios/RCTBaiduMap/BMKPointAnnotationPro.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#import "BMKPointAnnotationPro.h"

@implementation BMKPointAnnotationPro {
BMKActionPaopaoView *_pView;
BMKActionPaopaoView *_paopaoView;
}

Expand All @@ -18,7 +17,6 @@ @implementation BMKPointAnnotationPro {
- (instancetype)init {
self = [super init];
_annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:self reuseIdentifier:markerIdentifier];
_annotationView.animatesDrop = YES;
_customPopView = [[UIView alloc] init];
_paopaoView = [[BMKActionPaopaoView alloc] initWithCustomView:_customPopView];
return self;
Expand Down
4 changes: 4 additions & 0 deletions ios/RCTBaiduMap/GeolocationModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,8 @@ - (void)sendEvent:(NSString *)name body:(NSMutableDictionary *)body {
[self.bridge.eventDispatcher sendDeviceEventWithName:name body:body];
}

+ (BOOL)requiresMainQueueSetup {
return YES;
}

@end
1 change: 1 addition & 0 deletions ios/RCTBaiduMap/Modules/BaiduMapManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#import <BMKLocationkit/BMKLocationComponent.h>
#import <BaiduMapAPI_Map/BMKMapView.h>
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>
#import <CoreLocation/CLLocationManager.h>

@interface BaiduMapManager : NSObject<RCTBridgeModule>

Expand Down
10 changes: 10 additions & 0 deletions ios/RCTBaiduMap/Modules/BaiduMapManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,14 @@ @implementation BaiduMapManager
}
}

RCT_REMAP_METHOD(hasLocationPermission,
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
resolve(@NO);
} else {
resolve(@YES);
}
}

@end
1 change: 1 addition & 0 deletions ios/RCTBaiduMap/ViewManagers/OverlayMarkerManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ @implementation OverlayMarkerManager
RCT_EXPORT_VIEW_PROPERTY(location, NSDictionary*)
RCT_EXPORT_VIEW_PROPERTY(title, NSString*)
RCT_EXPORT_VIEW_PROPERTY(icon, RCTImageSource*)
RCT_EXPORT_VIEW_PROPERTY(animateType, NSString*)
RCT_EXPORT_VIEW_PROPERTY(pinColor, NSString*)
RCT_EXPORT_VIEW_PROPERTY(onClick, RCTBubblingEventBlock)

Expand Down
1 change: 1 addition & 0 deletions ios/RCTBaiduMap/Views/OverlayMarker.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) RCTImageSource *icon;
@property (nonatomic, strong) BMKPointAnnotationPro *annotation;
@property (nonatomic, strong) NSString *animateType;
@property (nonatomic, strong) NSString *pinColor;

@property (nonatomic, strong) BMKActionPaopaoView *paopaoView;
Expand Down
Loading

0 comments on commit a0c8030

Please sign in to comment.