一、高德地图Api的使用
1、引入依赖
//高德3d地图
implementation 'com.amap.api:3dmap:9.1.0'
//高德搜索
implementation 'com.amap.api:search:8.1.0'
//高德定位
implementation 'com.amap.api:location:6.0.1'
2、AndroidManifest声明
<!-- 访问网络,进行地图相关业务数据请求,包括地图数据,路线规划,POI检索等 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!--允许程序打开网络套接字-->
<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" />
<service android:name="com.amap.api.location.APSService"/>
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="你申请的key" />
3、布局文件
<com.amap.api.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title" />
4、初始化mapview及定位初始化
binding.mapView.map.apply {
myLocationStyle = MyLocationStyle()
.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE)
.interval(60000)//一分钟定位一次
.showMyLocation(true)
.strokeColor(Color.TRANSPARENT)
.radiusFillColor(Color.TRANSPARENT)
.myLocationIcon(BitmapDescriptorFactory.fromResource(R.mipmap.ico_blue_point))//自定义定位蓝点
isMyLocationEnabled = true
}
/**
* 初始化定位,并启动定位
*/
private fun initLocate() {
//隐私申明,不然会崩溃
AMapLocationClient.updatePrivacyShow(this, true, true)
AMapLocationClient.updatePrivacyAgree(this, true)
MapsInitializer.updatePrivacyShow(this, true, true)
MapsInitializer.updatePrivacyAgree(this, true)
ServiceSettings.updatePrivacyShow(this, true, true)
ServiceSettings.updatePrivacyAgree(this, true)
val location = AMapLocationUtils.getInstance()
location.setCustomLocationListener(getLocationListener())
location.startLocation()
}
override fun onPause() {
super.onPause()
baseBinding!!.mapView.onPause()
}
override fun onResume() {
super.onResume()
baseBinding!!.mapView.onResume()
val grant = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
if (grant == PackageManager.PERMISSION_GRANTED) {
if (Locater.instance.noCallback()) {
Locater.instance.callback(getLocationListener())
}
}
}
override fun onDestroy() {
baseBinding!!.mapView.map.isMyLocationEnabled = false
baseBinding!!.mapView.onDestroy()
AMapLocationUtils.getInstance().destroyLocation()
super.onDestroy()
}
/**
* 定位监听器
*/
private fun getLocationListener(): AMapLocationListener {
return AMapLocationListener {
if (it.errorCode == AMapLocation.LOCATION_SUCCESS) {
Timber.i("--定位回调,结果=${it.toStr()}")
lat = it.latitude
lng = it.longitude
address = it.address
mmkv.encode(KEY_LAN, it.latitude)
mmkv.encode(KEY_LON, it.longitude)
mmkv.encode(KEY_ADDRESS, it.address)
mmkv.encode(KEY_CITY, it.city)
mmkv.encode(KEY_CITY_CODE, it.cityCode)
query = PoiSearch.Query(mSearchKw, "", it.cityCode)
//poi搜索结果监听
mNearSearch.setOnPoiSearchListener(object : PoiSearch.OnPoiSearchListener {
override fun onPoiSearched(poiResult: PoiResult?, rCode: Int) {
baseBinding?.llWait?.visibility = View.GONE
baseBinding?.llPerm?.visibility = View.GONE
poiResult?.let { poiResult ->
if (rCode == 1000 && !poiResult.pois.isNullOrEmpty()) {
mPointMarkMap.clear()
poiResult.pois.forEach { poi ->
//绘制标点、并记录点的位置名称。点击标点的时候可获取名称
val location = LatLng(poi.latLonPoint.latitude, poi.latLonPoint.longitude)
mPointMarkMap[location] = poi.snippet
drawPoint(location, poi.title, poi.snippet)
}
mAdapterGasPower.setItemIcon(mSearchIcon)
mAdapterGasPower.resetData(poiResult.pois)
}else {
toast("没有搜索到相关地点")
}
}
}
override fun onPoiItemSearched(p0: PoiItem?, p1: Int) {
}
})
searchNear(it.latitude, it.longitude)
} else {
Timber.i("定位失败,${it.errorCode}")
}
Locater.instance.stopLocation()
}
}
5、设置标记点的监听
private val adapter = object : AMap.InfoWindowAdapter {
override fun getInfoWindow(marker: Marker?): View {
marker?.let {
mInfoWindowView.findViewById<TextView>(R.id.tvAddr).text = marker.title
mInfoWindowView.findViewById<TextView>(R.id.tvDistance).text = "${ceil(AMapUtils.calculateLineDistance(LatLng(lat, lng), it.position))}米"
}
return mInfoWindowView
}
override fun getInfoContents(marker: Marker?): View {
marker?.let {
mInfoWindowView.findViewById<TextView>(R.id.tvAddr).text = marker.title
mInfoWindowView.findViewById<TextView>(R.id.tvDistance).text = "${ceil(AMapUtils.calculateLineDistance(LatLng(lat, lng), it.position))}米"
}
return mInfoWindowView
}
}
private fun searchNear(lat: Double, lng: Double) {
mNearSearch.bound = PoiSearch.SearchBound(LatLonPoint(lat, lng), 15000)
mNearSearch.searchPOIAsyn()
}
private fun drawPoint(latLng: LatLng, title: String, snippet: String){
val option = MarkerOptions().title(title).snippet(snippet).position(latLng).icon(mPointMarker)
baseBinding!!.mapView.map.addMarker(option)
}
private fun initListener(){
//地图标点点击监听
baseBinding!!.mapView.map.setOnMarkerClickListener {
it.showInfoWindow()
mEndPoiInfo = PoiItem(it.id,
LatLonPoint(it.position.latitude, it.position.longitude), it.title, it.snippet)
Timber.i("点击标点: ${it.title},经纬度:${it.position.toString()}, 位置:${mPointMarkMap[it.position]}")
return@setOnMarkerClickListener true
}
baseBinding!!.mapView.map.setInfoWindowAdapter(this)
}
|