uber

Have you ever noticed how Uber and other few popular location-based applications have a different style of Google Maps? Some times it looks even better than the default Google Map Style. Today we’ll see how to implement Google map styles in our Android Application.

您是否曾经注意到Uber和其他少数几个流行的基于位置的应用程序具有不同的Google Maps风格? 有时它看起来甚至比默认的Google Map Style好。 今天,我们将看到如何在我们的Android应用程序中实现Google地图样式。

总览 (Overview)

We have discussed and implemented Google Maps in quite a few tutorials until now. Here’s a list of them:

到目前为止,我们已经在许多教程中讨论和实现了Google Maps。 以下是它们的列表:

  • Android Google Maps Example TutorialAndroid Google Maps示例教程
  • Android Google Maps API IntegrationAndroid Google Maps API集成
  • Android Google Maps Current Location, Night Mode FeaturesAndroid Google Maps当前位置,夜间模式功能
  • Android Google Map – Drawing Route Between two pointsAndroid Google Map –两点之间的绘图路线
  • Android Google Map Street View ExampleAndroid Google Map Street View示例
  • Google Static Maps AndroidGoogle静态地图Android

Android Studio provides a default template for Google Maps. Let’s use that.

Android Studio提供了Google Maps的默认模板。 让我们使用它。

As covered in the previous tutorials, you need to add the API key to use Google Maps from the Google Cloud Console.

如之前的教程所述,您需要添加API密钥才能从Google Cloud Console使用Google Maps。

To set Map styles we simply do the following on the instance of Google Map.

要设置地图样式,我们只需对Google Map实例执行以下操作。

mMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(this, "jsonPathGoesHere");

项目结构 (Project Structure)

Android Google Maps Style Project

Android Google Maps Style Project

The map styles are present in the raw folder JSON files. You can customize them or explore the various map styles from the web.

原始文件夹JSON文件中提供了地图样式。 您可以自定义它们或从Web浏览各种地图样式。

码 (Code)

The code for the activity_maps.xml is given below:

下面给出了activity_maps.xml的代码:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><fragment xmlns:tools="https://schemas.android.com/tools"android:id="@+id/map"android:name="com.google.android.gms.maps.SupportMapFragment"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MapsActivity" /><Buttonandroid:id="@+id/btnChangeStyle"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="bottom|center"android:text="CHANGE MAP STYLE"/></FrameLayout>

SupportMapFragment is used to showcase the map in the layout of our activity.

SupportMapFragment用于在我们的活动布局中展示地图。

The code for the MapsActivity.java class is given below:

下面给出了MapsActivity.java类的代码:

package com.journaldev.androidgooglemapsstyling;import android.content.res.Resources;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MapStyleOptions;
import com.google.android.gms.maps.model.MarkerOptions;public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {private GoogleMap mMap;private Button button;int[] rawArray = {R.raw.assasin_creed, R.raw.uber_style, R.raw.game_style, R.raw.vintage_style};int currentIndex = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_maps);button = findViewById(R.id.btnChangeStyle);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {setMapStyle();}});SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);mapFragment.getMapAsync(this);}@Overridepublic void onMapReady(GoogleMap googleMap) {mMap = googleMap;// Add a marker in Sydney and move the cameraLatLng sydney = new LatLng(-34, 151);mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));setMapStyle();}private void setMapStyle() {if (currentIndex == rawArray.length && rawArray.length != 0) {currentIndex = 0;}try {mMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(this, rawArray[currentIndex++]));} catch (Resources.NotFoundException e) {Log.e("MapsActivity", "Cannot find style.", e);}}}

Once the map is ready, the onMapReady gets triggered. Inside this, we set a dummy location marker. The setMapStyle is used to toggle through the various map styles stored in the array.

地图准备好后,onMapReady将被触发。 在其中,我们设置了一个虚拟位置标记。 setMapStyle用于切换存储在数组中的各种地图样式。

Let’s look at the output of the above application in action:

让我们看一下上面应用程序的输出:

Android Google Maps Styled Output

Android Google Maps样式输出

That brings an end to this quick tutorial on styling Google Maps in Android.

这结束了有关在Android中样式Google Maps的快速教程。

You can download the full source code from below or browse through it from our Github Repository.

您可以从下面下载完整的源代码,也可以从我们的Github存储库浏览它。

AndroidGoogleMapsStylingAndroidGoogleMapsStyling
Github RepositoryGithub存储库

翻译自: https://www.journaldev.com/31323/android-google-map-styles-like-uber

uber

uber_像Uber这样的Android Google地图样式相关推荐

  1. Android Google地图接入(一)

    最近项目中需要接入Google地图,遇到很多问题,现在记录下来,供以后参考. 1. 登录Google API Console,注册Android API密钥(需要翻墙 ~_~) 得到API KEY: ...

  2. Android Google地图接入(二)

    前面完成了地图的显示,现在想要获取位置数据,需要使用到另一个API:Google Play services Location API. 接收位置更新基本流程: 1. 连接LocationServic ...

  3. 《ArcGIS Runtime SDK for Android开发笔记》——(13)、图层扩展方式加载Google地图...

    1.前言 http://mt2.google.cn/vt/lyrs=m@225000000&hl=zh-CN&gl=cn&x=420&y=193&z=9& ...

  4. 通过Android上的意图启动Google地图路线

    本文翻译自:Launching Google Maps Directions via an intent on Android My app needs to show Google Maps dir ...

  5. android google定位和地图

    首先在google地图这需要个 <com.google.android.maps.MapView android:id="@+id/mv"     android:click ...

  6. Android Google Map实例 - 在地图和卫星图之间切换(Android mapview)

    之前讲述的例子中显示的 为地图模式,如何你想使用类似google earth的卫星图模式显示,如何操作? 在android上将变得非常简单: 增加两个Button按钮和两个对应的Button.OnCl ...

  7. Android的Google地图开发

    编写这个程序之前需要有一个Google 地图 扩展库在SDK中,一般来说,android 1.5中就已经有了.如果没有的话可以在这里下载: http://code.google.com/android ...

  8. 项目记录——为沙特客户Android开发Google地图应用

    最近在给沙特客户所做的应用中需要用到地图,所以原来常用的百度地图就不能使用了,只好换成了Google地图.结果在开发过程中遇到了很多坑,折腾了不少时间终于搞定.这里特记录下之中的几个要点,让同学们有个 ...

  9. android开发google地图

    最近学习android的开发,在学习google地图开发的时候遇见一些问题,在此特地记录一下开发学习的步骤,也希望能帮助和我一样遇见问题的朋友.因为市面上的很多学习视频和博客 文章对这个的介绍很多都是 ...

最新文章

  1. mysql执行计划中性能最差的是_面试中:mysql性能调优-执行计划explain
  2. Java并发编程实战笔记2:对象的组合
  3. 分配委托、匿名委托、委托
  4. c# 调用restful json_微服务调用为啥用RPC框架,http不更简单吗?
  5. 数据库事务处理差异:可提交读和重复读区别
  6. 随机数的扩展--等概率随机函数的实现
  7. matlab抖g是什么,MATLAB中dither抖动函数的用法
  8. Python Cookbook手记I
  9. 基于TCP协议的Socket通信,实现文件断点续传
  10. Restful风格实现页面之间跳转PageController
  11. springboot添加拦截器 监听等设置
  12. 【渝粤教育】国家开放大学2018年秋季 0109-22T公司财务 参考试题
  13. 超详细 Spring @RequestMapping 注解使用技巧
  14. 如何用C++做一个简单的QQ整人程序
  15. 搜狗微信文章采集php,搜狗微信采集 —— python爬虫系列一
  16. PDF格式转JPG格式怎么转?掌握方法其实很简单
  17. 新高考改革之下,有孚网络助力教育信息化进程
  18. C++primer plus第六版课后编程题答案14.3(仅供参考)
  19. sci四区大水刊 计算机,sci四区大水刊_sci四区免费大水刊_sci四区什么水平
  20. java lstm pb_在Tensorflow Serving上部署基于LSTM的文本分类模型

热门文章

  1. c#动态编译并动态生成dll
  2. 生成XML文件的步骤 解析XML文件
  3. [转载] python字符串查找的四种方法
  4. [转载] Python里面numpy库中zeros()的一些问题
  5. [转载] python3.6版本生成的python脚本如何打包为可独立执行的exe程序
  6. Substance PBR Guide
  7. 2019牛客多校 Round2
  8. 面向对象及os模块、socket模块
  9. System.out.printf()的使用方法
  10. input标签在谷歌浏览器记住密码下的一个自动填充BUG