Android使用高德地图API

1.引入高德地图

在工程级build.gradle文件中配置repositories,添加maven或jcenter仓库地址

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
        maven { url 'https://www.jitpack.io' }
    }
}

在项目级build.gradle文件中引入高德依赖

android {
    defaultConfig {
        ndk{
            //设置支持的SO库架构(开发者可以根据需要,选择一个或多个平台的so)
            abiFilters "armeabi", "armeabi-v7a", "arm64-v8a", "x86","arm64-v8a","x86_64"
        }
    }
}

dependencies {
    implementation 'com.amap.api:3dmap:8.1.0' //3D地图
    implementation 'com.amap.api:search:7.7.0' //搜索功能
    implementation 'com.amap.api:location:5.2.0' //定位功能
}

2.添加高德Key、配置权限

项目的 AndroidManifest.xml 文件中,配置代码:

<application
         android:icon="@drawable/icon"
         android:label="@string/app_name" >
         <meta-data
            android:name="com.amap.api.v2.apikey"
            android:value="Key" /> 
         
        <service android:name="com.amap.api.location.APSService" />
            ……
application>

地图权限:


<uses-permission android:name="android.permission.INTERNET" />  


<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 


<uses-permission android:name="android.permission.READ_PHONE_STATE" />    


<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    


<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 


<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> 


<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 


<uses-permission android:name="android.permission.WRITE_SETTINGS" />  


<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

搜索权限:


<uses-permission android:name="android.permission.INTERNET" />  


<uses-permission android:name="android.permission.READ_PHONE_STATE" />    


<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    


<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 


<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 


<uses-permission android:name="android.permission.WRITE_SETTINGS" />  


<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

3.隐私合规接口说明

自定义MyApplication继承Application类,在初始化地图之前完成进行合规检查:

class MyApplication : Application() {

    companion object {
        @SuppressLint("StaticFieldLeak")
        lateinit var context: Context
    }

    override fun onCreate() {
    	// 地图
        MapsInitializer.updatePrivacyShow(this,true,true)
        MapsInitializer.updatePrivacyAgree(this,true)
        // 搜索
        ServiceSettings.updatePrivacyShow(this,true,true)
        ServiceSettings.updatePrivacyAgree(this,true)
        super.onCreate()
        context = applicationContext

    }
}

你可能感兴趣的:(android,gradle,kotlin)