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

相关文章

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

中文分词jieba库的使用与实景应用(一)

知识星球:https://articles.zsxq.com/id_fxvgc803qmr2.html 目录 一.定义: 精确模式(默认模式): 全模式: 搜索引擎模式: paddle 模式(基于深度学习的分词模式): 二 自定义词典 三.文本解析   调整词出现的频率 四. 关键词提取 A. 基于TF-IDF算法的关键词提取 B. 基于TextRank算法的关键词提取

水位雨量在线监测系统概述及应用介绍

在当今社会,随着科技的飞速发展,各种智能监测系统已成为保障公共安全、促进资源管理和环境保护的重要工具。其中,水位雨量在线监测系统作为自然灾害预警、水资源管理及水利工程运行的关键技术,其重要性不言而喻。 一、水位雨量在线监测系统的基本原理 水位雨量在线监测系统主要由数据采集单元、数据传输网络、数据处理中心及用户终端四大部分构成,形成了一个完整的闭环系统。 数据采集单元:这是系统的“眼睛”,

无人叉车3d激光slam多房间建图定位异常处理方案-墙体画线地图切分方案

墙体画线地图切分方案 针对问题:墙体两侧特征混淆误匹配,导致建图和定位偏差,表现为过门跳变、外月台走歪等 ·解决思路:预期的根治方案IGICP需要较长时间完成上线,先使用切分地图的工程化方案,即墙体两侧切分为不同地图,在某一侧只使用该侧地图进行定位 方案思路 切分原理:切分地图基于关键帧位置,而非点云。 理论基础:光照是直线的,一帧点云必定只能照射到墙的一侧,无法同时照到两侧实践考虑:关

Hadoop企业开发案例调优场景

需求 (1)需求:从1G数据中,统计每个单词出现次数。服务器3台,每台配置4G内存,4核CPU,4线程。 (2)需求分析: 1G / 128m = 8个MapTask;1个ReduceTask;1个mrAppMaster 平均每个节点运行10个 / 3台 ≈ 3个任务(4    3    3) HDFS参数调优 (1)修改:hadoop-env.sh export HDFS_NAMENOD

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

hdu1394(线段树点更新的应用)

题意:求一个序列经过一定的操作得到的序列的最小逆序数 这题会用到逆序数的一个性质,在0到n-1这些数字组成的乱序排列,将第一个数字A移到最后一位,得到的逆序数为res-a+(n-a-1) 知道上面的知识点后,可以用暴力来解 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#in

嵌入式QT开发:构建高效智能的嵌入式系统

摘要: 本文深入探讨了嵌入式 QT 相关的各个方面。从 QT 框架的基础架构和核心概念出发,详细阐述了其在嵌入式环境中的优势与特点。文中分析了嵌入式 QT 的开发环境搭建过程,包括交叉编译工具链的配置等关键步骤。进一步探讨了嵌入式 QT 的界面设计与开发,涵盖了从基本控件的使用到复杂界面布局的构建。同时也深入研究了信号与槽机制在嵌入式系统中的应用,以及嵌入式 QT 与硬件设备的交互,包括输入输出设

OpenHarmony鸿蒙开发( Beta5.0)无感配网详解

1、简介 无感配网是指在设备联网过程中无需输入热点相关账号信息,即可快速实现设备配网,是一种兼顾高效性、可靠性和安全性的配网方式。 2、配网原理 2.1 通信原理 手机和智能设备之间的信息传递,利用特有的NAN协议实现。利用手机和智能设备之间的WiFi 感知订阅、发布能力,实现了数字管家应用和设备之间的发现。在完成设备间的认证和响应后,即可发送相关配网数据。同时还支持与常规Sof

zoj3820(树的直径的应用)

题意:在一颗树上找两个点,使得所有点到选择与其更近的一个点的距离的最大值最小。 思路:如果是选择一个点的话,那么点就是直径的中点。现在考虑两个点的情况,先求树的直径,再把直径最中间的边去掉,再求剩下的两个子树中直径的中点。 代码如下: #include <stdio.h>#include <string.h>#include <algorithm>#include <map>#