首先開啟 Android Studio ,[ File ] ⇒ [ New ] ⇒ [ New Project ],建立新專案,使用 Empty ... api 'com.mapbox.mapboxsdk:mapbox-android-telemetry:4.5.1' api ...
NAV
建立一個新專案
安裝Map8SDKaar
檢查build.gradle內容的完整性
加入定位的permisson
顯示地圖
請點此申請試用
歡迎試試我們的APIExplorer
建立一個新專案
首先開啟AndroidStudio,[File]⇒[New]⇒[NewProject],建立新專案,使用EmptyActivity
接著設定您的appname,Language使用的是Java
最後儲存您的專案位置
QuickStart:等不及,或是您很熟,請直接參見範例程式。
安裝Map8SDKaar
下載Map8AndroidSDK-release.aarhttps://api.map8.zone/download/maps_sdk/map8-maps-sdk-android.zip?key=您的key,將檔案放置於您的專案目錄中
開啟AndroidStudio,[File]⇒[New]⇒[NewModule]
選擇Import.JAR/.AARPackage
最後選擇剛剛下載的Map8AndroidSDK-release.aar路徑位置
檢查build.gradle內容的完整性
參考以下描述,檢查build.gradle中dependencies區塊的完整性:
...
dependencies{
implementationfileTree(dir:'libs',include:['*.jar'])
implementation'androidx.appcompat:appcompat:1.1.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'com.mapbox.mapboxsdk:mapbox-android-accounts:0.2.0'
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:4.8.0'
implementation'com.android.support:support-fragment:28.0.0'
implementation'com.squareup.okhttp3:okhttp:3.12.3'
implementation'com.facebook.soloader:soloader:0.6.1'
implementationproject(":Map8AndroidSDK-release")
}
...
加入定位的permisson
在AndroidManifest.xml加入
...
...
顯示地圖
在MainActivity中,填上以下範例程式,即可以顯示地圖:
publicclassMainActivityextendsAppCompatActivityimplementsPermissionsListener{
//Taiwanmapboundary
privatestaticfinalLatLngTW_BOUND_CORNER_NE=newLatLng(33.4,138.45858);
privatestaticfinalLatLngTW_BOUND_CORNER_SW=newLatLng(15,105);
privatestaticfinalLatLngBoundsTW_BOUNDS_AREA=newLatLngBounds.Builder()
.include(TW_BOUND_CORNER_NE)
.include(TW_BOUND_CORNER_SW)
.build();
privatePermissionsManagerpermissionsManager;
privateMapboxMapmapboxMap;
privateMapViewmapView;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
Map8.getInstance(this,"");
MapboxMapOptionsoptions=MapboxMapOptions.createFromAttributes(this,null)
.camera(newCameraPosition.Builder()
.target(newLatLng(25.03625,121.54885))
.zoom(16)
.bearing(0)
.tilt(50)
.build());
//createmap
mapView=newMapView(this,options);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(newOnMapReadyCallback(){
@Override
publicvoidonMapReady(@NonNullMapboxMapmapboxMap){
MainActivity.this.mapboxMap=mapboxMap;
mapboxMap.setMaxZoomPreference(19.99);
mapboxMap.setMinZoomPreference(6);
mapboxMap.setLatLngBoundsForCameraTarget(TW_BOUNDS_AREA);
mapboxMap.setStyle(newStyle.Builder().fromUri("https://api.map8.zone/styles/go-life-maps-tw-style-std/style.json"),newStyle.OnStyleLoaded(){
@Override
publicvoidonStyleLoaded(@NonNullStylestyle){
Log.d("[Map8SDKDemo]","mapView.getMapAsync()>onMapReady()>setStyle()>onStyleLoaded()");
enableLocationComponent(style);
}
});
}
});
setContentView(mapView);
}
@SuppressWarnings({"MissingPermission"})
privatevoidenableLocationComponent(@NonNullStyleloadedMapStyle){
//Checkifpermissionsareenabledandifnotrequest
if(PermissionsManager.areLocationPermissionsGranted(this)){
//Getaninstanceofthecomponent
LocationComponentlocationComponent=mapboxMap.getLocationComponent();
//Activatewithoptions
locationComponent.activateLocationComponent(
LocationComponentActivationOptions.builder(this,loadedMapStyle).build());
//Enabletomakecomponentvisible
locationComponent.setLocationComponentEnabled(true);
//Setthecomponent'scameramode
locationComponent.setCameraMode(CameraMode.TRACKING);
//Setthecomponent'srendermode
locationComponent.setRenderMode(RenderMode.COMPASS);
}else{
permissionsManager=newPermissionsManager(this);
permissionsManager.requestLocationPermissions(this);
}
}
@Override
publicvoidonRequestPermissionsResult(intrequestCode,@NonNullString[]permissions,@NonNullint[]grantResults){
permissionsManager.onRequestPermissionsResult(requestCode,permissions,grantResults);
}
@Override
publicvoidonExplanationNeeded(ListpermissionsToExplain){
Toast.makeText(this,"Thisappneedslocationpermissionsinordertoshowitsfunctionality.",Toast.LENGTH_LONG).show();
}
@Override
publicvoidonPermissionResult(booleangranted){
if(granted){
mapboxMap.getStyle(newStyle.OnStyleLoaded(){
@Override
publicvoidonStyleLoaded(@NonNullStylestyle){
enableLocationComponent(style);
}
});
}else{
Toast.makeText(this,"Thisappneedslocationpermissionsinordertoshowitsfunctionality.",Toast.LENGTH_LONG).show();
finish();
}
}
//AddthemapView'sownlifecyclemethodstotheactivity'slifecyclemethods
@SuppressWarnings({"MissingPermission"})
@Override
publicvoidonStart(){
super.onStart();
mapView.onStart();
}
@Override
publicvoidonResume(){
super.onResume();
mapView.onResume();
}
@Override
publicvoidonPause(){
super.onPause();
mapView.onPause();
}
@Override
publicvoidonStop(){
super.onStop();
mapView.onStop();
}
@Override
publicvoidonLowMemory(){
super.onLowMemory();
mapView.onLowMemory();
}
@Override
protectedvoidonDestroy(){
super.onDestroy();
mapView.onDestroy();
}
@Override
protectedvoidonSaveInstanceState(BundleoutState){
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
}
接著執行Run,應該就會顯示出地圖
恭喜!Congratulations!!!:D
台灣圖霸感謝您的支持與愛護!
有任何疑問,或是指教,都非常歡迎您找我們詢問。
非常感謝!
https://map8.zone