MapBox Android版开发 1 配置
- 前言
- [MapBox V9 配置](#MapBox V9 配置)
- [MapBox V11 配置](#MapBox V11 配置)
前言
本文主要介绍如何使用MapBox SDK V9
和 V11
两个版开发Android地图应用。
准备工作:
- 注册MapBox账户
- 获取公钥
- 获取私钥(确保选中
Downloads:Read
,注意申请后只有一次机会复制机会)
主要步骤:
- 创建工程
- 配置地图
- 显示地图
- 运行效果
MapBox V9 配置
创建工程
- AndroidStudio 选择菜单
File
→New
→New Project...
,打开New Project
对话框; - 选择模板
Empty Views Activity
,单击Next
; - 项目参数参考如下,设置后,单击
Finish
;
参数 | 值 |
---|---|
Name |
mapdemo |
Package name |
com.example.mapdemo |
Save location |
默认路径或自定义路径即可 |
Language |
Java |
Minimum SDK |
默认推荐即可 |
Build configuration language |
Groovy DSL (build.gradle) |
- 至此创建项目完成,目录结构如下:
bash
├── app
│ ├── build.gradle
│ ├── libs
│ ├── proguard-rules.pro
│ └── src
│ ├── androidTest
│ ├── main
│ └── test
├── build.gradle
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradle.properties
├── gradlew
├── gradlew.bat
├── local.properties
└── settings.gradle
配置地图
配置私钥
- 找到
Gradle
用户主文件夹<<USER_HOME>>/.gradle
; - 创建文件
gradle.properties
,即<<USER_HOME>>/.gradle/gradle.properties
; - 将私钥添加到
gradle.properties
文件中:
properties
MAPBOX_DOWNLOADS_TOKEN=YOUR_SECRET_MAPBOX_ACCESS_TOKEN
配置公钥
- 在
app
模块中创建文件app/src/main/res/values/developer-config.xml
; - 将公钥添加到
developer-config.xml
文件中:
xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<string name="mapbox_access_token" translatable="false" tools:ignore="UnusedResources">YOUR_MAPBOX_ACCESS_TOKEN</string>
</resources>
配置仓库
- 打开文件
settings.gradle
,声明Mapbox Downloads API
的maven
。其中:- 用户名为
mapbox
; - 密码为私钥(从文件
<<USER_HOME>>/.gradle/gradle.properties
中获取)。
- 用户名为
gradle
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven {
url 'https://api.mapbox.com/downloads/v2/releases/maven'
authentication {
basic(BasicAuthentication)
}
credentials {
// Do not change the username below.
// This should always be `mapbox` (not your username).
username = 'mapbox'
// Use the secret token you stored in gradle.properties as the password
password = providers.gradleProperty("MAPBOX_DOWNLOADS_TOKEN").get()
}
}
}
}
配置依赖
- 文件
app/build.gradle
,dependencies
中添加依赖项。
gradle
dependencies {
implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:9.7.1'
}
配置权限
- 文件
AndroidManifest.xml
,manifest
标签中添加定位权限。
xml
<manifest>
<!-- Always include this permission -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- Include only if your app benefits from precise location access. -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>
地图初始化
- 自定义
Application
类; - 在
AndroidManifest.xml
中声明该Application
类; - 初始化地图。
DemoApp
类完整代码如下:
java
package com.example.mapdemo;
import android.app.Application;
import com.mapbox.mapboxsdk.Mapbox;
public class DemoApp extends Application {
@Override
public void onCreate() {
super.onCreate();
Mapbox.getInstance(this, getString(R.string.mapbox_access_token));
}
}
- 文件
AndroidManifest.xml
,application
中配置自定义Application
类。
xml
<application
android:name=".DemoApp"
>
</application>
显示地图
布局文件
activity_main.xml
中添加地图视图。
xml
<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:mapbox_cameraTargetLat="32.2857965"
app:mapbox_cameraTargetLng="104.293174"
app:mapbox_cameraZoom="2"
app:mapbox_uiCompassGravity="start|top" />
地图Activity
MainActivity
管理地图视图生命周期。完整代码如下:
java
package com.example.mapdemo;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.Style;
public class MainActivity extends AppCompatActivity {
MapView mapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(@NonNull MapboxMap mapboxMap) {
mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
// Map is set up and the style has loaded. Now you can add data or make other map adjustments
}
});
}
});
}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onStart() {
super.onStart();
mapView.onStart();
}
@Override
protected void onStop() {
super.onStop();
mapView.onStop();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
}
运行效果
MapBox V11 配置
版本兼容性
Version | v11 |
---|---|
Minimum Android Version | 5.0 (API level 21) |
Kotlin | 1.6.0 or later |
Target/Compile SDK Version | 33 |
NDK Version | 23.2.8568313 |
OpenGL | OpenGL ES 3.0 |
创建工程
- AndroidStudio 选择菜单
File
→New
→New Project...
,打开New Project
对话框; - 选择模板
Empty Views Activity
,单击Next
; - 项目参数参考如下,设置后,单击
Finish
;
参数 | 值 |
---|---|
Name |
mapdemo |
Package name |
com.example.mapdemo |
Save location |
默认路径或自定义路径即可 |
Language |
kotlin |
Minimum SDK |
默认推荐即可 |
Build configuration language |
Kotlin DSL (build.gradle.kts) |
- 至此创建项目完成,目录结构如下:
bash
├── app
│ ├── build.gradle.kts
│ ├── libs
│ ├── proguard-rules.pro
│ └── src
│ ├── androidTest
│ ├── main
│ └── test
├── build.gradle.kts
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradle.properties
├── gradlew
├── gradlew.bat
├── local.properties
└── settings.gradle.kts
配置地图
配置私钥
- 找到
Gradle
用户主文件夹<<USER_HOME>>/.gradle
; - 创建文件
gradle.properties
,即<<USER_HOME>>/.gradle/gradle.properties
; - 将私钥添加到
gradle.properties
文件中:
properties
MAPBOX_DOWNLOADS_TOKEN=YOUR_SECRET_MAPBOX_ACCESS_TOKEN
配置公钥
- 在
app
模块中创建文件app/src/main/res/values/mapbox_access_token.xml
: - 将公钥添加到
mapbox_access_token.xml
文件中。
xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<string name="mapbox_access_token" translatable="false" tools:ignore="UnusedResources">YOUR_MAPBOX_ACCESS_TOKEN</string>
</resources>
配置仓库
- 打开文件
settings.gradle.kts
,声明 Mapbox Downloads API 的maven
。其中:- 用户名为
mapbox
; - 密码为私钥(从文件
<<USER_HOME>>/.gradle/gradle.properties
中获取)。
- 用户名为
gradle
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
// Mapbox Maven repository
maven {
url = uri("https://api.mapbox.com/downloads/v2/releases/maven")
// Do not change the username below. It should always be "mapbox" (not your username).
credentials.username = "mapbox"
// Use the secret token stored in gradle.properties as the password
credentials.password = providers.gradleProperty("MAPBOX_DOWNLOADS_TOKEN").get()
authentication.create<BasicAuthentication>("basic")
}
}
}
配置依赖
- 文件
app/build.gradle.kts
,dependencies
中添加依赖项。
gradle
dependencies {
implementation("com.mapbox.maps:android:11.6.0")
}
配置权限
- 文件
AndroidManifest.xml
,manifest
标签中添加定位权限。
xml
<manifest>
<!-- Always include this permission -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- Include only if your app benefits from precise location access. -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>
显示地图
布局文件
activity_main.xml
中添加地图视图。
xml
<com.mapbox.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:mapbox_cameraTargetLat="32.2857965"
app:mapbox_cameraTargetLng="104.293174"
app:mapbox_cameraZoom="2"
app:mapbox_cameraPitch="0.0"
app:mapbox_cameraBearing="0.0" />