![](https://blog.cuihp.top/images/392cce9e113b9d37d09cadac0693d989.jpg)
个人感觉地图UI真的是很漂亮,如果你也在为你的应用寻找一款漂亮的地图,MapBox值得一试。看图:
Add Mapbox as a dependency to your build.gradle
file:
1 | repositories { |
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 | <uses-sdk android:minSdkVersion="15" |
API overview
MapView and MapboxMap
MapView
和MapboxMap
是Mapbox Android SDK的关键组件。 MapView的行为与任何其他视图一样,其行为可以使用XML布局文件静态更改,或者在运行时以编程方式更改。您可以将MapboxMap视为地图的控制器类。它包括设置和移动相机位置,添加标记,配置用户交互等的方法。
Lifecycle methods
MapView
包含自己的生命周期方法,用于管理Android的OpenGL生命周期,必须直接从包含的Activity
调用。为了让您的应用程序正确地调用MapView
的生命周期方法,您必须在包含MapView的Activity
中覆盖以下生命周期方法:
1 | onCreate(); |
XML layout
1 | <com.mapbox.mapboxsdk.maps.MapView |
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.