1.注册MapBox,创建新令牌,获取私密令牌并记录保存
私密令牌只在创建成功后出现一次
私密令牌只在创建成功后出现一次
私密令牌只在创建成功后出现一次
2.在AndroidStudio中 ,配置私密令牌,打开项目文件gradle.properties ——>添加 MAPBOX_DOWNLOADS_TOKEN=你的私密令牌 或者打开文件«USER_HOME»/.gradle/gradle.properties。
3.配置公共令牌,创建文件app/src/main/res/values/developer-config.xml
- 声明 Mapbox 远程存储库,依赖项
maven {
url 'https://api.mapbox.com/downloads/v2/releases/maven'
authentication { basic(BasicAuthentication) }
credentials {
username = "mapbox"
password = providers.gradleProperty("MAPBOX_DOWNLOADS_TOKEN").get() }
}
注意:获取的名称就是第二步配置的私密令牌名称
app模块级 build.gradle中 添加依赖
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.6.2"
implementation "com.mapbox.maps:android:10.16.4"
implementation "com.mapbox.search:mapbox-search-android-ui:1.0.0-rc.6"
- 具体使用 直接上代码
//TODO 地图定位
private fun initMapView() {
val mode = AppCompatDelegate.getDefaultNightMode()
//使用的是 公共令牌 第三步创建的
searchEngine = SearchEngine.createSearchEngineWithBuiltInDataProviders(
SearchEngineSettings(getString(R.string.mapbox_access_token))
)
var customStyleFilePath = "mapbox://styles/" //使用自己定义的主题
val currentLanguage = Locale.getDefault().language
if (currentLanguage == "en") {
customStyleFilePath = if (mode == AppCompatDelegate.MODE_NIGHT_YES) {
//英文暗色ID 使用自己定义的主题
"mapbox://styles/"
} else {
//英文亮色ID 使用自己定义的主题
"mapbox://styles/"
}
} else if (currentLanguage == "zh") {
customStyleFilePath = if (mode == AppCompatDelegate.MODE_NIGHT_YES) {
"mapbox://styles/"
//中文暗色ID
} else {
"mapbox://styles/"
//中文亮色ID
}
}
//设置自定义地图样式
mMapView.getMapboxMap().loadStyleUri(customStyleFilePath)
//添加定位图层 图标固定居中
val crosshair = ImageView(requireContext())
crosshair.layoutParams = FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT,
Gravity.CENTER
)
crosshair.setImageResource(R.mipmap.icon_binding_point)
mMapView.addView(crosshair)
if (oldLatLng != null) {
//如果有坐标使用坐标定位
mMapView.getMapboxMap().setCamera(CameraOptions.Builder().center(oldLatLng).zoom(15.0).build()) //地图相机 放大 到15级别
val oldPoint = Point.fromLngLat(oldLatLng!!.longitude(), oldLatLng!!.latitude())
//根据坐标加载位置
findAddress(oldPoint)
} else {
//没有坐标就获取 当前用户位置 进行定位
mMapView.location.updateSettings {
//打开用户位置在地图上可见
enabled = true
}
mMapView.location.addOnIndicatorPositionChangedListener(object :
OnIndicatorPositionChangedListener {
override fun onIndicatorPositionChanged(point: Point) {
mMapView.getMapboxMap().setCamera(
CameraOptions.Builder().center(point).zoom(14.0).build()
)
findAddress(point)
mMapView.location.removeOnIndicatorPositionChangedListener(this)
//定位完成后清除用户位置可见
mMapView.location.updateSettings {
enabled = false
}
}
})
}
//添加地图移动监听
mMapView.getMapboxMap().addOnMoveListener(moveListener())
}
//地图移动监听
private fun moveListener() = object : OnMoveListener {
//在执行移动手势时调用@如果手势被处理,则返回true,否则返回false 不处理
override fun onMove(detector: MoveGestureDetector): Boolean {
return false
}
override fun onMoveBegin(detector: MoveGestureDetector) {
//在开始移动手势时调用。不处理
}
override fun onMoveEnd(detector: MoveGestureDetector) {
//在移动手势结束时调用 获取地图中心点 即是图标指向
val pointCenter = mMapView.getMapboxMap().cameraState.center
findAddress(pointCenter)
}
}
private fun findAddress(point: Point) {
//类型poi 获取5条数据
val listA = ArrayList<QueryType>()
listA.add(QueryType.POI)
val options = ReverseGeoOptions(
center = Point.fromLngLat(point.longitude(), point.latitude()), limit = 5, types = listA
)
searchRequestTask = searchEngine.search(options, searchCallback)
}
private val searchCallback = object : SearchCallback {
override fun onError(e: Exception) {
Log.i("SearchApiExample", "Reverse geocoding error", e)
}
override fun onResults(
results: List<com.mapbox.search.result.SearchResult>, responseInfo: ResponseInfo
) {
if (results.isEmpty()) {
// results 即是poi列表
}
}
}
}
定位思路:地图布局中添加中心图标,监听移动地图,停止后获取地图中心的坐标点进行逆地址解析
效果图:
个人写法,希望对大家有帮助,不喜勿喷。欢迎交流好的方法