谷歌maps菜单语言设置_使用Android版Google Maps构建热图

2024-02-16 12:30

本文主要是介绍谷歌maps菜单语言设置_使用Android版Google Maps构建热图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

谷歌maps菜单语言设置

Heat maps are a popular way to represent data where each entity is associated with some intensity or magnitude. This article serves as a guide to making heat maps for spatial data using Google Maps for Android.

热图是一种流行的表示数据的方式,其中每个实体都与某个强度或大小相关联。 本文是使用Android版Google Maps制作空间数据热图的指南。

Note: This blog expects you to know how to setup Google Maps in Android. If you are not aware of the same, check out the official guide by Google. This article uses Kotlin language for all Android code.

注意:此博客希望您知道如何在Android中设置Google Maps。 如果您不清楚, 请查看Google的官方指南 本文将Kotlin语言用于所有Android代码。

Source code of this project is available on my GitHub.

该项目的源代码 可在我的GitHub上找到

什么是热图? (What is a heat map?)

Image for post
A common example of heat maps in real life are temperature charts.
现实生活中常见的热图示例是温度图。

Heat maps are way of representing data having some magnitude using colors, and are quite popular in showing spatial data.

热图是使用颜色表示某种程度的数据的方式,并且在显示空间数据时非常流行。

了解数据结构 (Understanding the data structure)

In order to show as a heat map, the data is stored as a collection of objects having a latitude, longitude and intensity value. The easiest way to do this is to store them as a JSON array of objects each having latitude, longitude and intensity fields. An example is given below.

为了显示为热图,将数据存储为具有纬度,经度和强度值的对象的集合。 最简单的方法是将它们存储为对象的JSON数组,每个对象都有纬度,经度和强度字段。 下面给出一个例子。

[
{
"intensity": 213.0,
"lat": 14.68365,
"lon": 77.58146
},
{
"intensity": 275.0,
"lat": 13.20588,
"lon": 79.08805
},
{
"intensity": 477.0,
"lat": 16.96423,
"lon": 82.23792
}
]

使用Google Maps构建热图 (Building heatmaps using Google Maps)

For the sake this article, we will be showing a heat map of district-wise population density of India, represented as the number of people per square kilometre. I have already compiled the data as a JSON file, which we will be using later on to load our heatmap data. You can get a copy of the JSON file here.

为了本文方便起见,我们将显示一个印度地区人口密度的热图,以每平方公里的人数表示。 我已经将数据编译为JSON文件,稍后将用于加载热图数据。 您可以在此处获取JSON文件的副本。

While the basic Google Maps SDK does not contain any heat map utilities, Google has provided an extension library, called the Google Maps Utility Library, which adds complex features to your existing Google Maps setup.

尽管基本的Google Maps SDK不包含任何热图实用程序,但Google提供了一个扩展库,称为Google Maps Utility Library,该库向您现有的Google Maps设置中添加了复杂的功能。

添加依赖项 (Add dependencies)

Add the following gradle dependencies in your app module build.gradle. You might already have the Google Maps dependency added while you were setting up Google Maps in your project.

在您的应用程序模块build.gradle中添加以下gradle依赖项。 在项目中设置Google Maps时,可能已经添加了Google Maps依赖项。

implementation 'com.google.maps.android:android-maps-utils:1.0.2'
implementation 'com.google.android.gms:play-services-maps:17.0.0'

从资产加载热图数据 (Load heatmap data from assets)

In real life projects, spatial data is usually fetched from the server in real time. However, to reduce complexity for the sake of this example, we’ll be loading the data from a local JSON file.

在现实生活中的项目中,空间数据通常是从服务器实时获取的。 但是,为了减少复杂性,我们将从本地JSON文件加载数据。

In case you missed it, you can download the JSON file here.

如果您错过了它,可以在 此处 下载JSON文件

First, let’s set up our assets folder. Switch to Project view and in the app > src > main directory create a new directory called assets. Paste the downloaded JSON file in this assets folder.

首先,让我们设置assets文件夹。 切换到“项目”视图,然后在app > src > main目录中创建一个新的目录,称为assets 。 将下载的JSON文件粘贴到此资产文件夹中。

Image for post

With the file in place, we’ll now read the file contents in our Android code. Our JSON file contains an array of objects, each having the following structure:

放置好文件后,我们现在将在Android代码中读取文件内容。 我们的JSON文件包含一个对象数组,每个对象都具有以下结构:

{
"density": 123.4,
"lat": 18.5544,
"lon": 76.3324
}

In MainActivity, we’ll create a function called getJsonDataFromAsset which will read the file and retrieve its contents as a JSON array.

MainActivity ,我们将创建一个名为getJsonDataFromAsset的函数,该函数将读取文件并以JSON数组的形式检索其内容。

private fun getJsonDataFromAsset(fileName: String): JSONArray? {
try {
val jsonString = assets.open(fileName).bufferedReader().use { it.readText() }
return JSONArray(jsonString)
} catch (e: Exception) {
e.printStackTrace()
return null
}
}

显示热图 (Displaying the heatmap)

So far, we have our data as a JSON array. Great! But how do we use it? This is where Google Maps Utility library comes into the picture. The library has components that enable us to easily make heatmaps. One of them is the WeightedLatLng class, which can store latitude, longitude and the weight (intensity).

到目前为止,我们将数据作为JSON数组。 大! 但是我们如何使用它呢? 这是Google Maps Utility库出现的地方。 该库具有使我们能够轻松制作热图的组件。 其中之一是WeightedLatLng类,它可以存储纬度,经度和权重(强度)。

The Google Maps heat map overlay will expect an ArrayList of WeightedLatLng objects, so let’s generate one from our JSON data. We’ll write a function called getHeatMapData which will parse our JSON array and return us an ArrayList of WeightedLatLng objects.

Google Maps热图叠加层将期望一个WeightedLatLng对象的ArrayList ,因此让我们从JSON数据中生成一个。 我们将编写一个名为getHeatMapData的函数,该函数将解析我们的JSON数组并返回一个WeightedLatLng对象的ArrayList

private fun generateHeatMapData(): ArrayList<WeightedLatLng> {
val data = ArrayList<WeightedLatLng>() // call our function which gets json data from our asset file
val jsonData = getJsonDataFromAsset("district_data.json") // ensure null safety with let call
jsonData?.let {
// loop over each json object
for (i in 0 until it.length()) {
// parse each json object
val entry = it.getJSONObject(i)
val lat = entry.getDouble("lat")
val lon = entry.getDouble("lon")
val density = entry.getDouble("density") // optional: remove edge cases like 0 population density values
if (density != 0.0) {
val weightedLatLng = WeightedLatLng(LatLng(lat, lon), density)
data.add(weightedLatLng)
}
}
} return data
}

Now, in the overridden onMapReady function, we can write our code to generate our heat map. Let’s first generate our data by calling the generateHeatMapData function as follows.

现在,在重写的onMapReady函数中,我们可以编写代码以生成我们的热图。 首先,我们通过如下调用generateHeatMapData函数来生成数据。

val data = generateHeatMapData()

After that, we’ll be creating a HeatMapTileProvider object using its builder.

之后,我们将使用其生成器创建HeatMapTileProvider对象。

val heatMapProvider = HeatmapTileProvider.Builder()
.weightedData(data) // load our weighted data
.radius(50) // optional, in pixels, can be anything between 20 and 50
.build()

Finally, we’ll center Google Map over India (i.e. wherever our data is) so that we are able to see the heatmap.

最后,我们将Google Map(印度)放在整个印度的中心(即我们的数据在哪里),以便我们可以查看热图。

val indiaLatLng = LatLng(20.5937, 78.9629)
googleMap?.moveCamera(CameraUpdateFactory.newLatLngZoom(indiaLatLng, 5f))

That’s it! Here’s the entirety of onMapReady function for your reference.

而已! 这是完整的onMapReady函数供您参考。

override fun onMapReady(googleMap: GoogleMap?) {
val data = generateHeatMapData() val heatMapProvider = HeatmapTileProvider.Builder()
.weightedData(data) // load our weighted data
.radius(50) // optional, in pixels, can be anything between 20 and 50
.build() googleMap?.addTileOverlay(TileOverlayOptions().tileProvider(heatMapProvider)) val indiaLatLng = LatLng(20.5937, 78.9629)
googleMap?.moveCamera(CameraUpdateFactory.newLatLngZoom(indiaLatLng, 5f))
}

Let’s run the project and see how our map looks.

让我们运行该项目,看看地图的外观。

Image for post

That worked! But hey, why is the map so empty? Weren’t you expecting the map to light up? After all, India’s population density is one of the highest in the world!

可行! 但是,为什么地图这么空? 您是否不希望地图点亮? 毕竟,印度的人口密度是世界上最高的国家之一!

Well, here’s what happened. In places like Delhi, the population density goes to a whopping 36000 per square kilometer. Now consider a city like Bangalore which has a density of 4300 according to our data — although 4300 is a large value, it is nothing as compared to the density in Delhi. In other words, the relatively large value is shadowed by a gigantic other, and is hence considered a small value by the heat map utility when plotting on the map.

好吧,这就是发生的事情。 在德里这样的地方,人口密度高达每平方公里36000。 现在考虑像班加罗尔这样的城市,根据我们的数据,该城市的密度为4300 —尽管4300是一个很大的值,但与德里的密度相比,这算什么。 换句话说,相对较大的值被巨大的其他阴影遮盖,因此在绘制地图时,热图实用程序将其视为较小的值。

To overcome this, we have the ability to set a maximum intensity for the heat map when creating the heat map tile provider.

为了克服这个问题,我们可以在创建热图图块提供程序时为热图设置最大强度。

val heatMapProvider = HeatmapTileProvider.Builder()
.weightedData(data) // load our weighted data
.radius(50) // optional, in pixels, can be anything between 20 and 50
.maxIntensity(1000.0) // set the maximum intensity
.build()

Here, setting the maximum intensity overrides the global maximum, i.e. the highest intensity value in our data set, and sets it to whatever value we pass. Let’s try running our project again.

在这里,设置最大强度会覆盖全局最大值,即数据集中的最高强度值,并将其设置为我们通过的任何值。 让我们尝试再次运行我们的项目。

Image for post

This seems perfect, doesn’t it? Congratulations, you just built your first heat map on Android!

这看起来很完美,不是吗? 恭喜,您刚刚在Android上建立了第一个热图!

自定义热图 (Customizing the Heatmap)

You can style your heat map according to your requirements.

您可以根据需要设置热图样式。

You can change the display colors by passing a Gradient to the tile provider builder.

您可以通过将“ Gradient传递给图块提供程序构建器来更改显示颜色。

// Create the gradient with whatever start and end colors you wish to use
int[] colors = {Color.GREEN, Color.parseColor("#FF0000")};
float[] startPoints = {0.2f, 1f};
Gradient gradient = new Gradient(colors, startPoints);val heatMapProvider = HeatmapTileProvider.Builder()
.weightedData(data)
.radius(50)
.maxIntensity(1000.0)
.gradient(gradient) // set gradient
.build()

You can also change the opacity of your heat map layer by passing in a value between 0 and 1 to the tile provider builder. This can be particularly helpful if you wish the map in the background to be legible in dark colors.

您还可以通过将0到1之间的值传递给图块提供程序构建器来更改热图图层的不透明度。 如果您希望背景中的地图以深色清晰可见,这将特别有用。

val heatMapProvider = HeatmapTileProvider.Builder()
.weightedData(data)
.opacity(0.5) // set opacity, default is 0.7
.build()

总结思想 (Closing Thoughts)

Heat maps can be particularly useful if you wish to represent spatial data having some intensity associated with it. Hopefully this article proves to be the stepping stone for you if you are looking for a career in cartography in mobile applications. As always, I’ll be glad to answer all your questions in the comments section. Keep coding!

如果要表示具有一定强度的空间数据,热图可能特别有用。 如果您正在寻找移动应用制图中的职业,希望这篇文章对您来说是垫脚石。 与往常一样,我很乐意在评论部分回答您的所有问题。 继续编码!

翻译自: https://medium.com/@rtficial/building-heat-maps-using-google-maps-for-android-364584533157

谷歌maps菜单语言设置


http://www.taodudu.cc/news/show-8461054.html

相关文章:

  • 利用Google进行专题信息检索的方法和技巧
  • 印度 研究生 水平_辅助案例研究我们如何鼓励印度城市居民成为城市农民
  • Android 开发指南以及翻译API等
  • 记《线下活动|程序媛们一起去Google玩耍吧》
  • Google面试官抖出自己的面试题,分步解析,超多干货和建议
  • Google Brain学员计划第一期有哪些前沿研究?
  • 今晚8点开讲 | Transformer新型神经网络在机器翻译中的应用
  • 免费直播 | Transformer新型神经网络在机器翻译中的应用
  • 考研感悟
  • Code Wins
  • 孔乙己(考研版)
  • 一个考研的经历(历练——美丽我的人生)
  • CS考纲颁布后各个注意事项
  • Java后台开发面试实战(九):计算机网络
  • 九、Elasticsearch mapping文件解析(核心)
  • 爬虫 requests——获取网络请求(九)
  • Jsp九大隐式对象四大域对象
  • 技术博客整理
  • 谷歌分析工具是如何毁了市场营销的?
  • 20. 市场营销
  • 中药经方:桂枝汤
  • 国家基本药物学习班 止咳化痰中成药的简易选择
  • 关于数字进位的故事,绘本,儿童
  • 学儿歌听故事android源码,小宝听儿歌学故事app
  • 《人机交互设计》考前速刷
  • 米兔智能故事机吕继伦:踏实做事,爆品自来
  • 基于xr871机器人故事机设计原理和方法 图灵后台
  • 青莲云正式推出儿童智能故事机一站式解决方案
  • Hack the box靶机 Sneaky
  • Day 25 30 Days Of English Family Conversations
  • 这篇关于谷歌maps菜单语言设置_使用Android版Google Maps构建热图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

    相关文章

    C#中checked关键字的使用小结

    《C#中checked关键字的使用小结》本文主要介绍了C#中checked关键字的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录✅ 为什么需要checked? 问题:整数溢出是“静默China编程”的(默认)checked的三种用

    C#中预处理器指令的使用小结

    《C#中预处理器指令的使用小结》本文主要介绍了C#中预处理器指令的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录 第 1 名:#if/#else/#elif/#endif✅用途:条件编译(绝对最常用!) 典型场景: 示例

    JAVA Calendar设置上个月时,日期不存在或错误提示问题及解决

    《JAVACalendar设置上个月时,日期不存在或错误提示问题及解决》在使用Java的Calendar类设置上个月的日期时,如果遇到不存在的日期(如4月31日),默认会自动调整到下个月的相应日期(... 目录Java Calendar设置上个月时,日期不存在或错误提示java进行日期计算时如果出现不存在的

    Java利用Spire.XLS for Java自动化设置Excel的文档属性

    《Java利用Spire.XLSforJava自动化设置Excel的文档属性》一个专业的Excel文件,其文档属性往往能大大提升文件的可管理性和可检索性,下面我们就来看看Java如何使用Spire... 目录Spire.XLS for Java 库介绍与安装Java 设置内置的 Excel 文档属性Java

    Mysql中RelayLog中继日志的使用

    《Mysql中RelayLog中继日志的使用》MySQLRelayLog中继日志是主从复制架构中的核心组件,负责将从主库获取的Binlog事件暂存并应用到从库,本文就来详细的介绍一下RelayLog中... 目录一、什么是 Relay Log(中继日志)二、Relay Log 的工作流程三、Relay Lo

    使用Redis实现会话管理的示例代码

    《使用Redis实现会话管理的示例代码》文章介绍了如何使用Redis实现会话管理,包括会话的创建、读取、更新和删除操作,通过设置会话超时时间并重置,可以确保会话在用户持续活动期间不会过期,此外,展示了... 目录1. 会话管理的基本概念2. 使用Redis实现会话管理2.1 引入依赖2.2 会话管理基本操作

    Springboot请求和响应相关注解及使用场景分析

    《Springboot请求和响应相关注解及使用场景分析》本文介绍了SpringBoot中用于处理HTTP请求和构建HTTP响应的常用注解,包括@RequestMapping、@RequestParam... 目录1. 请求处理注解@RequestMapping@GetMapping, @PostMappin

    springboot3.x使用@NacosValue无法获取配置信息的解决过程

    《springboot3.x使用@NacosValue无法获取配置信息的解决过程》在SpringBoot3.x中升级Nacos依赖后,使用@NacosValue无法动态获取配置,通过引入SpringC... 目录一、python问题描述二、解决方案总结一、问题描述springboot从2android.x

    SpringBoot整合AOP及使用案例实战

    《SpringBoot整合AOP及使用案例实战》本文详细介绍了SpringAOP中的切入点表达式,重点讲解了execution表达式的语法和用法,通过案例实战,展示了AOP的基本使用、结合自定义注解以... 目录一、 引入依赖二、切入点表达式详解三、案例实战1. AOP基本使用2. AOP结合自定义注解3.

    Python中Request的安装以及简单的使用方法图文教程

    《Python中Request的安装以及简单的使用方法图文教程》python里的request库经常被用于进行网络爬虫,想要学习网络爬虫的同学必须得安装request这个第三方库,:本文主要介绍P... 目录1.Requests 安装cmd 窗口安装为pycharm安装在pycharm设置中为项目安装req