MapLibre Native 是一個免費且開源的庫,用於在各種平臺上的應用程式和桌面應用程式中釋出地圖。由於 GPU 加速的向量瓦片渲染,地圖的快速顯示成為可能。
MapLibre Native 安卓整合文件。
1,module級gradle中配置對應外掛
dependencies {
implementation 'org.maplibre.gl:android-sdk:11.0.0'
}
2,settings.gradle中配置一下依賴資源的代理加速
沒有網路問題太慢問題忽略此步驟
pluginManagement {
repositories {
maven {
url 'https://maven.aliyun.com/repository/public/'
}
maven {
url 'https://maven.aliyun.com/repository/google/'
}
maven {
url 'https://maven.aliyun.com/repository/jcenter/'
}
maven {
url 'https://maven.aliyun.com/repository/central/'
}
google()
mavenCentral()
gradlePluginPortal()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
maven {
url 'https://maven.aliyun.com/repository/public/'
}
maven {
url 'https://maven.aliyun.com/repository/google/'
}
maven {
url 'https://maven.aliyun.com/repository/jcenter/'
}
maven {
url 'https://maven.aliyun.com/repository/central/'
}
google()
mavenCentral()
}
}
3,在xml佈局檔案中新增對應元件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<org.maplibre.android.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
4,新增地圖對應載入邏輯
官方預設使用的kotlin,暫未找到java版本的,我們也用kotlin
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import org.maplibre.android.MapLibre
import org.maplibre.android.maps.MapView
class MainActivity : AppCompatActivity() {
// Declare a variable for MapView
private lateinit var mapView: MapView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// 初始化
MapLibre.getInstance(this)
val inflater = LayoutInflater.from(this)
val rootView = inflater.inflate(R.layout.activity_main, null)
setContentView(rootView)
// Init the MapView
// 繫結節點
mapView = rootView.findViewById(R.id.mapView)
// 載入資料
mapView.getMapAsync { map ->
map.setStyle("https://demotiles.maplibre.org/style.json")
// map.cameraPosition = CameraPosition.Builder().Buildertarget(LatLng(0.0,0.0)).zoom(1.0).build()
}
}
override fun onStart() {
super.onStart()
mapView.onStart()
}
override fun onResume() {
super.onResume()
mapView.onResume()
}
override fun onPause() {
super.onPause()
mapView.onPause()
}
override fun onStop() {
super.onStop()
mapView.onStop()
}
override fun onLowMemory() {
super.onLowMemory()
mapView.onLowMemory()
}
override fun onDestroy() {
super.onDestroy()
mapView.onDestroy()
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
mapView.onSaveInstanceState(outState)
}
}
5,如遇打包報錯的異常情況
如果出現報錯類似的錯誤
Error:Kotlin: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.7.0, expected version is 1.9.0.
注意切換當前專案專案級gradle裡面的的kotlin到對應提示的版本就行。
id 'org.jetbrains.kotlin.android' version '1.9.0' apply false 的作用主要是宣告和指定Kotlin Android Gradle外掛的版本
plugins {
id 'org.jetbrains.kotlin.android' version '1.9.0' apply false
}
有空繼續更新...