//  GetDistanceModule.m
//  RCTBaiduMap
//
//  Created by sybil052 on 2017/5/5.
//  Riant 略有调整 2019/10/22
//  https://www.jianshu.com/p/bd8335c40aa3

#import "GetDistanceModule.h"
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>

@implementation GetDistanceModule

@synthesize bridge = _bridge;

RCT_EXPORT_MODULE(BaiduGetDistanceModule);

RCT_EXPORT_METHOD(getLocationDistance:(double)lat1 lng1:(double)lng1 lat2:(double)lat2 lng2:(double)lng2
                  resolver:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject){

    BMKMapPoint point1 = BMKMapPointForCoordinate(CLLocationCoordinate2DMake(lat1,lng1));
    BMKMapPoint point2 = BMKMapPointForCoordinate(CLLocationCoordinate2DMake(lat2,lng2));
    CLLocationDistance distance = BMKMetersBetweenMapPoints(point1,point2);
    NSDictionary* coor = @{
                            @"distance": [NSNumber numberWithDouble:distance]
                          };
    resolve(coor);
}

RCT_EXPORT_METHOD(getLocationConvert:(double)latitude longitude:(double)longitude       resolver:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject){

    CLLocationCoordinate2D gcj02Coord = CLLocationCoordinate2DMake(latitude, longitude);
    // 转为百度经纬度类型的坐标
    CLLocationCoordinate2D bd09Coord = BMKCoordTrans(gcj02Coord,BMK_COORDTYPE_BD09LL, BMK_COORDTYPE_COMMON);
    NSDictionary* coor = @{
                            @"latitude": [NSNumber numberWithDouble:bd09Coord.latitude],
                            @"longitude": [NSNumber numberWithDouble:bd09Coord.longitude]
                          };
    resolve(coor);
}

@end
