MapBox 地图

个人感觉地图UI真的是很漂亮,如果你也在为你的应用寻找一款漂亮的地图,MapBox值得一试。看图:

CuiHp

Add Mapbox as a dependency to your build.gradle file:

1
2
3
4
5
6
7
8
9
repositories {
mavenCentral()
}

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

Required configuration

Permissions

从5.0开始,减少了需要包括任何地图SDK所需的东西在您的应用程序的清单文件。如果您计划在地图上显示用户位置或获取用户位置信息,您将需要添加位置许可。用户位置的权限也应检查使用AndroidManifest.xml时:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
or
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Supported Android versions

MAPbox Android SDK是应用程序运行Android 15及以上。确保在你的应用程序的例子,请设置在应用程序清单以不低于15。

1
2
3
<uses-sdk android:minSdkVersion="15"
android:targetSdkVersion="integer"
android:maxSdkVersion="integer" />

API overview

MapView and MapboxMap

MapViewMapboxMap是Mapbox Android SDK的关键组件。 MapView的行为与任何其他视图一样,其行为可以使用XML布局文件静态更改,或者在运行时以编程方式更改。您可以将MapboxMap视为地图的控制器类。它包括设置和移动相机位置,添加标记,配置用户交互等的方法。

Lifecycle methods

MapView包含自己的生命周期方法,用于管理Android的OpenGL生命周期,必须直接从包含的Activity调用。为了让您的应用程序正确地调用MapView的生命周期方法,您必须在包含MapView的Activity中覆盖以下生命周期方法:

1
2
3
4
5
6
7
8
onCreate();
onStart();
onResume();
onPause();
onStop();
onSaveInstanceState();
onLowMemory();
onDestroy();

XML layout

1
2
3
4
5
<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:mapbox_styleUrl="@string/style_mapbox_streets" />

Access tokens

需要使用访问令牌来使用Mapbox服务和API,例如地图,路线和地理编码。您的访问令牌可以在您的帐户设置中进行管理,您可以在其中检索当前的令牌并生成新的令牌。您应该为每个应用程序创建一个新的令牌,这将有助于您跟踪使用情况,并在令牌需要撤销的情况下尽量减少中断。

在您的应用程序中提供访问令牌有两个地方。如果您使用自定义应用程序对象,则可以将其放在onCreate()中,也可以在应用程序启动活动onCreate()上。在这两种情况下,您都需要调用MapboxAccountManager并传递上下文和访问令牌:

1
Mapbox.getInstance(context, <your access token here>);

Map styles

  • Mapbox Streets: Mapbox Streets is a comprehensive, general-purpose map that emphasizes accurate, legible styling of road and transit networks.
  • Outdoor: Mapbox Outdoors is a general-purpose map with curated datasets and specialized styling tailored to hiking, biking, and the most adventurous use cases..
  • Light and Dark: Mapbox Light and Mapbox Dark are subtle, full-featured maps designed to provide geographic context while highlighting the data on your analytics dashboard, data visualization, or data overlay.
  • Satellite: is our full global base map that is perfect as a blank canvas or an overlay for your own data.
  • Satellite Streets: combines our Mapbox Satellite with vector data from Mapbox Streets. The comprehensive set of road, label, and POI information brings clarity and context to the crisp detail in our high-resolution satellite imagery.

更多使用—>官网地址