在 Google 地图 API 中引用多个样式
Making Multiple Style References In Google Maps API
我不知道如何在 R 中的 Google 地图 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 地图 API 中是 poi.park
,元素又是 "geometry",颜色是 "blue"。)
在 Google 地图 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)
现在我的 gooleway 包有一个单独的插件,您可以在其中使用 JSON 指定样式,并且地图是交互式的
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 地图 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 地图 API 中是 poi.park
,元素又是 "geometry",颜色是 "blue"。)
在 Google 地图 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)
现在我的 gooleway 包有一个单独的插件,您可以在其中使用 JSON 指定样式,并且地图是交互式的
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)