This tutorial will show you how to retrieve longitude and latitude by simply providing an address.

You will start by installing RDSTK package from github using devtools. If you don’t have devtools, you will also need to install

devtools::install_github("rtelmore/RDSTK")

Next we will call RDSTK package along with dplyr package

library(RDSTK)
library(dplyr)

We will use street2coordinates function to pull lat and long based on location. The output is a dataframe. We will use Disneyland location as an example.

location = '1313 Disneyland Dr, Anaheim, CA 92802, United States'
street2coordinates(location)
##                                           full.address country_code3 latitude
## 1 1313 Disneyland Dr, Anaheim, CA 92802, United States           USA 33.81806
##    country_name longitude    street_address region confidence street_number
## 1 United States -117.9222 999 Disneyland Dr     CA      0.805           999
##   locality   street_name fips_county country_code
## 1  Anaheim Disneyland Dr       06059           US

Lets now pull lat and long using dplyr:: select function. We will use the Los Angeles Staples Center in this example.

df = street2coordinates('1111 S Figueroa St, Los Angeles, CA 90015, United States')
df = df %>% select(full.address, latitude, longitude)
print(df)
##                                               full.address latitude longitude
## 1 1111 S Figueroa St, Los Angeles, CA 90015, United States 34.04355 -118.2654

We can also call multiple addresses in a single function

location_example = c('1111 S Figueroa St, Los Angeles, CA 90015, United States', 
                     '1313 Disneyland Dr, Anaheim, CA 92802, United States')
geocode  <- do.call(rbind, lapply(location_example, street2coordinates))
geocode  <- geocode %>% select(full.address, longitude, latitude)
label <- c('Staples Center', 'Disneyland')
geocode <- data.frame(geocode, label=label)
print(geocode)
##                                               full.address longitude latitude
## 1 1111 S Figueroa St, Los Angeles, CA 90015, United States -118.2654 34.04355
## 2     1313 Disneyland Dr, Anaheim, CA 92802, United States -117.9222 33.81806
##            label
## 1 Staples Center
## 2     Disneyland
library(leaflet)

m <- leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addMarkers(lng= geocode$longitude, lat=geocode$latitude, popup=geocode$label)
m