University of the West Indies Open Campus Sites.
set.seed(2017-08-16)
library (plyr)
# google mapping library
library(ggmap)
# leaflet library
library(leaflet)
# load base address data
addresses <- read.csv("~/Coursea/LeafLetExamples/uwiOCsites.csv",
stringsAsFactors = FALSE)
# get geocode informaton via the google api
geo_reply = suppressWarnings(
geocode(addresses$Address,
output="all",messaging = FALSE ,override_limit = TRUE))
# function to extract required geocode elements - lat, lng, addr, Country
get_coords <- function(x) {
if(class(x)=="list") {
list("lat" = x$results[[1]]$geometry$location$lat,
"lng" = x$results[[1]]$geometry$location$lng,
"adr" = x$results[[1]]$formatted_address,
subset(ldply(x$results[[1]]$address_components, data.frame),types=="country",select=long_name))
}
}
# extract geocode elements
geo_latlona <- sapply(geo_reply, get_coords)
#convert list to dataframe
df <- ldply (geo_latlona, data.frame)
# The UWI Icon
uwiIcon <- makeIcon(
iconUrl = "https://pbs.twimg.com/profile_images/476414443102818304/va0bNPKC.jpeg",
iconWidth = 61*215/230, iconHeight = 61,
iconAnchorX = 91*215/230/2, iconAnchorY = 35
)
# build the leaflet map map
library(leaflet)
df[,1:2] %>%
leaflet(width = "100%") %>%
addTiles() %>%
addMarkers( popup = df$adr,clusterOptions = markerClusterOptions()) %>%
addMarkers(
icon = uwiIcon, popup = "<a href='http://www.open.uwi.edu/'>UWI Open Campus</a>",
lng=-80.456554, lat=14.078039) %>%
addLabelOnlyMarkers(
lng=-80.456554, lat=14.078039,
label='University of the West Indies, Open Campus Sites',
labelOptions = labelOptions(noHide = T)) %>%
addLabelOnlyMarkers(
lng=-80.456554, lat=11.078039,
label='Map Created on: 2017.8.16',
labelOptions = labelOptions(noHide = T))