1.RCTBaiduMapViewManager.m文件下添加RCT_EXPORT_VIEW_PROPERTY(polylines, NSArray*)绘制线的数组,禁用drawLineTexture方法,修改绘制线代理方法
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{},将需要绘制的纹理图片添加到纹理图片数组中,图片的宽和高必须是2的整数幂,箭头向下
代码如下
//
// RCTBaiduMapViewManager.m
// RCTBaiduMap
//
// Created by lovebing on Aug 6, 2016.
// Copyright 2016 lovebing.org. All rights reserved.
//
#import "RCTBaiduMapViewManager.h"
@implementation RCTBaiduMapViewManager;
RCT_EXPORT_MODULE(RCTBaiduMapView)
RCT_EXPORT_VIEW_PROPERTY(mapType, int)
RCT_EXPORT_VIEW_PROPERTY(zoom, float)
RCT_EXPORT_VIEW_PROPERTY(trafficEnabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(baiduHeatMapEnabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(marker, NSDictionary*)
RCT_EXPORT_VIEW_PROPERTY(markers, NSArray*)
RCT_EXPORT_VIEW_PROPERTY(polylines, NSArray*)
RCT_EXPORT_VIEW_PROPERTY(overlayOptions, NSArray*)
RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)
RCT_CUSTOM_VIEW_PROPERTY(center, CLLocationCoordinate2D, RCTBaiduMapView) {
[view setCenterCoordinate:json ? [RCTConvert CLLocationCoordinate2D:json] : defaultView.centerCoordinate];
}
CGFloat longitude;
CGFloat latitude;
NSString * titleStr = @"";
NSString * subtitle= @"";
BMKMapView* mapViewA;
+(void)initSDK:(NSString*)key {
BMKMapManager* _mapManager = [[BMKMapManager alloc]init];
BOOL ret = [_mapManager start:key generalDelegate:nil];
if (!ret) {
NSLog(@"manager start failed!");
}
}
- (UIView *)view {
RCTBaiduMapView* mapView = [[RCTBaiduMapView alloc] init];
mapView.delegate = self;
// mapView.zoomEnabled=NO;
mapView.zoomEnabledWithTap=NO;
mapView.overlookEnabled= NO; //禁止俯视手势
return mapView;
}
-(void)mapview:(BMKMapView *)mapView
onDoubleClick:(CLLocationCoordinate2D)coordinate {
NSLog(@"onDoubleClick");
NSDictionary* event = @{
@"type": @"onMapDoubleClick",
@"params": @{
@"latitude": @(coordinate.latitude),
@"longitude": @(coordinate.longitude)
}
};
[self sendEvent:mapView params:event];
}
-(void)mapView:(BMKMapView *)mapView
onClickedMapBlank:(CLLocationCoordinate2D)coordinate {
NSLog(@"onClickedMapBlank");
NSDictionary* event = @{
@"type": @"onMapClick",
@"params": @{
@"latitude": @(coordinate.latitude),
@"longitude": @(coordinate.longitude)
}
};
[self sendEvent:mapView params:event];
}
-(void)mapViewDidFinishLoading:(BMKMapView *)mapView {
NSDictionary* event = @{
@"type": @"onMapLoaded",
@"params": @{}
};
[self sendEvent:mapView params:event];
}
-(void)mapView:(BMKMapView *)mapView
didSelectAnnotationView:(BMKAnnotationView *)view {
titleStr=[[view annotation] title];
subtitle=[[view annotation] subtitle];
longitude=[[view annotation] coordinate].longitude;
latitude=[[view annotation] coordinate].latitude;
NSLog(@"didSelectAnnotationView %@",titleStr);
mapViewA=mapView;
NSDictionary* event = @{
@"type": @"onMarkerClick",
@"params": @{
@"title": [[view annotation] title],
@"position": @{
@"latitude": @([[view annotation] coordinate].latitude),
@"longitude": @([[view annotation] coordinate].longitude)
}
}
};
[self sendEvent:mapView params:event];
}
- (void) mapView:(BMKMapView *)mapView
onClickedMapPoi:(BMKMapPoi *)mapPoi {
NSLog(@"onClickedMapPoi");
NSDictionary* event = @{
@"type": @"onMapPoiClick",
@"params": @{
@"name": mapPoi.text,
@"uid": mapPoi.uid,
@"latitude": @(mapPoi.pt.latitude),
@"longitude": @(mapPoi.pt.longitude)
}
};
[self sendEvent:mapView params:event];
}
//- (void)mapView:(BMKMapView *)mapView onClickedBMKOverlayView:(BMKOverlayView *)overlayView{
// NSLog(@"onClickedBMKOverlayView");
//}
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation {
if ([annotation isKindOfClass:[BMKPointAnnotation class]])
{
static NSString *reuseIndetifier = @"annotationReuseIndetifier";
BMKAnnotationView *annotationView = (BMKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
if (annotationView == nil)
{
annotationView = [[BMKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:reuseIndetifier];
//添加tips上的按钮
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = rightButton;
}
NSString* title=[annotation title];
NSString* subtitle=[annotation subtitle];
NSLog(@"------->%@ title:%@",subtitle,title);
if(subtitle!=nil){
BOOL result = [subtitle isEqualToString:@"故障"];
if(result){
NSLog(@"=======>%@ title:%@",subtitle,title);
annotationView.image =[UIImage imageNamed:@"map-point-red.png"];//[self createShareImage:imgView Context:@"故障"];
}else{
annotationView.image = [UIImage imageNamed:@"point.png"];
}
}
return annotationView;
}
return nil;
}
// 1.将文字添加到图片上;imageName 图片名字, text 需画的字体
- (UIImage *)createShareImage:(UIImage *)tImage Context:(NSString *)text
{
UIImage *sourceImage = tImage;
CGSize imageSize; //画的背景 大小
imageSize = [sourceImage size];
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
[sourceImage drawAtPoint:CGPointMake(0, 0)];
//获得 图形上下文
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextDrawPath(context, kCGPathStroke);
CGFloat nameFont = 8.f;
//画 自己想要画的内容
NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:nameFont]};
CGRect sizeToFit = [text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, nameFont) options:NSStringDrawingUsesDeviceMetrics attributes:attributes context:nil];
NSLog(@"图片: %f %f",imageSize.width,imageSize.height);
NSLog(@"sizeToFit: %f %f",sizeToFit.size.width,sizeToFit.size.height);
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
[text drawAtPoint:CGPointMake((imageSize.width-sizeToFit.size.width)/2,7.5) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:nameFont]}];
//返回绘制的新图形
UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
-(void)showDetails:(UIButton *)btn{
NSLog(@"showDetails %@",titleStr);
NSDictionary* event = @{
@"type": @"onMarkerPaoClick",
@"params": @{
@"title": titleStr,
@"subtitle": subtitle,
@"longitude":@(longitude),
@"latitude": @(latitude)
}
};
[self sendEvent:mapViewA params:event];
}
- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
NSLog(@"----------->regionDidChangeAnimated ");
// [self drawLineTexture:mapView];
CLLocationCoordinate2D targetGeoPt = [mapView getMapStatus].targetGeoPt;
// NSLog(@"mapStatusDidChanged %f",targetGeoPt.latitude);
//当前屏幕中心的经纬度
CGFloat centerLongitude = targetGeoPt.longitude;
CGFloat centerLatitude = targetGeoPt.latitude;
//当前屏幕显示的范围的经纬度
CLLocationDegrees pointssLongitudeDelta= mapView.region.span.longitudeDelta;
CLLocationDegrees pointssLatitudeDelta = mapView.region.span.latitudeDelta;
NSLog(@"角 %f",pointssLatitudeDelta);
if(pointssLatitudeDelta==0.0035418173204959658 ||pointssLatitudeDelta==0.005312725980466837 ){
pointssLongitudeDelta = 0.439271;
pointssLatitudeDelta = 0.611535;
}
// NSLog(@"==pointssLongitudeDelta %f",pointssLongitudeDelta);
// NSLog(@"==pointssLatitudeDelta %f",pointssLatitudeDelta);
//左上角
CGFloat leftUpLong =centerLongitude -pointssLongitudeDelta/2.0;
CGFloat leftUpLati =centerLatitude +pointssLatitudeDelta/2.0;
//右上角
CGFloat rightUpLong =centerLongitude + pointssLongitudeDelta/2.0;
CGFloat rightUpLati =centerLatitude -pointssLatitudeDelta/2.0;
//左下角
CGFloat leftDownLong =centerLongitude - pointssLongitudeDelta/2.0;
CGFloat leftDownlati =centerLatitude + pointssLatitudeDelta/2.0;
//右下角
CGFloat rightDownLong =centerLongitude + pointssLongitudeDelta/2.0;
CGFloat rightDownlati =centerLatitude - pointssLatitudeDelta/2.0;
// NSLog(@"左上角 %f,%f-------右上角 %f,%f-------左下角 %f,%f-------右下角 %f,%f-------",leftUpLong,leftUpLati,rightUpLong,rightUpLati,leftDownLong,leftDownlati,rightDownLong,rightDownlati);
NSDictionary* event = @{
@"type": @"onMapStatusChangeFinish",
@"params": @{
@"target": @{
@"latitude": @(targetGeoPt.latitude),
@"longitude": @(targetGeoPt.longitude)
},
@"zoom": @(mapView.zoomLevel),
@"leftUpLong":@(leftUpLong),
@"leftUpLati":@(leftUpLati),
@"rightUpLong":@(rightUpLong),
@"rightUpLati":@(rightUpLati),
@"leftDownLong":@(leftDownLong),
@"leftDownlati":@(leftDownlati),
@"rightDownLong":@(rightDownLong),
@"rightDownlati":@(rightDownlati),
@"overlook": @""
}
};
[self sendEvent:mapView params:event];
}
- (void)drawLineTexture:(BMKMapView *)mapView{
//构建顶点数组
CLLocationCoordinate2D coords[5] = {0};
coords[0].latitude = 39.965;
coords[0].longitude = 116.404;
coords[1].latitude = 39.925;
coords[1].longitude = 116.454;
coords[2].latitude = 39.955;
coords[2].longitude = 116.494;
coords[3].latitude = 39.905;
coords[3].longitude = 116.654;
coords[4].latitude = 39.965;
coords[4].longitude = 116.704;
//构建分段纹理索引数组
NSArray *textureIndex = [NSArray arrayWithObjects:
[NSNumber numberWithInt:0],
[NSNumber numberWithInt:0],
[NSNumber numberWithInt:0],
[NSNumber numberWithInt:0], nil];
//构建BMKPolyline,使用分段纹理
BMKPolyline *polyLine = [BMKPolyline polylineWithCoordinates:coords count:5 textureIndex:textureIndex];
//添加分段纹理绘制折线覆盖物
[mapView addOverlay:polyLine];
}
//绘制线代理方法
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{
//绘制带纹理的线的设置
if ([overlay isKindOfClass:[BMKPolyline class]]) {
BMKPolylineView *polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
polylineView.lineWidth = 8;
polylineView.isFocus = YES;// 是否分段纹理绘制(突出显示),默认YES
//加载分段纹理图片,必须否则不能进行分段纹理绘制
[polylineView loadStrokeTextureImages:
[NSArray arrayWithObjects:[UIImage imageNamed:@"road_green_arrow.png"],nil]];
return polylineView;
}
return nil;
}
-(void)mapStatusDidChanged: (BMKMapView *)mapView {
CLLocationCoordinate2D targetGeoPt = [mapView getMapStatus].targetGeoPt;
// NSLog(@"mapStatusDidChanged %f",targetGeoPt.latitude);
//当前屏幕中心的经纬度
CGFloat centerLongitude = targetGeoPt.longitude;
CGFloat centerLatitude = targetGeoPt.latitude;
//当前屏幕显示的范围的经纬度
CLLocationDegrees pointssLongitudeDelta= mapView.region.span.longitudeDelta;
CLLocationDegrees pointssLatitudeDelta = mapView.region.span.latitudeDelta;
NSLog(@"角 %f",pointssLatitudeDelta);
if(pointssLatitudeDelta==0.0035418173204959658 ||pointssLatitudeDelta==0.005312725980466837 ){
pointssLongitudeDelta = 0.439271;
pointssLatitudeDelta = 0.611535;
}
// NSLog(@"==pointssLongitudeDelta %f",pointssLongitudeDelta);
// NSLog(@"==pointssLatitudeDelta %f",pointssLatitudeDelta);
//左上角
CGFloat leftUpLong =centerLongitude -pointssLongitudeDelta/2.0;
CGFloat leftUpLati =centerLatitude +pointssLatitudeDelta/2.0;
//右上角
CGFloat rightUpLong =centerLongitude + pointssLongitudeDelta/2.0;
CGFloat rightUpLati =centerLatitude -pointssLatitudeDelta/2.0;
//左下角
CGFloat leftDownLong =centerLongitude - pointssLongitudeDelta/2.0;
CGFloat leftDownlati =centerLatitude + pointssLatitudeDelta/2.0;
//右下角
CGFloat rightDownLong =centerLongitude + pointssLongitudeDelta/2.0;
CGFloat rightDownlati =centerLatitude - pointssLatitudeDelta/2.0;
// NSLog(@"左上角 %f,%f-------右上角 %f,%f-------左下角 %f,%f-------右下角 %f,%f-------",leftUpLong,leftUpLati,rightUpLong,rightUpLati,leftDownLong,leftDownlati,rightDownLong,rightDownlati);
NSDictionary* event = @{
@"type": @"onMapStatusChange",
@"params": @{
@"target": @{
@"latitude": @(targetGeoPt.latitude),
@"longitude": @(targetGeoPt.longitude)
},
@"zoom": @(mapView.zoomLevel),
@"leftUpLong":@(leftUpLong),
@"leftUpLati":@(leftUpLati),
@"rightUpLong":@(rightUpLong),
@"rightUpLati":@(rightUpLati),
@"leftDownLong":@(leftDownLong),
@"leftDownlati":@(leftDownlati),
@"rightDownLong":@(rightDownLong),
@"rightDownlati":@(rightDownlati),
@"overlook": @""
}
};
[self sendEvent:mapView params:event];
}
-(void)sendEvent:(RCTBaiduMapView *) mapView params:(NSDictionary *) params {
if (!mapView.onChange) {
return;
}
mapView.onChange(params);
}
@end
2.RCTBaiduMap.m文件中声明实例变量_polyLine,添加setPolylines方法,判断_polyLine是否为空,不为空的情况下先要调用removeOverlay:方法将原有的覆盖物删除
RCTBaiduMap.m代码如下
//
// RCTBaiduMap.m
// RCTBaiduMap
//
// Created by lovebing on 4/17/2016.
// Copyright 2016 lovebing.org. All rights reserved.
//
#import "RCTBaiduMapView.h"
@implementation RCTBaiduMapView {
BMKMapView* _mapView;
BMKPointAnnotation* _annotation;
BMKPolyline *_polyLine;
NSMutableArray* _annotations;
}
-(void)setZoom:(float)zoom {
self.zoomLevel = zoom;
}
-(void)setCenterLatLng:(NSDictionary *)LatLngObj {
double lat = [RCTConvert double:LatLngObj[@"lat"]];
double lng = [RCTConvert double:LatLngObj[@"lng"]];
CLLocationCoordinate2D point = CLLocationCoordinate2DMake(lat, lng);
self.centerCoordinate = point;
}
-(void)setMarker:(NSDictionary *)option {
NSLog(@"setMarker");
if(option != nil) {
if(_annotation == nil) {
_annotation = [[BMKPointAnnotation alloc]init];
[self addMarker:_annotation option:option];
}
else {
[self updateMarker:_annotation option:option];
}
}
}
-(void)setMarkers:(NSArray *)markers {
int markersCount = [markers count];
if(_annotations == nil) {
_annotations = [[NSMutableArray alloc] init];
}
if(markers != nil) {
for (int i = 0; i < markersCount; i++) {
NSDictionary *option = [markers objectAtIndex:i];
BMKPointAnnotation *annotation = nil;
if(i < [_annotations count]) {
annotation = [_annotations objectAtIndex:i];
}
if(annotation == nil) {
annotation = [[BMKPointAnnotation alloc]init];
[self addMarker:annotation option:option];
[_annotations addObject:annotation];
}
else {
[self updateMarker:annotation option:option];
}
}
int _annotationsCount = [_annotations count];
// NSString *smarkersCount = [NSString stringWithFormat:@"%d", markersCount];
// NSString *sannotationsCount = [NSString stringWithFormat:@"%d", _annotationsCount];
// NSLog(@"smarkersCount:%@",smarkersCount);
// NSLog(@"sannotationsCount:%@",sannotationsCount);
if(markersCount < _annotationsCount) {
int start = _annotationsCount - 1;
for(int i = start; i >= markersCount; i--) {
BMKPointAnnotation *annotation = [_annotations objectAtIndex:i];
[self removeAnnotation:annotation];
[_annotations removeObject:annotation];
}
}
}
}
- (void)setPolylines:(NSArray *)polylines {
if (_polyLine) {
[self removeOverlay:_polyLine];
}
int polylinesCount = [polylines count];
CLLocationCoordinate2D coords[polylinesCount];
NSMutableArray *textureIndex = [[NSMutableArray alloc] init];
if (polylines != nil && ![polylines isKindOfClass:[NSNull class]] && polylines.count != 0) {
for (int i=0; i < polylinesCount; i++) {
NSDictionary *option = [polylines objectAtIndex:i];
coords[i] = [self getCoorFromMarkerOption:option];
if (i < polylinesCount-1) {
[textureIndex addObject:[NSNumber numberWithInt:0]];
}
}
//构建BMKPolyline,使用分段纹理
_polyLine = [BMKPolyline polylineWithCoordinates:coords count:polylinesCount textureIndex:textureIndex];
//添加分段纹理绘制折线覆盖物
[self addOverlay:_polyLine];
}
}
-(void)setOverlayOptions:(NSArray *)Options {
}
-(CLLocationCoordinate2D)getCoorFromMarkerOption:(NSDictionary *)option {
double lat = [RCTConvert double:option[@"latitude"]];
double lng = [RCTConvert double:option[@"longitude"]];
CLLocationCoordinate2D coor;
coor.latitude = lat;
coor.longitude = lng;
return coor;
}
-(void)addMarker:(BMKPointAnnotation *)annotation option:(NSDictionary *)option {
[self updateMarker:annotation option:option];
[self addAnnotation:annotation];
}
-(void)updateMarker:(BMKPointAnnotation *)annotation option:(NSDictionary *)option {
CLLocationCoordinate2D coor = [self getCoorFromMarkerOption:option];
NSString *title = [RCTConvert NSString:option[@"title"]];
NSString *subtitle = [RCTConvert NSString:option[@"subtitle"]];
if(title.length == 0) {
title = nil;
}
if(subtitle.length == 0) {
subtitle = nil;
}
annotation.coordinate = coor;
annotation.title = title;
annotation.subtitle=subtitle;
// annotation.ID=ID;
}
@end