weex调用原生百度地图

第一步、写一个weex的模块,继承自NSObject,实现协议WXModuleProtocol

#import 
#import "WXModuleProtocol.h"

@interface WXBaiduMapNaviModule : NSObject

@end
#import "WXBaiduMapNaviModule.h"

@implementation WXBaiduMapNaviModule
/// 绑定
@synthesize weexInstance;
/// vue.js里可以调用这个传入相关参数给原生应用
WX_EXPORT_METHOD(@selector(openBMKAPPOrBMKWebWithStartName:startLongitude:startLatitude:endName:endLongitude:endLatitude:callback:))

/// vue.js要调用的原生方法
- (void)openBMKAPPOrBMKWebWithStartName:(NSString *)startName
                         startLongitude:(double)startLongitude
                          startLatitude:(double)startLatitude
                                endName:(NSString *)endName
                           endLongitude:(double)endLongitude
                            endLatitude:(double)endLatitude
                               callback:(WXModuleCallback)callback {

    CLLocationCoordinate2D coor1;
    coor1.longitude = startLongitude;
    coor1.latitude = startLatitude;
    
    CLLocationCoordinate2D coor2;
    coor2.longitude = endLongitude;
    coor2.latitude = endLatitude;
    
    
    BMKNaviPara *para = [[BMKNaviPara alloc] init];
    
    BMKPlanNode *start = [[BMKPlanNode alloc] init];
    start.pt = coor1;
    start.name = startName;
    para.startPoint = start;
    
    // 初始化指定终点节点
    BMKPlanNode *end = [[BMKPlanNode alloc] init];
    end.pt = coor2;
    end.name = endName;
    para.endPoint = end;
    
    para.appScheme = 这里填上你在百度地图申请的key;
    [BMKNavigation openBaiduMapNavigation:para];
    /// 这个好像官网都加了,先加上
    callback(@{@"result":@"success"});
}


@end

第二步、注册导航模块

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/// ...
/// 这个名字要跟vue.js里写的一样,才能调用
    [WXSDKEngine registerModule:@"mapNavigation" withClass:NSClassFromString(@"WXBaiduMapNaviModule")];

/// ...

}

第三步,weex项目中调用

/// ...
    
/// ... methods:{ goAddStore(){ /// mapNavigation这个名字要跟ios appdelegate didfinish..里的注册那个模块的名字一样 var eventModule = weex.requireModule('mapNavigation'); /// 用eventModule调用,六个参数(五个实际参数,一个callback对应 function(ret) { nativeLog(ret); } 不知道是不是有点类似 Swift里的闭包 ) eventModule.openBMKAPPOrBMKWebWithStartName('起点1',116.29022897479551,40.05732677308341,'终点2',116.421885,39.93857400000000,function(ret) { nativeLog(ret); }); }, /// ...

你可能感兴趣的:(weex调用原生百度地图)