在AndroidStudio集成Mapbox

来源:互联网 发布:微信商城开发源码下载 编辑:程序博客网 时间:2024/04/29 07:26

在AndroidStudio集成Mapbox

一、官网     点击打开链接


二、开始集成

1.先在官网注册登录

之后在个人主页https://www.mapbox.com/studio/点击Home的My access tokens创建 token

eg:"pk.eyJ1IjoiYW5kcm9pZHJhZGFjYXQiLCJhIjoiY2o0a3k0bDg5MG52ODJ4cWo5Y2F1czN6byJ9.pO2biWf4gfPr3HwtKZ7v9g"

2.添加Mapbox Android SDK依赖项

以下内容添加到app下的build.gradle

repositories {mavenCentral()} dependencies {compile('com.mapbox.mapboxsdk:mapbox-android-sdk:5.1.3@aar') {transitive=true}}


3.设置权限

将以下权限添加到AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest>...<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.INTERNET" />...</manifest>



4.添加地图视图类并声明布局

添加以下代码以创建地图视图活动类。 MainActivity.java
public class MainActivity extends AppCompatActivity {private MapView mapView; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Mapbox.getInstance(this, "pk.eyJ1IjoiYW5kcm9pZHJhZGFjYXQiLCJhIjoiY2o0a3k0bDg5MG52ODJ4cWo5Y2F1czN6byJ9.pO2biWf4gfPr3HwtKZ7v9g");setContentView(R.layout.activity_main);mapView = (MapView) findViewById(R.id.mapView);mapView.onCreate(savedInstanceState);} @Overridepublic void onStart() {super.onStart();mapView.onStart();} @Overridepublic void onResume() {super.onResume();mapView.onResume();} @Overridepublic void onPause() {super.onPause();mapView.onPause();} @Overridepublic void onStop() {super.onStop();mapView.onStop();} @Overridepublic void onLowMemory() {super.onLowMemory();mapView.onLowMemory();} @Overrideprotected void onDestroy() {super.onDestroy();mapView.onDestroy();} @Overrideprotected void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);mapView.onSaveInstanceState(outState);}}

然后在添加以下代码来声明布局。res layout activity_main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns: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.MapViewandroid:id="@+id/mapView"android:layout_width="match_parent"android:layout_height="match_parent"mapbox:mapbox_cameraTargetLat="40.73581"mapbox:mapbox_cameraTargetLng="-73.99155"mapbox:mapbox_styleUrl="mapbox://styles/mapbox/streets-v10"mapbox:mapbox_cameraZoom="11" /> </RelativeLayout>