建立一個新專案
首先開啟 Android Studio,[ File ] ⇒ [ New ] ⇒ [ New Project ],建立新專案,範本請選擇 Empty Views Activity

接著設定您的 app name,Language 選擇 Java,Build configuration language 選擇 Groovy DSL (build.gradle)

最後儲存您的專案位置
安裝 Map8 SDK aar
下載 SDK 壓縮檔 https://api.map8.zone/download/maps_sdk/map8-maps-sdk-android.zip?key=您的key,解壓縮後會取得以下兩個檔案:
Map8AndroidSDK-release.aarmap8-android-accounts-0.2.0.aar
在 Android Studio 左上角,將 Project 面板的檢視模式由 Android 切換為 Project

切換後,在 app 資料夾上按右鍵 ⇒ [ New ] ⇒ [ Directory ],建立一個名為 libs 的資料夾,並將剛剛解壓縮出來的兩個 .aar 檔案拖曳放入

檢查 build.gradle 內容的完整性
參考以下描述,檢查 app/build.gradle 中 dependencies 區塊的完整性:
...
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.10.0'
implementation 'androidx.core:core:1.6.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
api 'com.mapbox.mapboxsdk:mapbox-android-telemetry:4.5.1'
api 'com.mapbox.mapboxsdk:mapbox-sdk-geojson:4.8.0'
api 'com.mapbox.mapboxsdk:mapbox-android-gestures:0.5.1'
api files('libs/map8-android-accounts-0.2.0.aar')
implementation 'com.mapbox.mapboxsdk:mapbox-sdk-turf:4.8.0'
implementation 'com.mapbox.mapboxsdk:mapbox-sdk-services:4.8.0'
implementation 'com.android.support:support-annotations:28.0.0'
implementation 'com.android.support:support-fragment:28.0.0'
implementation 'com.squareup.okhttp3:okhttp:3.12.3'
implementation 'com.facebook.soloader:soloader:0.12.1'
implementation files('libs/Map8AndroidSDK-release.aar')
}
...
最後,請在 gradle.properties 中加入以下設定:
android.useAndroidX=true
android.enableJetifier=true
加入定位的 permisson
在 AndroidManifest.xml 加入
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
...
<application
...
</application>
...
</manifest>
顯示地圖
在 MainActivity 中,填上以下範例程式,即可以顯示地圖:
package com.example.mysdkdemo;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.mapbox.android.core.permissions.PermissionsListener;
import com.mapbox.android.core.permissions.PermissionsManager;
import com.mapbox.mapboxsdk.Map8;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.location.LocationComponent;
import com.mapbox.mapboxsdk.location.LocationComponentActivationOptions;
import com.mapbox.mapboxsdk.location.modes.CameraMode;
import com.mapbox.mapboxsdk.location.modes.RenderMode;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.MapboxMapOptions;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.Style;
import java.util.List;
public class MainActivity extends AppCompatActivity implements PermissionsListener {
// Taiwan map boundary
private static final LatLng TW_BOUND_CORNER_NE = new LatLng(33.4, 138.45858);
private static final LatLng TW_BOUND_CORNER_SW = new LatLng(15, 105);
private static final LatLngBounds TW_BOUNDS_AREA = new LatLngBounds.Builder()
.include(TW_BOUND_CORNER_NE)
.include(TW_BOUND_CORNER_SW)
.build();
private PermissionsManager permissionsManager;
private MapboxMap mapboxMap;
private MapView mapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Map8.getInstance(this, "<您的 Map8 Key>");
MapboxMapOptions options = MapboxMapOptions.createFromAttributes(this, null)
.camera(new CameraPosition.Builder()
.target(new LatLng(25.03625, 121.54885))
.zoom(16)
.bearing(0)
.tilt(50)
.build());
// create map
mapView = new MapView(this, options);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(@NonNull MapboxMap mapboxMap) {
MainActivity.this.mapboxMap = mapboxMap;
mapboxMap.setMaxZoomPreference(19.99);
mapboxMap.setMinZoomPreference(6);
mapboxMap.setLatLngBoundsForCameraTarget(TW_BOUNDS_AREA);
mapboxMap.setStyle(new Style.Builder().fromUri("https://api.map8.zone/styles/go-life-maps-tw-style-std/style.json"), new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
Log.d("[Map8 SDK Demo]", "mapView.getMapAsync() > onMapReady() > setStyle() > onStyleLoaded()");
enableLocationComponent(style);
}
});
}
});
setContentView(mapView);
// 新款手機(手勢導覽列等 edge-to-edge 顯示)預設會讓畫面延伸到系統列底下,
// 若不處理 inset,左下角的歸屬標籤會被系統列遮住。
ViewCompat.setOnApplyWindowInsetsListener(mapView, (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
}
@SuppressWarnings( {"MissingPermission"})
private void enableLocationComponent(@NonNull Style loadedMapStyle) {
// Check if permissions are enabled and if not request
if (PermissionsManager.areLocationPermissionsGranted(this)) {
// Get an instance of the component
LocationComponent locationComponent = mapboxMap.getLocationComponent();
// Activate with options
locationComponent.activateLocationComponent(
LocationComponentActivationOptions.builder(this, loadedMapStyle).build());
// Enable to make component visible
locationComponent.setLocationComponentEnabled(true);
// Set the component's camera mode
locationComponent.setCameraMode(CameraMode.TRACKING);
// Set the component's render mode
locationComponent.setRenderMode(RenderMode.COMPASS);
} else {
permissionsManager = new PermissionsManager(this);
permissionsManager.requestLocationPermissions(this);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
permissionsManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
public void onExplanationNeeded(List<String> permissionsToExplain) {
Toast.makeText(this, "This app needs location permissions in order to show its functionality.", Toast.LENGTH_LONG).show();
}
@Override
public void onPermissionResult(boolean granted) {
if (granted) {
mapboxMap.getStyle(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
enableLocationComponent(style);
}
});
} else {
Toast.makeText(this, "This app needs location permissions in order to show its functionality.", Toast.LENGTH_LONG).show();
finish();
}
}
// Add the mapView's own lifecycle methods to the activity's lifecycle methods
@SuppressWarnings( {"MissingPermission"})
@Override
public void onStart() {
super.onStart();
mapView.onStart();
}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onStop() {
super.onStop();
mapView.onStop();
}
@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);
}
}
接著執行 Run,應該就會顯示出地圖

恭喜!Congratulations!!! :D
台灣圖霸感謝您的支持與愛護!
有任何疑問,或是指教,都非常歡迎您找我們詢問。
非常感謝!