在Google Maps API中制作多个样式参考 [英] Making Multiple Style References In Google Maps API

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

问题描述

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



创建一个查询很简单:

  library(ggmap)

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

但是,让我们说我想让所有的公园都是蓝色的,以及医院是红色的。我怎么去做这件事?



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

 通过多个实际参数匹配的形式参数style 

(仅供参考,公园 poi.park 在Google Maps API中,元素又是几何图形,颜色将为蓝色。)



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


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

如何在R中执行此操作?



感谢您的任何和所有帮助,请让我知道您是否有任何疑问或需要澄清!

解决方案

我认为这是一个糟糕的文档组合,再加上ggmap中的一个bug。

解释



如果您查看






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

  library(googleway)

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

map_key< - you_need_an_api_key

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


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天全站免登陆