dkahle / ggmap

A package for plotting maps in R with ggplot2
766 stars 231 forks source link

Auto calculation of zoom level does not always cover lat/lon range #358

Open tobadia opened 8 months ago

tobadia commented 8 months ago

Hi,

Upon toying around with ggmap, I realized in some instances the zoom level that is calculated is erroneous so that the returned map does not cover the complete range of latitude and longitude when it is provided as a bounding box. This was tested on Google Maps with "hybrid" maps but the behavior should be identical for all map providers. The blame is tu put here, where the max should rather be min: https://github.com/dkahle/ggmap/blob/4cdb1fb9b1c2c800f87acbc63dbc5896347f27be/R/get_map.R#L189

Replicable exemple (you'll need your own Google API key)

# Global variables, API key...
MAP_PROVIDER                  <- "google"
GOOGLE_API_KEY              <- ""

if (MAP_PROVIDER == "google") {
  register_google(key = GOOGLE_API_KEY)
}

# Example failing
gps_range <- c("left" = 46.00803, 
"bottom" = -19.62127, 
"right" = 46.11314, 
"top" = -19.35588)

# Get and plot map
plot_map <- get_map(location = gps_range, 
              source   = MAP_PROVIDER, 
              maptype  = MAP_TYPE)

With that example, zoomlon is calculated at 11 and zoomlat at 13. The map that is fetched at a max of these two (13) has coordinates ranging latitude -19.540 through -19.440, and longitude 46.000 through 46.125. The latitude does not cover the expected extent.

Changing the max to min solves the issue.

Thanks for considering this !