demo的使用
如果想要在自己的项目中整合进高德,第一步就是要看看人家写的demo,所以首先
下载高德的demo
下载完以后包含4个文件夹,其中2DMap包含了我们平时用到的很多功能
我们运行在手机上的时候,使用某些功能会提示
这是SHA1值的问题,我们可以进行如下操作来暂时使用高德的demo
如何正常使用高德demo
1、获取 SHA1 值
进入cmd
cd .android
keytool -list -v -keystore debug.keystore
提示输入密钥库密码,编译器提供的debug keystore默认密码是 android
此时可在控制台显示的信息中获取 SHA1 值
2、登录高德开放平台
高德开放平台
创建应用,并添加新key
3、复制应用的key
填写到AndroidManifest.xml中的如下位置
这样高德的demo就可以正常使用了
在自己 app 整合高德,仿钉钉打卡
1、按照上面的步骤在高德开放平台添加 release 的 签名文件获取新的 key
2、要实现仿钉钉打卡,需要利用高德的定位 SDK 的地理围栏功能,参见 示例中心:移动端地理围栏,根据 开发指南:Android Studio 配置工程 引入定位依赖
implementation 'com.amap.api:3dmap:latest.integration'
implementation 'com.amap.api:location:5.2.0'
3、build.gradle 确认使用的 key
为了调试,可以把 debug 和 release 用一个签名文件
signingConfigs {
debug {
storeFile file('../app/key.jks')
storePassword '123456'
keyAlias "key0"
keyPassword '123456'
}
release {
storeFile file('../app/key.jks')
storePassword '123456'
keyAlias "key0"
keyPassword '123456'
}
}
4、确认 build.gradle 中的 applicationId 是否是刚才申请 key 时填的一样
5、AndroidManifest 中配置 高德 key,并添加权限
//地图包、搜索包需要的基础权限
<!--允许程序打开网络套接字-->
<uses-permission android:name="android.permission.INTERNET" />
<!--允许程序设置内置sd卡的写权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--允许程序获取网络状态-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!--允许程序访问WiFi网络信息-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!--允许程序读写手机状态和身份-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!--允许程序访问CellID或WiFi热点来获取粗略的位置-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!--如果设置了target >= 28 如果需要启动后台定位则必须声明这个权限-->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<!--如果您的应用需要后台定位权限,且有可能运行在Android Q设备上,并且设置了target>28,必须增加这个权限声明-->
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
<application
android:name=".MyApplication"
......>
<!-- 设置key -->
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="设置刚才申请的key"/>
<!-- 定位需要的服务 适配Android Q需要加上android:foregroundServiceType="location"-->
<service
android:name="com.amap.api.location.APSService"
android:foregroundServiceType="location" />
</application>
6、因为我们要用到 地理围栏,所以可以先看下官方的demo,在下载的实例中找到 Amap_Android_API_Location_Demo 并运行
7、把 Demo 项目中的页面 plugin_geofence_map.xml 拿到自己项目中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.amap.api.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.amap.api.maps.MapView>
<ScrollView
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#D999"
android:layout_alignParentBottom="true">
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
</RelativeLayout>
</LinearLayout>
代码直接把 GeoFence_Round_Activity 拿过来就行了
其中 CheckPermissionsActivity 是检查以下权限
protected String[] needPermissions = {
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.READ_PHONE_STATE
};
/**
* 圆形地理围栏
*
* @author hongming.wang
* @since 3.2.0
*/
public class GeoFence_Round_Activity extends CheckPermissionsActivity
implements
OnClickListener,
GeoFenceListener,
OnMapClickListener,
LocationSource,
AMapLocationListener,
OnCheckedChangeListener {
private View lyOption;
private TextView tvGuide;
private TextView tvResult;
private EditText etCustomId;
private EditText etRadius;
private CheckBox cbAlertIn;
private CheckBox cbAlertOut;
private CheckBox cbAldertStated;
private Button btAddFence;
private Button btOption;
/**
* 用于显示当前的位置
* <p>
* 示例中是为了显示当前的位置,在实际使用中,单独的地理围栏可以不使用定位接口
* </p>
*/
private AMapLocationClient mlocationClient;
private OnLocationChangedListener mListener;
private AMapLocationClientOption mLocationOption;
private MapView mMapView;
private AMap mAMap;
// 中心点坐标
private LatLng centerLatLng = null;
// 中心点marker
private Marker centerMarker;
private BitmapDescriptor ICON_YELLOW = BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW);
private BitmapDescriptor ICON_RED = BitmapDescriptorFactory<