Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
A
amap_flutter_location
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
openSourceLibrary
amap_flutter_location
Commits
4ef29091
提交
4ef29091
authored
1月 07, 2023
作者:
张国庆
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
增加计算2点距离
上级
284d2f06
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
204 行增加
和
167 行删除
+204
-167
AMapFlutterLocationPlugin.java
.../com/amap/flutter/location/AMapFlutterLocationPlugin.java
+183
-166
AMapLocationClientImpl.java
...ava/com/amap/flutter/location/AMapLocationClientImpl.java
+16
-1
amap_flutter_location.dart
lib/amap_flutter_location.dart
+5
-0
没有找到文件。
android/src/main/java/com/amap/flutter/location/AMapFlutterLocationPlugin.java
浏览文件 @
4ef29091
...
...
@@ -16,202 +16,219 @@ 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"
;
private
static
final
String
CHANNEL_STREAM_LOCATION
=
"amap_flutter_location_stream"
;
private
Context
mContext
=
null
;
public
static
EventChannel
.
EventSink
mEventSink
=
null
;
private
Map
<
String
,
AMapLocationClientImpl
>
locationClientMap
=
new
ConcurrentHashMap
<
String
,
AMapLocationClientImpl
>(
8
);
@Override
public
void
onMethodCall
(
MethodCall
call
,
Result
result
)
{
String
callMethod
=
call
.
method
;
switch
(
call
.
method
)
{
case
"updatePrivacyStatement"
:
updatePrivacyStatement
((
Map
)
call
.
arguments
);
break
;
case
"setApiKey"
:
setApiKey
((
Map
)
call
.
arguments
);
break
;
case
"setLocationOption"
:
setLocationOption
((
Map
)
call
.
arguments
);
break
;
case
"startLocation"
:
startLocation
((
Map
)
call
.
arguments
);
break
;
case
"stopLocation"
:
stopLocation
((
Map
)
call
.
arguments
);
break
;
case
"destroy"
:
destroy
((
Map
)
call
.
arguments
);
break
;
default
:
result
.
notImplemented
();
break
;
private
static
final
String
CHANNEL_METHOD_LOCATION
=
"amap_flutter_location"
;
private
static
final
String
CHANNEL_STREAM_LOCATION
=
"amap_flutter_location_stream"
;
private
Context
mContext
=
null
;
public
static
EventChannel
.
EventSink
mEventSink
=
null
;
private
Map
<
String
,
AMapLocationClientImpl
>
locationClientMap
=
new
ConcurrentHashMap
<
String
,
AMapLocationClientImpl
>(
8
);
@Override
public
void
onMethodCall
(
MethodCall
call
,
Result
result
)
{
String
callMethod
=
call
.
method
;
switch
(
call
.
method
)
{
case
"updatePrivacyStatement"
:
updatePrivacyStatement
((
Map
)
call
.
arguments
);
break
;
case
"setApiKey"
:
setApiKey
((
Map
)
call
.
arguments
);
break
;
case
"setLocationOption"
:
setLocationOption
((
Map
)
call
.
arguments
);
break
;
case
"startLocation"
:
startLocation
((
Map
)
call
.
arguments
);
break
;
case
"stopLocation"
:
stopLocation
((
Map
)
call
.
arguments
);
break
;
case
"destroy"
:
destroy
((
Map
)
call
.
arguments
);
break
;
case
"calculateDistance"
:
calculateDistance
((
Map
)
call
.
arguments
,
result
);
default
:
result
.
notImplemented
();
break
;
}
}
}
@Override
public
void
onListen
(
Object
o
,
EventChannel
.
EventSink
eventSink
)
{
mEventSink
=
eventSink
;
}
@Override
public
void
onListen
(
Object
o
,
EventChannel
.
EventSink
eventSink
)
{
mEventSink
=
eventSink
;
}
@Override
public
void
onCancel
(
Object
o
)
{
for
(
Map
.
Entry
<
String
,
AMapLocationClientImpl
>
entry
:
locationClientMap
.
entrySet
())
{
entry
.
getValue
().
stopLocation
();
@Override
public
void
onCancel
(
Object
o
)
{
for
(
Map
.
Entry
<
String
,
AMapLocationClientImpl
>
entry
:
locationClientMap
.
entrySet
())
{
entry
.
getValue
().
stopLocation
();
}
}
}
/**
* 开始定位
*/
private
void
startLocation
(
Map
argsMap
)
{
AMapLocationClientImpl
locationClientImp
=
getLocationClientImp
(
argsMap
);
if
(
null
!=
locationClientImp
)
{
locationClientImp
.
startLocation
();
/**
* 开始定位
*/
private
void
startLocation
(
Map
argsMap
)
{
AMapLocationClientImpl
locationClientImp
=
getLocationClientImp
(
argsMap
);
if
(
null
!=
locationClientImp
)
{
locationClientImp
.
startLocation
();
}
}
}
/**
* 停止定位
*/
private
void
stopLocation
(
Map
argsMap
)
{
AMapLocationClientImpl
locationClientImp
=
getLocationClientImp
(
argsMap
);
if
(
null
!=
locationClientImp
)
{
locationClientImp
.
stopLocation
();
}
}
/**
* 销毁
*
* @param argsMap
*/
private
void
destroy
(
Map
argsMap
)
{
AMapLocationClientImpl
locationClientImp
=
getLocationClientImp
(
argsMap
);
if
(
null
!=
locationClientImp
)
{
locationClientImp
.
destroy
();
locationClientMap
.
remove
(
getPluginKeyFromArgs
(
argsMap
));
/**
* 停止定位
*/
private
void
stopLocation
(
Map
argsMap
)
{
AMapLocationClientImpl
locationClientImp
=
getLocationClientImp
(
argsMap
);
if
(
null
!=
locationClientImp
)
{
locationClientImp
.
stopLocation
();
}
}
}
/**
* 设置apikey
*
* @param apiKeyMap
*/
private
void
setApiKey
(
Map
apiKeyMap
)
{
if
(
null
!=
apiKeyMap
)
{
if
(
apiKeyMap
.
containsKey
(
"android"
)
&&
!
TextUtils
.
isEmpty
((
String
)
apiKeyMap
.
get
(
"android"
)))
{
AMapLocationClient
.
setApiKey
((
String
)
apiKeyMap
.
get
(
"android"
));
}
/**
* 计算两点之间的距离
*/
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
);
}
}
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"
);
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();
/**
* 设置apikey
*
* @param apiKeyMap
*/
private
void
setApiKey
(
Map
apiKeyMap
)
{
if
(
null
!=
apiKeyMap
)
{
if
(
apiKeyMap
.
containsKey
(
"android"
)
&&
!
TextUtils
.
isEmpty
((
String
)
apiKeyMap
.
get
(
"android"
)))
{
AMapLocationClient
.
setApiKey
((
String
)
apiKeyMap
.
get
(
"android"
));
}
}
}
}
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
)
{
private
void
updatePrivacyStatement
(
Map
privacyShowMap
)
{
if
(
null
!=
privacyShowMap
)
{
Class
<
AMapLocationClient
>
locationClazz
=
AMapLocationClient
.
class
;
if
(
privacyShowMap
.
containsKey
(
"hasContains"
)
&&
privacyShowMap
.
containsKey
(
"hasShow"
))
{
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();
}
}
}
}
}
}
/**
* 设置定位参数
*
* @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
();
/**
* 方法调用通道
*/
final
MethodChannel
channel
=
new
MethodChannel
(
binding
.
getBinaryMessenger
(),
CHANNEL_METHOD_LOCATION
);
channel
.
setMethodCallHandler
(
this
);
@Override
public
void
onAttachedToEngine
(
FlutterPluginBinding
binding
)
{
if
(
null
==
mContext
)
{
mContext
=
binding
.
getApplicationContext
();
/**
* 回调监听通道
*/
final
EventChannel
eventChannel
=
new
EventChannel
(
binding
.
getBinaryMessenger
(),
CHANNEL_STREAM_LOCATION
);
eventChannel
.
setStreamHandler
(
this
);
}
/**
* 方法调用通道
*/
final
MethodChannel
channel
=
new
MethodChannel
(
binding
.
getBinaryMessenger
(),
CHANNEL_METHOD_LOCATION
);
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
)
{
if
(
null
==
locationClientMap
)
{
locationClientMap
=
new
ConcurrentHashMap
<
String
,
AMapLocationClientImpl
>(
8
);
@Override
public
void
onDetachedFromEngine
(
FlutterPluginBinding
binding
)
{
for
(
Map
.
Entry
<
String
,
AMapLocationClientImpl
>
entry
:
locationClientMap
.
entrySet
())
{
entry
.
getValue
().
destroy
();
}
}
String
pluginKey
=
getPluginKeyFromArgs
(
argsMap
);
if
(
TextUtils
.
isEmpty
(
pluginKey
))
{
return
null
;
}
private
AMapLocationClientImpl
getLocationClientImp
(
Map
argsMap
)
{
if
(
null
==
locationClientMap
)
{
locationClientMap
=
new
ConcurrentHashMap
<
String
,
AMapLocationClientImpl
>(
8
);
}
String
pluginKey
=
getPluginKeyFromArgs
(
argsMap
);
if
(
TextUtils
.
isEmpty
(
pluginKey
))
{
return
null
;
}
if
(!
locationClientMap
.
containsKey
(
pluginKey
))
{
AMapLocationClientImpl
locationClientImp
=
new
AMapLocationClientImpl
(
mContext
,
pluginKey
,
mEventSink
);
locationClientMap
.
put
(
pluginKey
,
locationClientImp
);
if
(!
locationClientMap
.
containsKey
(
pluginKey
))
{
AMapLocationClientImpl
locationClientImp
=
new
AMapLocationClientImpl
(
mContext
,
pluginKey
,
mEventSink
);
locationClientMap
.
put
(
pluginKey
,
locationClientImp
);
}
return
locationClientMap
.
get
(
pluginKey
);
}
return
locationClientMap
.
get
(
pluginKey
);
}
private
String
getPluginKeyFromArgs
(
Map
argsMap
)
{
String
pluginKey
=
null
;
try
{
if
(
null
!=
argsMap
)
{
pluginKey
=
(
String
)
argsMap
.
get
(
"pluginKey"
);
}
}
catch
(
Throwable
e
)
{
e
.
printStackTrace
()
;
private
String
getPluginKeyFromArgs
(
Map
argsMap
)
{
String
pluginKey
=
null
;
try
{
if
(
null
!=
argsMap
)
{
pluginKey
=
(
String
)
argsMap
.
get
(
"pluginKey"
);
}
}
catch
(
Throwable
e
)
{
e
.
printStackTrace
();
}
return
pluginKey
;
}
return
pluginKey
;
}
}
android/src/main/java/com/amap/flutter/location/AMapLocationClientImpl.java
浏览文件 @
4ef29091
...
...
@@ -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
;
}
}
/**
* 定位回调
*
...
...
lib/amap_flutter_location.dart
浏览文件 @
4ef29091
...
...
@@ -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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论