r - 在 Google Maps API 中制作多个样式引用

标签 r google-maps google-maps-api-3 ggmap

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

查询很简单:

library(ggmap)

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

但是,假设我想让所有公园都变成蓝色,而医院也变成红色。我该怎么做呢?

我在我的样式变量中尝试了嵌套连接,但这不起作用。此外,如果我制作两个单独的样式参数,我会收到以下错误:

formal argument "style" matched by multiple actual arguments

(作为引用,公园是 Google Maps API 中的 poi.park,元素还是“几何”,颜色是“蓝色”。)

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

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.

如何在 R 中做到这一点?

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

最佳答案

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

说明

如果您查看 Google Documentation 上的示例您会看到样式由 &style=

分隔

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

所以在你的例子中,如果你想要你的两种风格

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

这看起来像

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

?get_googlemap 中,对于它所说的 style 参数

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

source code我们看到它也可以处理列表。所以如果我们从我们的样式中创建一个列表,我们会得到

style <- list(style1, style2)

当通过 get_googlemap 运行时会给出 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

这也是不正确的。

同样,对于串联的样式向量,我们会得到格式不正确的 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

解决方案

强制它使用 &sytle= 值作为第二个(和后续)样式向量中的第一个(未命名)元素,并使用 c() 连接它们,而不是 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)

enter image description here


现在我的 gooleway 包有一个单独的插件,您可以在其中使用 JSON 指定样式,并且 map 是交互式的

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)

关于r - 在 Google Maps API 中制作多个样式引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41049782/

相关文章:

R dplyr : Use the function as a string in one column on the next column

javascript - 谷歌地图 API 在哪里加载?

php - 使用 Google Maps V3 API 会导致页面刷新很多次!

r - 在接近度上匹配两个 R 数据帧

R:使用 mutate 和 case_when 将变量中的分数相加

javascript - Google map - 更改图层图标大小

javascript - 无法在街景中触发事件

javascript - 在 Google map 信息窗口内显示动态内容

javascript - 如何在 Google map API 中仅显示公交路线[火车]?

r - 在 ggplot2 中创建范围(走廊)图