在 Google Maps API 中创建多个样式引用 [英] Making Multiple Style References In Google Maps API

查看:15
本文介绍了在 Google Maps API 中创建多个样式引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何在 R 中的 Google Maps API 的一个 ggmap() 查询中进行多个样式引用.

查询很简单:

库(ggmap)地图 <- get_googlemap("纽约市",缩放 = 12,maptype = "路线图",style = c(feature = "poi.medical",元素 = "几何",颜色 = "红色"))ggmap(地图)

但假设我想让所有公园和医院都变成蓝色.我该怎么做?

我在样式变量中尝试过嵌套串联,但不起作用.此外,如果我提出两个单独的样式参数,则会出现以下错误:

形参style"匹配多个实参

(作为参考,公园是 Google Maps API 中的 poi.park,元素再次是几何",颜色是蓝色".)

在 Google Maps API 参考中,他们声明您可以轻松地将多个 JSON 声明嵌套在一个参数中:

<块引用>

样式规则按您指定的顺序应用.不合并将多个操作合并为一个样式操作.相反,定义每个操作作为样式数组中的一个单独条目.

如何在 R 中执行此操作?

感谢您的任何帮助,如果您有任何问题或需要任何说明,请告诉我!

解决方案

我认为这是糟糕的文档加上 ggmap 中的错误的组合.

说明

如果您查看

<小时>

现在为我的 gooleway 包添加了一个单独的插件,您可以在其中使用 JSON 指定样式,并且地图是交互式的

图书馆(googleway)样式 <- '[{"featureType": "poi.park","elementType": "geometry","stylers": [{"color": "#00FF00"}]},{"featureType":"poi.medical","elementType":"geometry","stylers":[{"color":"#FF00FF"}]}]'map_key <- "you_need_an_api_key"google_map(key = map_key, location = c(40.7128, -74.0059),缩放 = 13,高度 = 800,风格=风格)

I cannot figure out how to make multiple style references in one ggmap() query from the Google Maps API in R.

Making one query is simple:

library(ggmap)

map <- get_googlemap("new york city", 
                     zoom = 12, 
                     maptype = "roadmap", 
                     style = c(feature = "poi.medical", 
                               element = "geometry", 
                               color = "red"))
ggmap(map)

But let's say I want to make all parks blue as well as hospitals red. How would I go about doing that?

I have tried nested concatenation within my style variable, but that doesn't work. Also, if I make two separate style arguments, I get the following error:

formal argument "style" matched by multiple actual arguments

(For reference, parks are poi.park in the Google Maps API, element is again "geometry", and color would be "blue".)

In the Google Maps API reference, they state that you can easily make multiple JSON declarations nested within one argument:

Style rules are applied in the order that you specify. Do not combine multiple operations into a single style operation. Instead, define each operation as a separate entry in the style array.

How can I do this in R?

Thanks for any and all help and please, let me know if you have any questions or need any clarification!

解决方案

I think this is a combination of poor documenation, plus a bug in ggmap.

Explanation

If you look at the example on Google Documentation you see that styles are separated by &style=

&style=feature:road.local%7Celement:geometry%7Ccolor:0x00ff00&style=feature:landscape%7Celement:geometry.fill%7Ccolor:0x000000&style=element:labels%7Cinvert_lightness:true

So in your example, if you wanted your two styles

style1 <- c(feature = "poi.medical", element = "geometry", color = "red")
style2 <- c(feature = "poi.park", element = "geometry", color = "blue")

This woud look something like

&style=feature:poi.medical|element:geometry|color:red&style=feature:poi.park|element:geometry|color:blues

In ?get_googlemap, for the style argument it says

character string to be supplied directly to the api for the style argument or a named vector (see examples)

And in the source code we see that it can also supposedly handle lists. So if we create a list out of our styles we get

style <- list(style1, style2)

Which, when run through the get_googlemap gives the url

map <- get_googlemap("new york city", 
                        zoom = 12, 
                        maptype = "roadmap", 
                        style = style)

...&style=style=c(%22poi.medical%22,%20%22geometry%22,%20%22red%22)&style=c(%22poi.park%22,%20%22geometry%22,%20%22blue%22)&sensor=false

Which is also incorrect.

And similarly for a concatenated vector of styles we get an incorrectly formatted URL

style <- c(style1, style2)

map <- get_googlemap("new york city", 
                        zoom = 12, 
                        maptype = "roadmap", 
                        style = style)

...&style=feature:poi.medical%7Celement:geometry%7Ccolor:red%7Cfeature:poi.park%7Celement:geometry%7Ccolor:blue&sensor=false

Solution

Force it to use a &sytle= value as the first (unnamed) element in the 2nd (and subsequent) style vector, and concatenate them using c(), rather than list()

style1 <- c(feature = "poi.medical", element = "geometry", color = "red")
style2 <- c("&style=", feature = "poi.park", element = "geometry", color = "blue")

style <- c(style1, style2)

map <- get_googlemap("new york city", 
                        zoom = 12, 
                        maptype = "roadmap", 
                        style = style)

plot(map)


And now a separate plug for my gooleway package, where you can specify the style using JSON, and the map is interactive

library(googleway)

style <- '[{"featureType": "poi.park","elementType": "geometry","stylers": [{"color": "#00FF00"}]},{"featureType":"poi.medical","elementType":"geometry","stylers":[{"color":"#FF00FF"}]}]'

map_key <- "you_need_an_api_key"

google_map(key = map_key, location = c(40.7128, -74.0059), 
                     zoom = 13, height = 800, 
                     styles = style)

这篇关于在 Google Maps API 中创建多个样式引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆