提交 4ef29091 authored 作者: 张国庆's avatar 张国庆

增加计算2点距离

上级 284d2f06
......@@ -16,7 +16,9 @@ import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
/** 高德地图定位sdkFlutterPlugin */
/**
* 高德地图定位sdkFlutterPlugin
*/
public class AMapFlutterLocationPlugin implements FlutterPlugin, MethodCallHandler,
EventChannel.StreamHandler {
private static final String CHANNEL_METHOD_LOCATION = "amap_flutter_location";
......@@ -34,7 +36,7 @@ public class AMapFlutterLocationPlugin implements FlutterPlugin, MethodCallHandl
String callMethod = call.method;
switch (call.method) {
case "updatePrivacyStatement":
updatePrivacyStatement((Map)call.arguments);
updatePrivacyStatement((Map) call.arguments);
break;
case "setApiKey":
setApiKey((Map) call.arguments);
......@@ -51,6 +53,8 @@ public class AMapFlutterLocationPlugin implements FlutterPlugin, MethodCallHandl
case "destroy":
destroy((Map) call.arguments);
break;
case "calculateDistance":
calculateDistance((Map) call.arguments,result);
default:
result.notImplemented();
break;
......@@ -91,6 +95,18 @@ public class AMapFlutterLocationPlugin implements FlutterPlugin, MethodCallHandl
}
}
/**
* 计算两点之间的距离
*/
private void calculateDistance(Map argsMap,Result result) {
double lat1 = (double) argsMap.get("lat1");
double lng1 = (double) argsMap.get("lng1");
double lat2 = (double) argsMap.get("lat2");
double lng2 = (double) argsMap.get("lng2");
double distance = AMapLocationClientImpl.calculateDistance(lat1, lng1, lat2, lng2);
result.success(distance);
}
/**
* 销毁
*
......@@ -127,7 +143,8 @@ public class AMapFlutterLocationPlugin implements FlutterPlugin, MethodCallHandl
boolean hasContains = (boolean) privacyShowMap.get("hasContains");
boolean hasShow = (boolean) privacyShowMap.get("hasShow");
try {
Method showMethod = locationClazz.getMethod("updatePrivacyShow", Context.class, boolean.class, boolean.class);;
Method showMethod = locationClazz.getMethod("updatePrivacyShow", Context.class, boolean.class, boolean.class);
;
showMethod.invoke(null, mContext, hasContains, hasShow);
} catch (Throwable e) {
// e.printStackTrace();
......
......@@ -7,6 +7,8 @@ import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.CoordinateConverter;
import com.amap.api.location.DPoint;
import java.util.Map;
......@@ -58,6 +60,18 @@ public class AMapLocationClientImpl implements AMapLocationListener {
}
public static float calculateDistance(double lat1, double lon1, double lat2, double lon2) {
DPoint dPoint1 = new DPoint();
dPoint1.setLatitude(lat1);
dPoint1.setLongitude(lon1);
DPoint dPoint2 = new DPoint();
dPoint2.setLatitude(lat2);
dPoint2.setLongitude(lon2);
return CoordinateConverter.calculateLineDistance(dPoint1, dPoint2);
}
/**
* 停止定位
*/
......@@ -70,11 +84,12 @@ public class AMapLocationClientImpl implements AMapLocationListener {
}
public void destroy() {
if(null != locationClient) {
if (null != locationClient) {
locationClient.onDestroy();
locationClient = null;
}
}
/**
* 定位回调
*
......
......@@ -54,6 +54,11 @@ class AMapFlutterLocation {
_methodChannel.invokeMethod('stopLocation', {'pluginKey': _pluginKey});
return;
}
///计算2点之间的距离
Future<double> calculateDistance(double lat1, double lon1, double lat2, double lon2) async {
double result = await _methodChannel.invokeMethod('calculateDistance', {'lat1': lat1, 'lon1': lon1, 'lat2': lat2, 'lon2': lon2});
return result;
}
///设置Android和iOS的apikey,建议在weigdet初始化时设置<br>
///apiKey的申请请参考高德开放平台官网<br>
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论