GGMap – a map background from Google Maps

Today we’ll take a look at the ggmap library, which allows us to retrieve a map background from Google Maps to present our data there (but more on that in the next posts). First, let’s install the ggmap package and activate it:

The basic function in this package is get_map, which allows us to download a map fragment from Google Maps. Interestingly, we can select it by the place name specified in the location attribute:

To display the map, we can use the ggmap function or a regular plot. The former additionally inserts coordinates into the map frame:

The result is a map printout with the center at the specified position:

Unfortunately, the zoom of the map is so small that the location we specify is barely visible.We can change the zoom by setting the value of the zoom attribute, which defaults to 10 (city scale). The zoom value can be set from 3 (continent scale) to 21 (single building scale). In our example, we set the value to 18 and display the map:

As a result, we get a printout of the map with the selected object visible in the center:

W mapach Google mapy możliwość przełączania różnych typów podkładów, np. mapowy, satelita lub hybrydowy. W get_map możemy zdefiniować, który z dostępnych podkładów chcemy pobrać poprzez atrybut maptype. Domyślnie ma on wartość “roadmap”. Zmiana wartości na “satellite” pobierze fragment mapy z podkładem ortofotomapy:

In the Google Maps it is possible to switch different types of underlay, e.g. map, satellite or hybrid. In get_map, we can specify which of the available underlays we want to retrieve via the maptype attribute. By default, it has the value “roadmap”. If you change the value to “satellite”, the map fragment with the orthophoto underlay will be retrieved:

If you change the value to “hybrid”, the map fragment with hybrid underlay (roadmap + satellite) will be retrieved:

The attribute “location” defined as the name of the location based on the search for Google Maps retrieves its geolocation. We can also provide the location of the object directly using a vector of geographic coordinates, which is useful for more precise localization of an object that may not always be in the Google database:

For large objects, such as a municipality, the location is best defined as a range in the form of a vector with four elements.

The get_map function has many other attributes that we can use to change the appearance of the downloaded primes. For more sophisticated users, we refer to the documentation of this tool:

Leave a Reply

Your email address will not be published.

Translate using Google Translate»
Social media & sharing icons powered by UltimatelySocial

Podoba Ci się nasza strona? Odwiedź nasz profil