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

增加计算2点距离

上级 284d2f06
...@@ -16,202 +16,219 @@ import io.flutter.plugin.common.MethodChannel; ...@@ -16,202 +16,219 @@ import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler; import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result; import io.flutter.plugin.common.MethodChannel.Result;
/** 高德地图定位sdkFlutterPlugin */ /**
* 高德地图定位sdkFlutterPlugin
*/
public class AMapFlutterLocationPlugin implements FlutterPlugin, MethodCallHandler, public class AMapFlutterLocationPlugin implements FlutterPlugin, MethodCallHandler,
EventChannel.StreamHandler { EventChannel.StreamHandler {
private static final String CHANNEL_METHOD_LOCATION = "amap_flutter_location"; private static final String CHANNEL_METHOD_LOCATION = "amap_flutter_location";
private static final String CHANNEL_STREAM_LOCATION = "amap_flutter_location_stream"; private static final String CHANNEL_STREAM_LOCATION = "amap_flutter_location_stream";
private Context mContext = null; private Context mContext = null;
public static EventChannel.EventSink mEventSink = null; public static EventChannel.EventSink mEventSink = null;
private Map<String, AMapLocationClientImpl> locationClientMap = new ConcurrentHashMap<String, AMapLocationClientImpl>(8); private Map<String, AMapLocationClientImpl> locationClientMap = new ConcurrentHashMap<String, AMapLocationClientImpl>(8);
@Override @Override
public void onMethodCall(MethodCall call, Result result) { public void onMethodCall(MethodCall call, Result result) {
String callMethod = call.method; String callMethod = call.method;
switch (call.method) { switch (call.method) {
case "updatePrivacyStatement": case "updatePrivacyStatement":
updatePrivacyStatement((Map)call.arguments); updatePrivacyStatement((Map) call.arguments);
break; break;
case "setApiKey": case "setApiKey":
setApiKey((Map) call.arguments); setApiKey((Map) call.arguments);
break; break;
case "setLocationOption": case "setLocationOption":
setLocationOption((Map) call.arguments); setLocationOption((Map) call.arguments);
break; break;
case "startLocation": case "startLocation":
startLocation((Map) call.arguments); startLocation((Map) call.arguments);
break; break;
case "stopLocation": case "stopLocation":
stopLocation((Map) call.arguments); stopLocation((Map) call.arguments);
break; break;
case "destroy": case "destroy":
destroy((Map) call.arguments); destroy((Map) call.arguments);
break; break;
default: case "calculateDistance":
result.notImplemented(); calculateDistance((Map) call.arguments,result);
break; default:
result.notImplemented();
break;
}
} }
}
@Override @Override
public void onListen(Object o, EventChannel.EventSink eventSink) { public void onListen(Object o, EventChannel.EventSink eventSink) {
mEventSink = eventSink; mEventSink = eventSink;
} }
@Override @Override
public void onCancel(Object o) { public void onCancel(Object o) {
for (Map.Entry<String, AMapLocationClientImpl> entry : locationClientMap.entrySet()) { for (Map.Entry<String, AMapLocationClientImpl> entry : locationClientMap.entrySet()) {
entry.getValue().stopLocation(); entry.getValue().stopLocation();
}
} }
}
/**
/** * 开始定位
* 开始定位 */
*/ private void startLocation(Map argsMap) {
private void startLocation(Map argsMap) { AMapLocationClientImpl locationClientImp = getLocationClientImp(argsMap);
AMapLocationClientImpl locationClientImp = getLocationClientImp(argsMap); if (null != locationClientImp) {
if (null != locationClientImp) { locationClientImp.startLocation();
locationClientImp.startLocation(); }
} }
}
/** /**
* 停止定位 * 停止定位
*/ */
private void stopLocation(Map argsMap) { private void stopLocation(Map argsMap) {
AMapLocationClientImpl locationClientImp = getLocationClientImp(argsMap); AMapLocationClientImpl locationClientImp = getLocationClientImp(argsMap);
if (null != locationClientImp) { if (null != locationClientImp) {
locationClientImp.stopLocation(); locationClientImp.stopLocation();
} }
}
/**
* 销毁
*
* @param argsMap
*/
private void destroy(Map argsMap) {
AMapLocationClientImpl locationClientImp = getLocationClientImp(argsMap);
if (null != locationClientImp) {
locationClientImp.destroy();
locationClientMap.remove(getPluginKeyFromArgs(argsMap));
} }
}
/**
/** * 计算两点之间的距离
* 设置apikey */
* private void calculateDistance(Map argsMap,Result result) {
* @param apiKeyMap double lat1 = (double) argsMap.get("lat1");
*/ double lng1 = (double) argsMap.get("lng1");
private void setApiKey(Map apiKeyMap) { double lat2 = (double) argsMap.get("lat2");
if (null != apiKeyMap) { double lng2 = (double) argsMap.get("lng2");
if (apiKeyMap.containsKey("android") double distance = AMapLocationClientImpl.calculateDistance(lat1, lng1, lat2, lng2);
&& !TextUtils.isEmpty((String) apiKeyMap.get("android"))) { result.success(distance);
AMapLocationClient.setApiKey((String) apiKeyMap.get("android"));
}
} }
}
private void updatePrivacyStatement(Map privacyShowMap) { /**
if (null != privacyShowMap) { * 销毁
Class<AMapLocationClient> locationClazz = AMapLocationClient.class; *
* @param argsMap
*/
private void destroy(Map argsMap) {
AMapLocationClientImpl locationClientImp = getLocationClientImp(argsMap);
if (null != locationClientImp) {
locationClientImp.destroy();
locationClientMap.remove(getPluginKeyFromArgs(argsMap));
}
}
if (privacyShowMap.containsKey("hasContains") && privacyShowMap.containsKey("hasShow")) { /**
boolean hasContains = (boolean) privacyShowMap.get("hasContains"); * 设置apikey
boolean hasShow = (boolean) privacyShowMap.get("hasShow"); *
try { * @param apiKeyMap
Method showMethod = locationClazz.getMethod("updatePrivacyShow", Context.class, boolean.class, boolean.class);; */
showMethod.invoke(null, mContext, hasContains, hasShow); private void setApiKey(Map apiKeyMap) {
} catch (Throwable e) { if (null != apiKeyMap) {
// e.printStackTrace(); if (apiKeyMap.containsKey("android")
&& !TextUtils.isEmpty((String) apiKeyMap.get("android"))) {
AMapLocationClient.setApiKey((String) apiKeyMap.get("android"));
}
} }
} }
if (privacyShowMap.containsKey("hasAgree")) { private void updatePrivacyStatement(Map privacyShowMap) {
boolean hasAgree = (boolean) privacyShowMap.get("hasAgree"); if (null != privacyShowMap) {
try { Class<AMapLocationClient> locationClazz = AMapLocationClient.class;
Method agreeMethod = locationClazz.getMethod("updatePrivacyAgree", Context.class, boolean.class);
agreeMethod.invoke(null, mContext, hasAgree); if (privacyShowMap.containsKey("hasContains") && privacyShowMap.containsKey("hasShow")) {
} catch (Throwable e) { boolean hasContains = (boolean) privacyShowMap.get("hasContains");
boolean hasShow = (boolean) privacyShowMap.get("hasShow");
try {
Method showMethod = locationClazz.getMethod("updatePrivacyShow", Context.class, boolean.class, boolean.class);
;
showMethod.invoke(null, mContext, hasContains, hasShow);
} catch (Throwable e) {
// e.printStackTrace();
}
}
if (privacyShowMap.containsKey("hasAgree")) {
boolean hasAgree = (boolean) privacyShowMap.get("hasAgree");
try {
Method agreeMethod = locationClazz.getMethod("updatePrivacyAgree", Context.class, boolean.class);
agreeMethod.invoke(null, mContext, hasAgree);
} catch (Throwable e) {
// e.printStackTrace(); // e.printStackTrace();
}
}
} }
}
}
}
/**
* 设置定位参数
*
* @param argsMap
*/
private void setLocationOption(Map argsMap) {
AMapLocationClientImpl locationClientImp = getLocationClientImp(argsMap);
if (null != locationClientImp) {
locationClientImp.setLocationOption(argsMap);
} }
}
/**
* 设置定位参数
*
* @param argsMap
*/
private void setLocationOption(Map argsMap) {
AMapLocationClientImpl locationClientImp = getLocationClientImp(argsMap);
if (null != locationClientImp) {
locationClientImp.setLocationOption(argsMap);
}
}
@Override
public void onAttachedToEngine(FlutterPluginBinding binding) {
if (null == mContext) {
mContext = binding.getApplicationContext();
/** @Override
* 方法调用通道 public void onAttachedToEngine(FlutterPluginBinding binding) {
*/ if (null == mContext) {
final MethodChannel channel = new MethodChannel(binding.getBinaryMessenger(), CHANNEL_METHOD_LOCATION); mContext = binding.getApplicationContext();
channel.setMethodCallHandler(this);
/** /**
* 回调监听通道 * 方法调用通道
*/ */
final EventChannel eventChannel = new EventChannel(binding.getBinaryMessenger(), CHANNEL_STREAM_LOCATION); final MethodChannel channel = new MethodChannel(binding.getBinaryMessenger(), CHANNEL_METHOD_LOCATION);
eventChannel.setStreamHandler(this); channel.setMethodCallHandler(this);
}
} /**
* 回调监听通道
*/
final EventChannel eventChannel = new EventChannel(binding.getBinaryMessenger(), CHANNEL_STREAM_LOCATION);
eventChannel.setStreamHandler(this);
}
@Override
public void onDetachedFromEngine(FlutterPluginBinding binding) {
for (Map.Entry<String, AMapLocationClientImpl> entry : locationClientMap.entrySet()) {
entry.getValue().destroy();
} }
}
private AMapLocationClientImpl getLocationClientImp(Map argsMap) { @Override
if (null == locationClientMap) { public void onDetachedFromEngine(FlutterPluginBinding binding) {
locationClientMap = new ConcurrentHashMap<String, AMapLocationClientImpl>(8); for (Map.Entry<String, AMapLocationClientImpl> entry : locationClientMap.entrySet()) {
entry.getValue().destroy();
}
} }
String pluginKey = getPluginKeyFromArgs(argsMap); private AMapLocationClientImpl getLocationClientImp(Map argsMap) {
if (TextUtils.isEmpty(pluginKey)) { if (null == locationClientMap) {
return null; locationClientMap = new ConcurrentHashMap<String, AMapLocationClientImpl>(8);
} }
String pluginKey = getPluginKeyFromArgs(argsMap);
if (TextUtils.isEmpty(pluginKey)) {
return null;
}
if (!locationClientMap.containsKey(pluginKey)) { if (!locationClientMap.containsKey(pluginKey)) {
AMapLocationClientImpl locationClientImp = new AMapLocationClientImpl(mContext, pluginKey, mEventSink); AMapLocationClientImpl locationClientImp = new AMapLocationClientImpl(mContext, pluginKey, mEventSink);
locationClientMap.put(pluginKey, locationClientImp); locationClientMap.put(pluginKey, locationClientImp);
}
return locationClientMap.get(pluginKey);
} }
return locationClientMap.get(pluginKey);
} private String getPluginKeyFromArgs(Map argsMap) {
String pluginKey = null;
private String getPluginKeyFromArgs(Map argsMap) { try {
String pluginKey = null; if (null != argsMap) {
try { pluginKey = (String) argsMap.get("pluginKey");
if (null != argsMap) { }
pluginKey = (String) argsMap.get("pluginKey"); } catch (Throwable e) {
} e.printStackTrace();
} catch (Throwable e) { }
e.printStackTrace(); return pluginKey;
} }
return pluginKey;
}
} }
...@@ -7,6 +7,8 @@ import com.amap.api.location.AMapLocation; ...@@ -7,6 +7,8 @@ import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient; import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption; import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener; import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.CoordinateConverter;
import com.amap.api.location.DPoint;
import java.util.Map; import java.util.Map;
...@@ -58,6 +60,18 @@ public class AMapLocationClientImpl implements AMapLocationListener { ...@@ -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 { ...@@ -70,11 +84,12 @@ public class AMapLocationClientImpl implements AMapLocationListener {
} }
public void destroy() { public void destroy() {
if(null != locationClient) { if (null != locationClient) {
locationClient.onDestroy(); locationClient.onDestroy();
locationClient = null; locationClient = null;
} }
} }
/** /**
* 定位回调 * 定位回调
* *
......
...@@ -54,6 +54,11 @@ class AMapFlutterLocation { ...@@ -54,6 +54,11 @@ class AMapFlutterLocation {
_methodChannel.invokeMethod('stopLocation', {'pluginKey': _pluginKey}); _methodChannel.invokeMethod('stopLocation', {'pluginKey': _pluginKey});
return; 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> ///设置Android和iOS的apikey,建议在weigdet初始化时设置<br>
///apiKey的申请请参考高德开放平台官网<br> ///apiKey的申请请参考高德开放平台官网<br>
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论