第一步 依賴。
project 下 build.gradle
repositories {
jcenter()
mavenCentral()
}
moudle 下 build.gradle
compile('com.mapbox.mapboxsdk:mapbox-android-sdk:5.0.2'){
transitive=true
}
同步之後報錯:
WTF?這是什麼鬼東西嘛。
網上搜了搜在版本號後邊加上@aar
compile('com.mapbox.mapboxsdk:mapbox-android-sdk:5.0.2@aar'){
transitive=true
}
重新同步後依賴成功。
第二步 在MapBox官網註冊賬號並申請token。MapBox
第三步 在Application中初始化
public class App extends Application{
@Override
public void onCreate() {
super.onCreate();
//設置accessToken 申請到的token放到strings.xml
Mapbox.getInstance(this,getString(R.string.MapBox_token));
}
}
AndroidManifest.xml中application 節點下配置
android:name=".App"
第四步 添加權限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
第五步 使用地圖
佈局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
Activity
public class MainActivity extends Activity {
private MapView mapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(MapboxMap mapboxMap) {
}
});
}
@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);
}
}
然後運行項目,應該沒問題了吧。結果是
這又是什麼鬼嘛。網上說可能是模擬器的原因。
剛開始使用的夜神,所以換成了自帶的6.0模擬器重新運行。
發現地圖已經展現出來了。