Googleway provides access to Google Maps APIs through specified functions, and the ability to plot a Google Map overlayed with markers, circles, rectangles, shapes (polygons), lines (polylines) and heatmaps. You can also overlay traffic information, transit and cycling routes.
The API functions are
google_directions()
google_distance()
google_elevation()
google_geocode()
google_reverse_geocode()
google_places()
google_place_details()
google_timezone()
google_snapToRoads()
and google_nearestRoads()
Plotting a Google Map uses the JavaScript API, and the functions used to create a map and overlays are
google_map()
add_markers()
add_heatmap()
add_circles()
add_polygons()
add_polylines()
add_rectangles()
add_bicycling()
add_traffic()
add_transit()
Downloading a static streetview map
google_streetview()
Finally, the package includes the helper functions, encode_pl()
and decode_pl()
for encoding and decoding polylines.
To use this package you need a valid API KEY (follow instructions here to get a key) for the API you wish to use. Note that the same API key can be used for all the functions, but you need to register it with each API first.
Google Maps allows users to find directions between locations.
The Google Maps Directions API is a service available to developers that calculates directions between locations.
Searching Google Maps for directions from Melbourne to Sydney generates the route:
Melbourne to Sydney
The same query using the developers API generates the data in JSON
{
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"place_id" : "ChIJ90260rVG1moRkM2MIXVWBAQ",
"types" : [ "colloquial_area", "locality", "political" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJP3Sa8ziYEmsRUKgyFmh9AQM",
"types" : [ "colloquial_area", "locality", "political" ]
}
],
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : -33.8660005,
"lng" : 151.2176931
},
"southwest" : {
"lat" : -37.8136598,
"lng" : 144.8875036
}
},
"copyrights" : "Map data ©2017 Google",
"legs" : [
{
"distance" : {
"text" : "878 km",
"value" : 878208
},
"duration" : {
"text" : "8 hours 44 mins",
"value" : 31447
},
"end_address" : "Sydney NSW, Australia",
"end_location" : {
"lat" : -33.8689894,
"lng" : 151.2091978
},
"start_address" : "Melbourne VIC, Australia",
"start_location" : {
"lat" : -37.8136598,
"lng" : 144.9629147
},
... etc
This result can be returned in R using the google_directions()
function. By default the result will be coerced to the simplest R
structure possible using jsonlite::fromJSON()
. If you want the result in JSON set simplify = FALSE
.
library(googleway)
## you will need a valid Google Directions Developers API key
df <- google_directions(origin = "Melbourne, Australia",
destination = "Sydney, Australia",
key = key,
mode = "driving",
simplify = TRUE)
The data used to draw the route on the map is the overview_polyline. This string represents a sequence of lat/lon pairs, encoded using a lossy compression algorithm (https://developers.google.com/maps/documentation/utilities/polylinealgorithm) that allows you to store the series of coordinates as a single string.
pl <- df$routes$overview_polyline$points
pl
# [1] "rqxeF_cxsZgr@xmCekBhMunGnWc_Ank@vBpyCqjAfbAqmBjXydAe{AoF{oEgTqjGur@ch@qfAhUuiCww@}kEtOepAtdD{dDf~BsgIuj@}tHi{C{bGg{@{rGsmG_bDbW{wCuTyiBajBytF_oAyaI}K}bEkqA{jDg^epJmbB{gC}v@i~D`@gkGmJ_kEojD_O{`FqvCetE}bGgbDm_BqpD}pEqdGiaBo{FglEg_Su~CegHw`Cm`Hv[mxFwaAisAklCuUgzAqmCalJajLqfDedHgyC_yHibCizK~Xo_DuqAojDshAeaEpg@g`Dy|DgtNswBcgDiaAgEqgBozB{jEejQ}p@ckIc~HmvFkgAsfGmjCcaJwwD}~AycCrx@skCwUqwN{yKygH}nF_qAgyOep@slIehDcmDieDkoEiuCg|LrKo~Eb}Bw{Ef^klG_AgdFqvAaxBgoDeqBwoDypEeiFkjBa|Ks}@gr@c}IkE_qEqo@syCgG{iEazAmeBmeCqvA}rCq_AixEemHszB_SisB}mEgeEenCqeDab@iwAmZg^guB}cCk_F_iAmkGsu@abDsoBylBk`Bm_CsfD{jFgrAerB{gDkw@{|EacB_jDmmAsjC{yBsyFaqFqfEi_Ei~C{yAmwFt{B{fBwKql@onBmtCq`IomFmdGueD_kDssAwsCyqDkx@e\\kwEyUstC}uAe|Ac|BakGpGkfGuc@qnDguBatBot@}kD_pBmmCkdAgkB}jBaIyoC}xAexHka@cz@ahCcfCayBqvBgtBsuDxb@yiDe{Ikt@c{DwhBydEynDojCapAq}AuAksBxPk{EgPgkJ{gA}tGsJezKbcAcdK__@uuBn_AcuGsjDwvC_|AwbE}~@wnErZ{nGr_@stEjbDakFf_@clDmKkwBbpAi_DlgA{lArLukCBukJol@w~DfCcpBwnAghCweA}{EmyAgaEbNybGeV}kCtjAq{EveBwuHlb@gyIg\\gmEhBw{G{dAmpHp_@a|MsnCcuGy~@agIe@e`KkoA}lBspBs^}sAmgIdpAumE{Y_|Oe|CioKouFwuIqnCmlDoHamBiuAgnDqp@yqIkmEqaIozAohAykDymA{uEgiE}fFehBgnCgrGmwCkiLurBkhL{jHcrGs}GkhFwpDezGgjEe_EsoBmm@g}KimLizEgbA{~DwfCwvFmhBuvBy~DsqCicBatC{z@mlCkkDoaDw_BagA}|Bii@kgCpj@}{E}b@cuJxQwkK}j@exF`UanFzM{fFumB}fCirHoTml@CoAh`A"
Having retrieved the polyline, you can decode it into latitude and longitude coordinates using decode_pl()
.
polyline <- "rqxeF_cxsZgr@xmCekBhMunGnWc_Ank@vBpyCqjAfbAqmBjXydAe{AoF{oEgTqjGur@ch@qfAhUuiCww@}kEtOepAtdD{dDf~BsgIuj@}tHi{C{bGg{@{rGsmG_bDbW{wCuTyiBajBytF_oAyaI}K}bEkqA{jDg^epJmbB{gC}v@i~D`@gkGmJ_kEojD_O{`FqvCetE}bGgbDm_BqpD}pEqdGiaBo{FglEg_Su~CegHw`Cm`Hv[mxFwaAisAklCuUgzAqmCalJajLqfDedHgyC_yHibCizK~Xo_DuqAojDshAeaEpg@g`Dy|DgtNswBcgDiaAgEqgBozB{jEejQ}p@ckIc~HmvFkgAsfGmjCcaJwwD}~AycCrx@skCwUqwN{yKygH}nF_qAgyOep@slIehDcmDieDkoEiuCg|LrKo~Eb}Bw{Ef^klG_AgdFqvAaxBgoDeqBwoDypEeiFkjBa|Ks}@gr@c}IkE_qEqo@syCgG{iEazAmeBmeCqvA}rCq_AixEemHszB_SisB}mEgeEenCqeDab@iwAmZg^guB}cCk_F_iAmkGsu@abDsoBylBk`Bm_CsfD{jFgrAerB{gDkw@{|EacB_jDmmAsjC{yBsyFaqFqfEi_Ei~C{yAmwFt{B{fBwKql@onBmtCq`IomFmdGueD_kDssAwsCyqDkx@e\\kwEyUstC}uAe|Ac|BakGpGkfGuc@qnDguBatBot@}kD_pBmmCkdAgkB}jBaIyoC}xAexHka@cz@ahCcfCayBqvBgtBsuDxb@yiDe{Ikt@c{DwhBydEynDojCapAq}AuAksBxPk{EgPgkJ{gA}tGsJezKbcAcdK__@uuBn_AcuGsjDwvC_|AwbE}~@wnErZ{nGr_@stEjbDakFf_@clDmKkwBbpAi_DlgA{lArLukCBukJol@w~DfCcpBwnAghCweA}{EmyAgaEbNybGeV}kCtjAq{EveBwuHlb@gyIg\\gmEhBw{G{dAmpHp_@a|MsnCcuGy~@agIe@e`KkoA}lBspBs^}sAmgIdpAumE{Y_|Oe|CioKouFwuIqnCmlDoHamBiuAgnDqp@yqIkmEqaIozAohAykDymA{uEgiE}fFehBgnCgrGmwCkiLurBkhL{jHcrGs}GkhFwpDezGgjEe_EsoBmm@g}KimLizEgbA{~DwfCwvFmhBuvBy~DsqCicBatC{z@mlCkkDoaDw_BagA}|Bii@kgCpj@}{E}b@cuJxQwkK}j@exF`UanFzM{fFumB}fCirHoTml@CoAh`A"
library(googleway)
df <- decode_pl(polyline)
head(df)
## lat lon
## 1 -37.81418 144.9632
## 2 -37.80598 144.9404
## 3 -37.78867 144.9380
## 4 -37.74520 144.9341
## 5 -37.73494 144.9270
## 6 -37.73554 144.9023
And, of course, to encode a series of lat/lon coordinates you use encode_pl()
encode_pl(lat = df$lat, lon = df$lon)
## [1] "pqxeF}bxsZer@vmCgkBlMunGjWc_Apk@vBpyCqjAfbAqmBjXydAg{AoFyoEgTojGsr@gh@sfAhUuiCuw@}kEtOepAvdD{dDd~BsgIuj@{tHi{C{bGg{@{rGsmGabDbW{wCuTwiBajBytFaoA{aI{K{bEkqA{jDg^gpJkbB{gC_w@g~D`@ikGmJ_kEojD_O}`FqvCctE}bGgbDk_BspD_qEodGgaBo{FilEi_Su~CcgHw`Cm`Hv[mxFwaAisAklCuUgzAsmCalJajLqfDcdHgyCayHibCezK~Xo_DuqAqjDshAeaEpg@g`Dy|DgtNqwBegDkaAeEogBozB{jEejQ_q@ckIc~HkvFkgAufGmjCcaJwwD}~AycCrx@skCwUqwN}yKygH}nF}pAgyOep@qlIghDemDgeDkoEkuCc|LtKq~E`}Bw{Ef^klG}@gdFsvAcxBeoDcqByoDypEciFkjBc|Ku}@gr@a}IkE_qEoo@syCgG}iEczAkeBmeCqvA}rCq_AgxEgmHszB}RksB}mEgeEgnCoeD_b@iwAmZi^guB}cCm_F_iAikGsu@cbDsoB{lBk`Bi_CsfD}jFerAgrB{gDiw@}|EacB_jDkmAsjC}yBsyFaqFofEk_Ei~C{yAowFv{ByfBwKsl@onBktCq`IqmFmdGueDakDssAssCyqDmx@e\\kwEyUstC}uAe|Ac|BakGpGmfGuc@mnDguBctBmt@}kDapBmmCkdAgkB}jBcIyoC}xAexHia@cz@ahCafCayBqvBitBuuDzb@wiDe{Imt@e{DwhBwdEynDqjCapAo}AuAmsBzPi{EiPgkJ{gA}tGsJezKbcAcdK__@wuBn_AauGsjDwvC_|AybE{~@unEpZ{nGt_@stEhbDakFh_@elDoKiwBdpAi_DjgAylArLykCDukJql@u~DfCepBwnAghCweA{{EmyAgaEbN{bGcV{kCrjAq{EveByuHlb@eyIg\\gmEhBw{GydAopHn_@_|MsnCcuGw~@agIg@e`KkoA}lBspBu^}sAmgIfpAsmE}Ya|Oc|CgoKouFwuIsnCmlDmHamBkuAinDop@wqIkmEsaIqzAmhAwkD{mA}uEciE}fFghBenCirGowCgiLsrBmhL}jHerGs}GihFupDezGgjEg_EuoBim@g}KmmLizEebAy~DwfCyvFmhBuvBy~DsqCicBatC{z@klCkkDqaDw_BagA}|Bii@kgCpj@}{E{b@cuJxQwkK}j@exF~TanFzM{fFumB_gCirHmTml@AoAd`A"
The Google Maps Distance API is a service that provides travel distance and time for a matrix of origins and destinations.
Finding the distances between Melbourne Airport, the MCG, a set of coordinates (-37.81659, 144.9841), to Portsea, Melbourne.
df <- google_distance(origins = list(c("Melbourne Airport, Australia"),
c("MCG, Melbourne, Australia"),
c(-37.81659, 144.9841)),
destinations = c("Portsea, Melbourne, Australia"),
key = key)
head(df)
$destination_addresses
[1] "Melbourne Rd, Victoria, Australia"
$origin_addresses
[1] "Melbourne Airport (MEL), Departure Dr, Melbourne Airport VIC 3045, Australia"
[2] "Jolimont Station, Wellington Cres, East Melbourne VIC 3002, Australia"
[3] "176 Wellington Parade, East Melbourne VIC 3002, Australia"
$rows
elements
1 130 km, 129501, 1 hour 38 mins, 5853, 1 hour 36 mins, 5770, OK
2 104 km, 104393, 1 hour 20 mins, 4819, 1 hour 20 mins, 4792, OK
3 104 km, 104350, 1 hour 20 mins, 4814, 1 hour 20 mins, 4788, OK
$status
[1] "OK"
The Google Maps Elevation API provides elevation data for all locations on the surface of the earth, including depth locations on the ocean floor (which return negative values).
Finding the elevation of 20 points between the MCG, Melbourne and the beach at Elwood, Melbourne
google_elevation(df_locations = data.frame(lat = c(-37.81659, -37.88950),
lon = c(144.9841, 144.9841)),
location_type = "path",
samples = 20,
key = key,
simplify = TRUE)
$results
elevation location.lat location.lng resolution
1 20.8899250 -37.81659 144.9841 9.543952
2 7.8955822 -37.82043 144.9841 9.543952
3 8.4334993 -37.82426 144.9841 9.543952
4 5.4820895 -37.82810 144.9841 9.543952
5 33.5920677 -37.83194 144.9841 9.543952
6 30.4819584 -37.83578 144.9841 9.543952
7 15.0097895 -37.83961 144.9841 9.543952
8 10.9842978 -37.84345 144.9841 9.543952
9 13.8762951 -37.84729 144.9841 9.543952
10 13.4834013 -37.85113 144.9841 9.543952
11 13.3473139 -37.85496 144.9841 9.543952
12 24.9176636 -37.85880 144.9841 9.543952
13 16.7720604 -37.86264 144.9841 9.543952
14 5.8330226 -37.86648 144.9841 9.543952
15 10.7889471 -37.87031 144.9841 9.543952
16 6.9589133 -37.87415 144.9841 9.543952
17 3.9915009 -37.87799 144.9841 9.543952
18 5.3637657 -37.88183 144.9841 9.543952
19 7.1594319 -37.88566 144.9841 9.543952
20 0.6697893 -37.88950 144.9841 9.543952
$status
[1] "OK"
The Google Maps Time zone API provides time offset data for locations on the surface of the earth. You request the time zone information for a specific latitude/longitude pair and date. The API returns the name of that time zone, the time offset from UTC, and the daylight savings offset.
Finding the timezone of the MCG in Melbourne
google_timezone(location = c(-37.81659, 144.9841),
timestamp = as.POSIXct("2016-06-05"),
key = key,
simplify = FALSE)
[1] "{"
[2] " \"dstOffset\" : 0,"
[3] " \"rawOffset\" : 36000,"
[4] " \"status\" : \"OK\","
[5] " \"timeZoneId\" : \"Australia/Hobart\","
[6] " \"timeZoneName\" : \"Australian Eastern Standard Time\""
[7] "}"
The Google Maps Geocoding API is a service that provides geocoding and reverse geocoding of addresses.
Finding the location details for Flinders Street Station, Melbourne
## An unbound search for Flinders Street Station returns 6 options.
df <- google_geocode(address = "Flinders Street Station",
key = key,
simplify = TRUE)
df$results$formatted_address
[1] "Flinders Street Railway Station, Flinders St, Melbourne VIC 3000, Australia"
[2] "Flinders St Station, Melbourne VIC 3000, Australia"
[3] "Flinders Street Station, Melbourne VIC 3000, Australia"
[4] "Flinders Street, Rye VIC 3941, Australia"
[5] "Station Rd, Victoria, Australia"
[6] "Station Rd, Albion Park Rail NSW 2527, Australia"
## using a bounded search limits the results
bounds <- list(c(-37.81962,144.9657),
c(-37.81692, 144.9684))
df <- google_geocode(address = "Flinders Street Station",
bounds = bounds,
key = key,
simplify = TRUE)
df$results$formatted_address
[1] "Flinders Street Railway Station, Flinders St, Melbourne VIC 3000, Australia"
[2] "Flinders Street Station, Melbourne VIC 3000, Australia"
[3] "Flinders St Station, Melbourne VIC 3000, Australia"
The Google Maps Reverse Geocoding API is a service that converts geographic coordinates into a human-readable address.
Finding the street address for a set of coordinates, using result_type
and location_type
as bounding parameters:
df <- google_reverse_geocode(location = c(-37.81659, 144.9841),
result_type = c("street_address", "postal_code"),
location_type = "rooftop",
key = key,
simplify = TRUE)
df$results$address_components
[[1]]
long_name short_name types
1 176 176 street_number
2 Wellington Parade Wellington Parade route
3 East Melbourne East Melbourne locality, political
4 Victoria VIC administrative_area_level_1, political
5 Australia AU country, political
6 3002 3002 postal_code
df$results$geometry
location.lat location.lng location_type viewport.northeast.lat viewport.northeast.lng viewport.southwest.lat
1 -37.81608 144.9842 ROOFTOP -37.81473 144.9855 -37.81743
viewport.southwest.lng
1 144.9828
The Google Maps Places API gets data from the same database used by Google Maps and Google+ Local. Places features more than 100 million businesses and points of interest that are updated frequently through owner-verified listings and user-moderated contributions.
There are four types of search you can perform
A Nearby Search lets you search for places within a specified area. You can refine your search request by supplying keywords or specifying the type of place you are searching for.
A Text Search Service is a web service that returns information about a set of places based on a string — for example “pizza in New York” or “shoe stores near Ottawa” or “123 Main Street”. The service responds with a list of places matching the text string and any location bias that has been set.
A Radar search lets you search for places within a specified search radius by keyword, type or name. The Radar search returns more results than a Nearby or Text search, but the results contain fewer fields.
A Place Detail search (using google_place_details()
) can be performed when you have a given place_id
from one of the other three search methods.
For a text search you are required to provide a search_string
For example, here’s a query for “restaurants in Melbourne”
res <- google_places(search_string = "Restaurants in Melbourne, Australia",
key = key)
## View the names of the returned restaurantes, and whether they are open or not
cbind(res$results$name, res$results$opening_hours)
res$results$name open_now weekday_text
1 Vue de monde TRUE NULL
2 ezard FALSE NULL
3 MoVida TRUE NULL
4 Flower Drum Restaurant Melbourne TRUE NULL
5 The Press Club FALSE NULL
6 Maha TRUE NULL
7 Bluestone NA NULL
8 Chin Chin TRUE NULL
9 Taxi Kitchen TRUE NULL
10 Max on Hardware TRUE NULL
11 Attica FALSE NULL
12 Nirankar Restaurant FALSE NULL
13 The Mill TRUE NULL
14 The Left Bank Melbourne TRUE NULL
15 The Colonial Tramcar Restaurant TRUE NULL
16 Rockpool Bar & Grill TRUE NULL
17 Lane Restaurant Cafe & Bar - Novotel Melbourne on Collins TRUE NULL
18 Melba Restaurant TRUE NULL
19 CUMULUS INC. TRUE NULL
20 radii restaurant & bar FALSE NULL
A single query will return 20 results per page. You can view the next 20 results using the next_page_token
that is returned as part of the initial query.
res_next <- google_places(search_string = "Restaurants in Melbourne, Australia",
page_token = res$next_page_token,
key = key)
cbind(res_next$results$name, res_next$results$opening_hours)
res_next$results$name open_now weekday_text
1 Moshi Moshi Japanese Seafood Restaurant TRUE NULL
2 Grill Steak Seafood TRUE NULL
3 Conservatory TRUE NULL
4 Sarti FALSE NULL
5 Tsindos TRUE NULL
6 The Cerberus Beach House TRUE NULL
7 Stalactites Restaurant TRUE NULL
8 Hanabishi Japanese Restaurant FALSE NULL
9 GAZI Restaurant TRUE NULL
10 Om Vegetarian FALSE NULL
11 Shark Fin Inn TRUE NULL
12 Om Vegetarian TRUE NULL
13 The Atlantic Restaurant TRUE NULL
14 Takumi TRUE NULL
15 Pei Modern NA NULL
16 Bamboo House Restaurant TRUE NULL
17 Byblos Bar & Restaurant TRUE NULL
18 Waterfront TRUE NULL
19 No 35 TRUE NULL
20 Bistro Guillaume TRUE NULL
For a nearby search you are required to provide a location
as a pair of latitude/longitude coordinates. You can refine your search by providing a keyword and / or a radius.
res <- google_places(location = c(-37.918, 144.968),
keyword = "Restaurant",
radius = 5000,
key = key)
cbind(res$results$name, res$results$opening_hours)
# res$results$name open_now weekday_text
# 1 Melbourne NA NULL
# 2 Quest Brighton on the Bay TRUE NULL
# 3 Brighton Savoy FALSE NULL
# 4 Caroline Serviced Apartments Brighton TRUE NULL
# 5 The Buckingham Serviced Apartment NA NULL
# 6 Sandringham Hotel TRUE NULL
# 7 Indian Palace Restaurant TRUE NULL
# 8 Elsternwick Park TRUE NULL
# 9 Brown Cow Cafe TRUE NULL
# 10 Bok Choy Chinese Cuisine FALSE NULL
# 11 Marine Hotel TRUE NULL
# 12 Daily Planet TRUE NULL
# 13 Riva St Kilda TRUE NULL
# 14 New Bay Hotel TRUE NULL
# 15 Flight Centre North Brighton TRUE NULL
# 16 Vintage Cellars Brighton TRUE NULL
# 17 Palace Brighton Bay TRUE NULL
# 18 Brighton Toyota TRUE NULL
# 19 Sportsgirl TRUE NULL
# 20 Saint Kilda NA NULL
The Google Roads API provides three functions
The snap to roads function takes up to 100 GPS points collected along a route and returns the points snapped to the most likely roads that were travelled along.
df_path <- read.table(text = "lat lon
-35.27801 149.12958
-35.28032 149.12907
-35.28099 149.12929
-35.28144 149.12984
-35.28194 149.13003
-35.28282 149.12956
-35.28302 149.12881
-35.28473 149.12836
", header = T, stringsAsFactors = F)
res <- google_snapToRoads(df_path = df_path, key = key)
res$snappedPoints
location.latitude location.longitude originalIndex placeId
1 -35.27800 149.1295 0 ChIJr_xl0GdNFmsRsUtUbW7qABM
2 -35.28032 149.1291 1 ChIJOyypT2hNFmsRZBtscGL0htw
3 -35.28101 149.1292 2 ChIJv5r0smlNFmsR5nunau79Fv4
4 -35.28147 149.1298 3 ChIJ601MoWlNFmsR5mvkfPp2ovA
5 -35.28194 149.1300 4 ChIJ601MoWlNFmsR5mvkfPp2ovA
6 -35.28282 149.1296 5 ChIJaUpThGlNFmsRMHWxoH7EOsc
7 -35.28313 149.1289 6 ChIJWSb8ImpNFmsRp_4JAxJFE1A
8 -35.28473 149.1283 7 ChIJtWxAZmpNFmsRlbUCkc6VtnA
df_path$snapped_lat <- res$snappedPoints$location$latitude
df_path$snapped_lon <- res$snappedPoints$location$longitude
df_path$colour <- 'green'
map_key <- "your_api_key"
google_map(key = map_key) %>%
add_markers(data = df_path, lat = 'lat', lon = 'lon') %>%
add_markers(data = df_path, lat = 'snapped_lat', lon = 'snapped_lon', colour = "colour")
The result includes the column originalIndex
. This is a zero-based index that indicates which of the input coordinates has been snapped to the given location.latitude
/location.longitude
coordinates. So in this example, originalIndex
0 is the first row of df_path
, originalIndex
1 is the second row of df_path
, and so on.
The image shows the original coordinates from df_path
in red, and the associated snapped
coordinates in green.
Snap to Roads
The nearest roads function takes up to 100 independent coordinates and returns the closest road segment for each point.
df_points <- read.table(text = "lat lon
60.1707 24.9426
60.1708 24.9424
60.1709 24.9423", header = T)
res <- google_nearestRoads(df_points, key = key)
res$snappedPoints
location.latitude location.longitude originalIndex placeId
1 60.17070 24.94272 0 ChIJNX9BrM0LkkYRIM-cQg265e8
2 60.17081 24.94271 1 ChIJNX9BrM0LkkYRIM-cQg265e8
3 60.17091 24.94270 2 ChIJNX9BrM0LkkYRIM-cQg265e8
df_points$snapped_lat <- res$snappedPoints$location$latitude
df_points$snapped_lon <- res$snappedPoints$location$longitude
df_points$colour <- 'green'
google_map(key = map_key) %>%
add_markers(data = df_points, lat = 'lat', lon = 'lon') %>%
add_markers(data = df_points, lat = 'snapped_lat', lon = 'snapped_lon', colour = "colour")
nearest Roads
A google map can be made using the google_map()
function. Without any data present, or no location
value set, the map will default to Melbourne, Australia.
You can also display traffic, transit (public transport) or bicycle routes using the functions add_traffic()
, add_transit()
and add_bicycling()
respectively.
You can also include a search box in your map by using the argument search_box = TRUE
, which allows you to search the maps just like you would when using Google Maps.
map_key <- "your_api_key"
google_map(key = map_key, search_box = T) %>%
add_traffic()
Melbourne
Markers and circles can be used to show points on the map.
In this example I’m using the tram_stops
data set provided with googleway.
You can specify a column in the data.frame to use to populate a popup info_window
that will be displayed when clicking on a maker. The info window can display any valid HTML, as demonstrated in this Stack Overflow answer.
df <- tram_stops
df$info <- paste0("<b>Stop Name: </b>", df$stop_name)
map_key <- "your_api_key"
google_map(data = df, key = map_key) %>%
add_markers(lat = "stop_lat", lon = "stop_lon", info_window = "info")