Android地图应用新视界--mapbox的应用开发之初始集成篇

2024-06-01 08:08

本文主要是介绍Android地图应用新视界--mapbox的应用开发之初始集成篇,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

mapbox的应用开发之初始集成篇

最近一款应用需要集成可用于全球的Android地图应用,本应首选Googlemap,但因为众所周知的原因在国内无法使用,并且国内的手机出厂时大多阉割Google service的部分导致不可用,即便可以用也没有数据(与苹果的原生地图全球可用真是好伤心,期盼Google快点回归天朝吧);譬如百度,高德抑或腾讯地图只在国内有数据,国外无法使用,也综合考虑了微软的bing map 和诺基亚的here map,都对Android端不太好用.综合考虑之下选用mapbox  这是一款开源多平台的库可在全球使用并且支持很多自定义和扩展化,当然难度也较大 ,本文只引用和介绍初始的集成应用  ,后续会有各种开发的具体实现和demo分享,有兴趣的可以一试:
好了话不多说,直接参考如下,英文较多但也简单易懂

First steps with the Mapbox Android SDK

The Mapbox Android SDK is our vector maps library for Android. This guide will walk you through installing the Mapbox Android SDK with Android Studio, loading a map, placing a pin on it, and changing the map style.

Heads up! We’re still actively developing the Mapbox Android SDK and will update this guide and add new ones as we improve its amazing features.

Getting started

Here’s what you’ll need to get started:

  • An access token. You can find your access tokens on your account page.

  • Android Studio. You can get this for free from Google.

  • Google Play Developer Account (optional). If you want to publish your app to Google Play, you’ll need a Google Play developer account. Without one, you’ll still be able to preview the app on an Android Virtual Device (AVD) or install the demo app from an SDK on a physical device.

Setting up Android Studio

Open Android Studio and go to File ‣ New ‣ New Project to create a new project (or select Start a new Android Studio project). Under Configure your new project:

  • Name the project My First GL App
  • Set company domain to mycompany.com

Under Select the form factors your app will run on, check “Phone and Tablet.” For minimum SDK, select API 15: Android 4.0.3 (IceCreamSandwich). This is the lowest API level currently supported by Mapbox Android SDK.

Click Next to advance to the activity selection screen. Select Empty Activity and click Next. Accept the default Activity Name and Layout Name and click Finish.

Set up an Android Virtual Device

Android Studio can help you set up virtual Android devices on your computer to help test your app while you develop. To set one up, click on the Android Virtual Device (AVD) Manager icon , then click the Create Virtual Device button. From the Phones category, select Nexus 5 and click Next. Select the release you would like to test against (this guide was built using Lollipop API level 23, x86, Android 6.0). Learn more about setting up an AVD from Android Studio’s documentation.

Installing Mapbox

We recommend installing with Gradle. This will automatically install the necessary dependencies and pull the SDK binaries from the Maven Central repository Mapbox Android SDK on Maven Central.

To install the current stable version, add this to your Module Build File <PROJECT_ROOT>\app\build.gradle:

repositories {mavenCentral()}dependencies {compile fileTree(dir: 'libs', include: ['*.jar'])testCompile 'junit:junit:4.12'compile 'com.android.support:appcompat-v7:23.1.1'// add the Mapbox SDK dependency belowcompile ('com.mapbox.mapboxsdk:mapbox-android-sdk:4.0.0-beta.2@aar'){transitive=true}}

For a full example Android project incorporating the SDK in this manner, please see the Mapbox Android SDK test app.

  • Source: https://github.com/mapbox/mapbox-gl-native/tree/master/platform/android/MapboxGLAndroidSDKTestApp/

App permissions

Ensure the following core permissions are requested in your project’s AndroidManifest.xml file from ~/MyFirstGLApp/app/src/main/:

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

If the app is targeting Android Marshmallow (API 23) or later it’s necessary to request permissions at runtime. Additionally, you’ll need to add the telemetry service inside your <application> tags:

<service android:name="com.mapbox.mapboxsdk.telemetry.TelemetryService" />

Access tokens

To use Mapbox services and APIs, such as maps, directions, and geocoding, you must have a Mapbox access token. You can find your access tokens in your account settings, where you can retrieve current tokens and generate new ones. Access tokens help you track usage and minimize disruption in the event a token needs to be revoked.

First, go to your account settings and create a new access token for My First GL App. Once you’ve got your token, open strings.xml file from ~/MyFirstGLApp/app/src/main/res/values.

Add a new string for your access token:

<resources><string name="app_name">My First GL App</string><string name="accessToken"><your access token here></string></resources>

We’ll reference the string in the next step.

MapView

The MapView class is the key component of our library. It behaves like any other View and its behavior can be changed statically with an XML layout file, or programmatically during runtime.

To add the MapView as a layout element, replace the code in the activity_main.xml file from ~/MyFirstGLApp/app/src/main/res/layout/ with the following:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:mapbox="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><com.mapbox.mapboxsdk.maps.MapViewandroid:id="@+id/mapview"android:layout_width="fill_parent"android:layout_height="fill_parent"mapbox:access_token="@string/accessToken"/><!-- note the access token string created in the previous step --></RelativeLayout>

Later on, you can call the MapView class programmatically within an Activity:

findViewById(R.id.mapview);

MapboxMap

Once you have declared and assigned your MapView, you will need to call MapView.getMapAsync to create a MapboxMap object. The MapboxMap object allows you to change styles and interact with your map.

mapView.getMapAsync(new OnMapReadyCallback() {@Overridepublic void onMapReady(MapboxMap mapboxMap) {// Interact with the map using mapboxMap here}});

Now you’re ready to start building with Mapbox!

Adding a map

Let’s add a map of Chicago to our app. To start, open your My First GL App in Android Studio and navigate to ~/app/src/main/java/com.mycompany.myfirstglapp/MainActivity.java.

Screenshot

We’re going to use the mapView class we set up earlier to add a Mapbox map to our app. The app needs to know when to add the map, so we’ll do so when the activity is initialized with onCreate.

private MapView mapView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// Create a mapViewmapView = (MapView) findViewById(R.id.mainMapView);mapView.onCreate(savedInstanceState);}

Next, use your new mapView class to create a MapboxMap object. MapboxMap has lots of built-in methods that will allow you to apply styles, change the camera position, add markers, and more. Create your MapboxMap:

mapView.getMapAsync(new OnMapReadyCallback() {@Overridepublic void onMapReady(MapboxMap mapboxMap) {// Customize map with markers, polylines, etc.}});

Configure your mapView

Starting with version 4.0.0 of the Mapbox Android SDK, you can configure many of your map’s characteristics, including starting camera position, style URL, and access token, in your activity’s layout file. Add the following to your activity’s layout file, inside your com.mapbox.mapboxsdk.maps.MapViewtags:

mapbox:style_url="@string/style_mapbox_streets"
mapbox:center_latitude="41.885"
mapbox:center_longitude="-87.679"
mapbox:zoom="12"
mapbox:tilt="20"

When you’ve finished entering the above code, you will probably see some red warning text from Android Studio. This is because we haven’t yet imported some of the classes we’re referencing in MainActivity.java. You can automatically import these classes by pressing Alt + Enter (option + return on Mac). Alternatively, manually add the following to the top of your MainActivity.java file:

import android.app.Activity;import android.os.Bundle;import com.mapbox.mapboxsdk.camera.CameraPosition;import com.mapbox.mapboxsdk.constants.Style;import com.mapbox.mapboxsdk.geometry.LatLng;import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;import com.mapbox.mapboxsdk.maps.MapView;import com.mapbox.mapboxsdk.maps.MapboxMap;import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;

Next, we’ll handle the other types of events that might affect our map app:

@Overridepublic void onResume() {super.onResume();mapView.onResume();}@Overridepublic void onPause() {super.onPause();mapView.onPause();}@Overridepublic void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);mapView.onSaveInstanceState(outState);}@Overridepublic void onLowMemory() {super.onLowMemory();mapView.onLowMemory();}@Overrideprotected void onDestroy() {super.onDestroy();mapView.onDestroy();}

Click the Run ‘app’ button  to build your app. Android Studio will take a few seconds to build and if it finishes without errors, you’ll be able to test drive it in the AVD you set up earlier.

Changing the map style

The Mapbox Android SDK comes bundled with a handful of map styles, so let’s try changing it up a bit. You can find a list of the current bundled styles with the Mapbox SDK’s Style constant. Let’s use the LIGHT style (the version of Mapbox Light that conforms to the v8 release of the GL style specification).

Hint: Try typing mapboxMap.setStyleUrl(Style. and Android Studio will give you a list of available styles to choose from.

To set your map’s initial style to LIGHT, open your activity_main.xml layout file and set mapbox:style_urlto @string/style_light:

mapbox:style_url="@string/style_light"

You can also change your style programmatically using MapboxMap’s setStyleURL method. Inside onCreate(), change the line that sets mapboxMap’s style to:

mapboxMap.setStyleUrl(Style.LIGHT);

Creating your own GL styles

You can create your own custom GL styles with Mapbox Studio and and add them to your app. To add one of your custom styles to your mapboxMap, head to your styles page, copy your style’s style URL, then add it to your mapboxMap with setStyleURL():

mapboxMap.setStyleUrl("mapbox://styles/<your-account-name>/<your-style-ID>");

Using Mapbox Studio Classic styles and maps

Raster maps and styles created with Mapbox Studio Classic are not directly compatible with the Android SDK, though you can still load these maps via a custom GL style. Please note that associated markers, lines, and shapes are not supported.

Next steps

You just built a small Android app with Mapbox! You can now create an Android Studio project, install the Mapbox SDK, add a map, place a pin on it, and change the map style. Pat yourself on the back!

As you continue to develop your Mapbox app, we recommend that you read the following:

  • Attribution — comply with the licensing terms of the map data used in your application

We’ll be adding and updating guides to help you learn all of Mapbox’s amazing features as we continue to develop it. Here are a few resources to keep you up-to-date with Mapbox:

  • Mapbox Android SDK documentation
  • Mapbox GL Native on GitHub to follow the open source project behind Mapbox Mobile

这篇关于Android地图应用新视界--mapbox的应用开发之初始集成篇的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1020446

相关文章

Java中Springboot集成Kafka实现消息发送和接收功能

《Java中Springboot集成Kafka实现消息发送和接收功能》Kafka是一个高吞吐量的分布式发布-订阅消息系统,主要用于处理大规模数据流,它由生产者、消费者、主题、分区和代理等组件构成,Ka... 目录一、Kafka 简介二、Kafka 功能三、POM依赖四、配置文件五、生产者六、消费者一、Kaf

基于Python开发电脑定时关机工具

《基于Python开发电脑定时关机工具》这篇文章主要为大家详细介绍了如何基于Python开发一个电脑定时关机工具,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 简介2. 运行效果3. 相关源码1. 简介这个程序就像一个“忠实的管家”,帮你按时关掉电脑,而且全程不需要你多做

Java中的Opencv简介与开发环境部署方法

《Java中的Opencv简介与开发环境部署方法》OpenCV是一个开源的计算机视觉和图像处理库,提供了丰富的图像处理算法和工具,它支持多种图像处理和计算机视觉算法,可以用于物体识别与跟踪、图像分割与... 目录1.Opencv简介Opencv的应用2.Java使用OpenCV进行图像操作opencv安装j

将Python应用部署到生产环境的小技巧分享

《将Python应用部署到生产环境的小技巧分享》文章主要讲述了在将Python应用程序部署到生产环境之前,需要进行的准备工作和最佳实践,包括心态调整、代码审查、测试覆盖率提升、配置文件优化、日志记录完... 目录部署前夜:从开发到生产的心理准备与检查清单环境搭建:打造稳固的应用运行平台自动化流水线:让部署像

Linux中Curl参数详解实践应用

《Linux中Curl参数详解实践应用》在现代网络开发和运维工作中,curl命令是一个不可或缺的工具,它是一个利用URL语法在命令行下工作的文件传输工具,支持多种协议,如HTTP、HTTPS、FTP等... 目录引言一、基础请求参数1. -X 或 --request2. -d 或 --data3. -H 或

在Ubuntu上部署SpringBoot应用的操作步骤

《在Ubuntu上部署SpringBoot应用的操作步骤》随着云计算和容器化技术的普及,Linux服务器已成为部署Web应用程序的主流平台之一,Java作为一种跨平台的编程语言,具有广泛的应用场景,本... 目录一、部署准备二、安装 Java 环境1. 安装 JDK2. 验证 Java 安装三、安装 mys

Python中构建终端应用界面利器Blessed模块的使用

《Python中构建终端应用界面利器Blessed模块的使用》Blessed库作为一个轻量级且功能强大的解决方案,开始在开发者中赢得口碑,今天,我们就一起来探索一下它是如何让终端UI开发变得轻松而高... 目录一、安装与配置:简单、快速、无障碍二、基本功能:从彩色文本到动态交互1. 显示基本内容2. 创建链

基于Qt开发一个简单的OFD阅读器

《基于Qt开发一个简单的OFD阅读器》这篇文章主要为大家详细介绍了如何使用Qt框架开发一个功能强大且性能优异的OFD阅读器,文中的示例代码讲解详细,有需要的小伙伴可以参考一下... 目录摘要引言一、OFD文件格式解析二、文档结构解析三、页面渲染四、用户交互五、性能优化六、示例代码七、未来发展方向八、结论摘要

SpringCloud集成AlloyDB的示例代码

《SpringCloud集成AlloyDB的示例代码》AlloyDB是GoogleCloud提供的一种高度可扩展、强性能的关系型数据库服务,它兼容PostgreSQL,并提供了更快的查询性能... 目录1.AlloyDBjavascript是什么?AlloyDB 的工作原理2.搭建测试环境3.代码工程1.

Node.js 中 http 模块的深度剖析与实战应用小结

《Node.js中http模块的深度剖析与实战应用小结》本文详细介绍了Node.js中的http模块,从创建HTTP服务器、处理请求与响应,到获取请求参数,每个环节都通过代码示例进行解析,旨在帮... 目录Node.js 中 http 模块的深度剖析与实战应用一、引言二、创建 HTTP 服务器:基石搭建(一