12

I am using R package ggmap.

?get_map says:

location: an address, longitude/latitude pair (in that order), or left/bottom/right/top bounding box

My code:

library(ggmap)
library(mapproj)

lat_bottom = 52.33  # bottom latitude of Berlin
lat_top    = 52.5   # top latitude of Berlin
lon_left   = 13.0   # left longitude of Berlin
lon_rigth  = 13.95  # right longitude of Berlin

mymap <- get_map(location = c(lon_left,lat_bottom,lon_rigth,lat_top),
source="google")
ggmap(mymap)

Why is it giving me a warning:

Warning: bounding box given to google - spatial extent only approximate. converting bounding box to center/zoom specification. (experimental)

Does it mean that there is no way for me to create a map with these exact corners?

Based on the advice below I tried this:

lat_bottom = 52.33  # bottom latitude of Berlin
lat_top    = 52.68   # top latitude of Berlin
lon_left   = 13.08   # left longitude of Berlin
lon_rigth  = 13.77  # right longitude of Berlin

mylon = c(lon_left,lon_rigth)
mylat = c(lat_bottom,lat_top)

mymap <- get_map(location = c(lon = mean(mylon), lat = mean(mylat)),
               maptype = "roadmap", source = "google", zoom=11) # using zoom
ggmap(mymap)
foo<-ggmap(mymap)+
  scale_x_continuous(limits = c(lon_left,lon_right), expand = c(0, 0)) +
  scale_y_continuous(limits = c(lat_bottom,lat_top), expand = c(0, 0))
foo

It looks OK. But when I take other coordinates (those closer to each other), for example, like those below - then the map looks weird - it kinda shifts to the left on the gray background...

lat_bottom = 52.35  # new bottom
lat_top    = 52.50  # new top
lon_left   = 13.2   # new left
lon_rigth  = 13.5   # new right
Jaap
  • 81,064
  • 34
  • 182
  • 193
user2323534
  • 585
  • 1
  • 6
  • 18
  • 1
    You can get a map with a certain zoom. If you use `ggplot2`, you can trim the map using `scale_x_continous` and `scale_y_continous`. Have a look of this [link](http://stackoverflow.com/questions/25636897/get-map-with-specified-boundary-coordinates/25639124#25639124). This may be what you are after. – jazzurro Feb 27 '15 at 14:03
  • Just to clarify my objective: I am trying to avoid using "zoom". I don't know in advance (when I write the code) what area of the map I want to see. Sometimes it's larger, sometimes it's smaller. This is why I have to grab the 4 corners from the data and then try to draw a map with those corners. – user2323534 Feb 27 '15 at 14:30

1 Answers1

12

If you want to work with boundaries, it is better to use OpenStreetMap than GoogleMaps. Setting boundaries for GoogleMaps in ggmap doesn't work. Instead it will make an estimate of the centerpoint.

You can specify the source by including source = "osm" in your get_map call.

With:

mymap <- get_map(location = c(13.2,52.35,13.5,52.50), source = "osm")

ggmap(mymap)

you get:

enter image description here

Jaap
  • 81,064
  • 34
  • 182
  • 193