Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

r - Making Multiple Style References In Google Maps API

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

enter image description here


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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...