当前位置:   article > 正文

Android 使用高德地图

Android 使用高德地图

一、获取高德平台key

【1】基于application包名&sha1值在高德控制台获取key值,详情参考:  获取Key-创建工程-开发指南-Android 地图SDK | 高德地图API

【2】在manifest中声明权限

【3】将拿到的key值在manifest中进行声明

  1. <!--允许程序打开网络套接字-->
  2. <uses-permission android:name="android.permission.INTERNET" />
  3. <!--允许程序设置内置sd卡的写权限-->
  4. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  5. <!--允许程序获取网络状态-->
  6. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  7. <!--允许程序访问WiFi网络信息-->
  8. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  9. <!--允许程序访问CellID或WiFi热点来获取粗略的位置-->
  10. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  1. <meta-data
  2. android:name="com.amap.api.v2.apikey"
  3. android:value="xxxxxxxxxxxx" />

二、集成依赖

在app build.gradle文件中增加高德地图依赖

implementation("com.amap.api:3dmap:9.8.2")

三、显示地图

概述-Android 地图SDK | 高德地图API

在xml文件中声明MapView控件,并在class中重写 onCreate 方法(此方法必须重写),注意生命周期的管理

  1. <com.amap.api.maps.MapView
  2. android:id="@+id/map"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent" />
  1. private var mapView: MapView? = null
  2. override fun onCreate(savedInstanceState: Bundle?) {
  3. super.onCreate(savedInstanceState)
  4. setContentView(R.layout.test_activity)
  5. mapView = findViewById(R.id.map)
  6. mapView?.onCreate(savedInstanceState)
  7. }
  8. override fun onResume() {
  9. super.onResume()
  10. mapView?.onResume()
  11. }
  12. override fun onPause() {
  13. super.onPause()
  14. mapView?.onPause()
  15. }
  16. override fun onDestroy() {
  17. super.onDestroy()
  18. mapView?.onDestroy()
  19. }
  20. override fun onSaveInstanceState(outState: Bundle) {
  21. super.onSaveInstanceState(outState)
  22. mapView?.onSaveInstanceState(outState)
  23. }

地图默认中心点为北京天安门

四、高德地图具体使用

以下对于地图的控制使用之前需要拿到AMap对象

map = mapView?.map

1、切换城市中心点

  1. private fun moveCenterTo(latLng: LatLng) {
  2. val cameraUpdate = CameraUpdateFactory.newLatLng(latLng)
  3. map?.moveCamera(cameraUpdate)
  4. }

Android高德地图切换城市中心点展示

2、设置缩放级别

moveCamera(CameraUpdateFactory.zoomTo(14.0f))

3、绘制marker

  1. companion object {
  2. private val MARKER1 = LatLng(40.02855349893361, 116.3052948784071)
  3. private val MARKER2 = LatLng(41.093445392798934, 116.11030767409169)
  4. }
  5. val marker1: MarkerOptions = MarkerOptions().apply {
  6. position(MARKER1)
  7. }
  8. val marker2: MarkerOptions = MarkerOptions().apply {
  9. position(MARKER2)
  10. }
  11. val list = ArrayList<MarkerOptions>()
  12. list.add(marker1)
  13. list.add(marker2)
  14. map?.addMarkers(list, false)

如下图所示,不设置icon默认展示蓝色定位图标。

11

针对marker可以设置的属性:

position在地图上标记位置的经纬度值,必填参数
title点标记的标题
snippet点标记的内容
draggable点标记是否可拖拽
visible点标记是否可见
anchor点标记的锚点
alpha点的透明度

public final java.util.ArrayList<Marker> addMarkers(java.util.ArrayList<MarkerOptions> options, boolean moveToCenter)

在地图上添一组图片标记(marker)对象,并设置是否改变地图状态以至于所有的marker对象都在当前地图可视区域范围内显示。

参数:

options - 多个markerOptions对象,它们分别定义了对应marker的属性信息。

moveToCenter - 是否改变地图状态,默认为false。

返回:

返回一组被添加的marker对象。

4、绘制折线

  1. val latLngList = ArrayList<LatLng>()
  2. latLngList.add(MARKER1)
  3. latLngList.add(MARKER2)
  4. map?.addPolyline(
  5. PolylineOptions().addAll(latLngList).width(3f).color(Color.RED)
  6. )

5、轨迹

  1. SmoothMoveMarker(map).apply {
  2. setDescriptor(BitmapDescriptorFactory.fromResource(R.drawable.smooth))
  3. setPoints(latLngList)
  4. setTotalDuration(5)
  5. startSmoothMove()
  6. }

6、两点之间距离计算

AMapUtils.calculateLineDistance(latLng1,latLng2)

7、切换地图图层

【1】预设模式

mapType = AMap.MAP_TYPE_NIGHT

【2】在线自定义模式

  1. val options = CustomMapStyleOptions().apply {
  2. isEnable = true
  3. styleId = ""
  4. }
  5. map?.setCustomMapStyle(options)

8、手势交互

  1. map?.uiSettings.let {
  2. it.isRotateGesturesEnabled = false
  3. it.isZoomControlsEnabled = false
  4. it.isTiltGesturesEnabled = false
  5. }

更多设置可参考:https://a.amap.com/lbs/static/unzip/Android_Map_Doc/3D/index.html?overview-summary.html

9、地图状态监听

  1. setOnMapLoadedListener(object : AMap.OnMapLoadedListener {
  2. override fun onMapLoaded() {
  3. Log.e(TAG, "onMapLoaded...")
  4. }
  5. })
  6. //自带放大缩小接口
  7. setOnCameraChangeListener(object : AMap.OnCameraChangeListener {
  8. override fun onCameraChange(p0: CameraPosition?) {
  9. //Log.e(TAG, "onCameraChange + ${p0?.toString()}")
  10. }
  11. override fun onCameraChangeFinish(p0: CameraPosition?) {
  12. Log.e(TAG, "onCameraChangeFinish + ${p0?.toString()}")
  13. }
  14. })
  15. //map point点击事件
  16. addOnMapClickListener(object : AMap.OnMapClickListener {
  17. override fun onMapClick(p0: LatLng?) {
  18. Log.e(TAG, "onMapClick + ${p0?.toString()}")
  19. }
  20. })
  21. //地图自带poi点击事件
  22. addOnPOIClickListener(object : AMap.OnPOIClickListener {
  23. override fun onPOIClick(p0: Poi?) {
  24. Log.e(TAG, "onPOIClick + ${p0?.toString()}")
  25. }
  26. })
  27. addOnMarkerClickListener(object : AMap.OnMarkerClickListener {
  28. override fun onMarkerClick(p0: Marker?): Boolean {
  29. Log.e(TAG, "onMarkerClick + ${p0?.id}")
  30. return true
  31. }
  32. })

10、截图功能

拿到bitmap对象进行处理

  1. map?.getMapScreenShot(object : AMap.OnMapScreenShotListener {
  2. override fun onMapScreenShot(p0: Bitmap?) {
  3. TODO("Not yet implemented")
  4. }
  5. override fun onMapScreenShot(p0: Bitmap?, p1: Int) {
  6. TODO("Not yet implemented")
  7. }
  8. })

五、代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. <com.amap.api.maps.MapView
  7. android:id="@+id/map"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent" />
  10. <LinearLayout
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:layout_marginTop="20dp"
  14. android:gravity="center"
  15. android:orientation="horizontal">
  16. <TextView
  17. android:id="@+id/bj"
  18. android:layout_width="60dp"
  19. android:layout_height="wrap_content"
  20. android:background="@drawable/button_bg"
  21. android:gravity="center"
  22. android:padding="10dp"
  23. android:text="北京"
  24. android:textSize="18dp" />
  25. <TextView
  26. android:id="@+id/gz"
  27. android:layout_width="60dp"
  28. android:layout_height="wrap_content"
  29. android:layout_marginLeft="20dp"
  30. android:background="@drawable/button_bg"
  31. android:gravity="center"
  32. android:padding="10dp"
  33. android:text="广州"
  34. android:textSize="18dp" />
  35. <TextView
  36. android:id="@+id/xa"
  37. android:layout_width="60dp"
  38. android:layout_height="wrap_content"
  39. android:layout_marginLeft="20dp"
  40. android:background="@drawable/button_bg"
  41. android:gravity="center"
  42. android:padding="10dp"
  43. android:text="西安"
  44. android:textSize="18dp" />
  45. <ImageView
  46. android:id="@+id/share"
  47. android:layout_width="40dp"
  48. android:layout_height="40dp"
  49. android:layout_marginLeft="20dp"
  50. android:scaleType="fitXY"
  51. android:src="@drawable/screenshot" />
  52. </LinearLayout>
  53. </FrameLayout>
  1. package com.example.myapplication
  2. import android.graphics.Bitmap
  3. import android.graphics.Color
  4. import android.location.Location
  5. import android.os.Bundle
  6. import android.util.Log
  7. import android.widget.ImageView
  8. import android.widget.TextView
  9. import androidx.activity.ComponentActivity
  10. import com.amap.api.maps.AMap
  11. import com.amap.api.maps.CameraUpdateFactory
  12. import com.amap.api.maps.MapView
  13. import com.amap.api.maps.model.BitmapDescriptorFactory
  14. import com.amap.api.maps.model.CameraPosition
  15. import com.amap.api.maps.model.LatLng
  16. import com.amap.api.maps.model.Marker
  17. import com.amap.api.maps.model.MarkerOptions
  18. import com.amap.api.maps.model.Poi
  19. import com.amap.api.maps.model.PolylineOptions
  20. import com.amap.api.maps.utils.overlay.SmoothMoveMarker
  21. class TestActivity : ComponentActivity() {
  22. private var mapView: MapView? = null
  23. private var map: AMap? = null
  24. override fun onCreate(savedInstanceState: Bundle?) {
  25. super.onCreate(savedInstanceState)
  26. setContentView(R.layout.test_activity)
  27. initMap(savedInstanceState)
  28. initView()
  29. }
  30. private fun initView() {
  31. findViewById<TextView>(R.id.bj).setOnClickListener { moveCenterTo(LATLNG_BJ) }
  32. findViewById<TextView>(R.id.gz).setOnClickListener { moveCenterTo(LATLNG_GZ) }
  33. findViewById<TextView>(R.id.xa).setOnClickListener { moveCenterTo(LATLNG_XA) }
  34. findViewById<ImageView>(R.id.share).setOnClickListener {
  35. map?.getMapScreenShot(object : AMap.OnMapScreenShotListener {
  36. override fun onMapScreenShot(p0: Bitmap?) {
  37. TODO("Not yet implemented")
  38. }
  39. override fun onMapScreenShot(p0: Bitmap?, p1: Int) {
  40. TODO("Not yet implemented")
  41. }
  42. })
  43. }
  44. }
  45. private fun initMap(savedInstanceState: Bundle?) {
  46. mapView = findViewById(R.id.map)
  47. mapView?.onCreate(savedInstanceState)
  48. mapView?.let {
  49. map = it.map
  50. }
  51. map?.apply {
  52. uiSettings.let {
  53. it.isRotateGesturesEnabled = false
  54. it.isZoomControlsEnabled = false
  55. it.isTiltGesturesEnabled = false
  56. }
  57. moveCamera(CameraUpdateFactory.zoomTo(8.0f))
  58. //自定义图层
  59. // val options = CustomMapStyleOptions().apply {
  60. // isEnable = true
  61. // styleId = ""
  62. // }
  63. // setCustomMapStyle(options)
  64. // mapType = AMap.MAP_TYPE_NIGHT
  65. setOnMapLoadedListener(object : AMap.OnMapLoadedListener {
  66. override fun onMapLoaded() {
  67. Log.e(TAG, "onMapLoaded...")
  68. //AnimatorUtil.obtainLinePointF(mapView, MARKER1, MARKER2)
  69. // Handler().postDelayed(object : Runnable{
  70. // override fun run() {
  71. // AnimatorUtil.obtainLinePointF(mapView, MARKER2, MARKER3)
  72. // }
  73. // }, 500)
  74. }
  75. })
  76. //自带放大缩小接口
  77. setOnCameraChangeListener(object : AMap.OnCameraChangeListener {
  78. override fun onCameraChange(p0: CameraPosition?) {
  79. //Log.e(TAG, "onCameraChange + ${p0?.toString()}")
  80. }
  81. override fun onCameraChangeFinish(p0: CameraPosition?) {
  82. Log.e(TAG, "onCameraChangeFinish + ${p0?.toString()}")
  83. }
  84. })
  85. //map point点击事件
  86. addOnMapClickListener(object : AMap.OnMapClickListener {
  87. override fun onMapClick(p0: LatLng?) {
  88. Log.e(TAG, "onMapClick + ${p0?.toString()}")
  89. }
  90. })
  91. //地图自带poi点击事件
  92. addOnPOIClickListener(object : AMap.OnPOIClickListener {
  93. override fun onPOIClick(p0: Poi?) {
  94. Log.e(TAG, "onPOIClick + ${p0?.toString()}")
  95. }
  96. })
  97. addOnMarkerClickListener(object : AMap.OnMarkerClickListener {
  98. override fun onMarkerClick(p0: Marker?): Boolean {
  99. Log.e(TAG, "onMarkerClick + ${p0?.id}")
  100. return true
  101. }
  102. })
  103. setOnMyLocationChangeListener(object : AMap.OnMyLocationChangeListener {
  104. override fun onMyLocationChange(p0: Location?) {
  105. Log.e(TAG, "setOnMyLocationChangeListener + ${p0?.toString()}")
  106. p0?.let {
  107. moveCamera(CameraUpdateFactory.newLatLng(LatLng(it.latitude, it.longitude)))
  108. }
  109. }
  110. })
  111. //绘制marker点
  112. val marker1: MarkerOptions = MarkerOptions().apply {
  113. position(MARKER1)
  114. }
  115. val marker2: MarkerOptions = MarkerOptions().apply {
  116. position(MARKER2)
  117. }
  118. val list = ArrayList<MarkerOptions>()
  119. list.add(marker1)
  120. list.add(marker2)
  121. addMarkers(list, false)
  122. // val builder = LatLngBounds.builder().apply {
  123. // include(MARKER1)
  124. // include(MARKER2)
  125. // include(MARKER3)
  126. // include(MARKER4)
  127. // }
  128. // val bounds = builder.build()
  129. // moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 300))
  130. //绘制marker折线
  131. val latLngList = ArrayList<LatLng>()
  132. latLngList.add(MARKER1)
  133. latLngList.add(MARKER2)
  134. addPolyline(
  135. PolylineOptions().addAll(latLngList).width(3f)
  136. .color(Color.RED)
  137. )
  138. //轨迹
  139. SmoothMoveMarker(map).apply {
  140. setDescriptor(BitmapDescriptorFactory.fromResource(R.drawable.smooth))
  141. setPoints(latLngList)
  142. setTotalDuration(5)
  143. startSmoothMove()
  144. }
  145. }
  146. }
  147. private fun moveCenterTo(latLng: LatLng) {
  148. val cameraUpdate = CameraUpdateFactory.newLatLng(latLng)
  149. map?.moveCamera(cameraUpdate)
  150. }
  151. override fun onResume() {
  152. super.onResume()
  153. mapView?.onResume()
  154. }
  155. override fun onPause() {
  156. super.onPause()
  157. mapView?.onPause()
  158. }
  159. override fun onDestroy() {
  160. super.onDestroy()
  161. mapView?.onDestroy()
  162. }
  163. override fun onSaveInstanceState(outState: Bundle) {
  164. super.onSaveInstanceState(outState)
  165. mapView?.onSaveInstanceState(outState)
  166. }
  167. companion object {
  168. private const val TAG = "TestActivity"
  169. private val MARKER1 = LatLng(40.02855349893361, 116.3052948784071)
  170. private val MARKER2 = LatLng(41.093445392798934, 116.11030767409169)
  171. private val LATLNG_BJ = LatLng(39.90508988475248, 116.4083842390264)
  172. private val LATLNG_GZ = LatLng(23.11523439186301, 113.24706837513949)
  173. private val LATLNG_XA = LatLng(34.321288624880815, 108.94042782381482)
  174. }
  175. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/143225
推荐阅读