Clustered circle markers are added in the map with labels and pop-ups showing facility name, facility phone number and vendor name.
Different circle colours indicate different groups (parent organizations), as presented in the legend.
Interactive layer display is added so that differnt styles of map can be chosen.
Measure add-on is created to measure distance and area.
Two easybuttons are attached: one sets the map zoom level to 1, and the other attempts to locate the user.
setwd("~/")
MFT <- read.csv("MFT.csv", stringsAsFactors = FALSE)
MFT_tidy <- MFT %>%
tbl_df %>%
distinct(Sending_Facility_ID, .keep_all = TRUE) %>%
select(Parent_Organization, Facility_Name, Facility_Street_Address, Facility_City,
Facility_Zip, Facility_County, Facility_Phone, Vendor_Name) %>%
mutate( ) %>%
print
geocodeAdddress <- function(address) {
require(RJSONIO)
url <- "http://maps.google.com/maps/api/geocode/json?address="
url <- URLencode(paste(url, address, "&sensor=false", sep = ""))
x <- fromJSON(url, simplify = FALSE)
if (x$status == "OK") {
out <- c(x$results[[1]]$geometry$location$lng,
x$results[[1]]$geometry$location$lat)
} else {
out <- NA
}
Sys.sleep(0.2) # API only allows 5 requests per second
out
}
address <- paste(MFT_tidy$Facility_Street_Address, MFT_tidy$Facility_City,
"AZ", MFT_tidy$Facility_Zip, sep = ", ")
address_coordinate <- sapply(address, geocodeAdddress)
MFT_tidy$longitude <- address_coordinate[1,]
MFT_tidy$latitude <- address_coordinate[2,]
pal <- colorFactor(palette = "Set1", domain = MFT_tidy$Parent_Organization)
popup_text <- paste(MFT_tidy$Facility_Name, "<br>",
"Facility_Phone:", MFT_tidy$Facility_Phone, "<br>",
"Vendor_Name:", MFT_tidy$Vendor_Name)
layer_groups <- c("OSM (default)", "Toner", "Positron", "NatGeoWorldMap")
leaflet(MFT_tidy, width = "100%") %>%
addTiles(group = "OSM (default)") %>%
addProviderTiles(providers$Stamen.Toner, group = "Toner") %>%
addProviderTiles(providers$CartoDB.Positron, group = "Positron") %>%
addProviderTiles(providers$OpenTopoMap, group = "OpenTopoMap") %>%
addProviderTiles(providers$Esri.NatGeoWorldMap, group = "NatGeoWorldMap") %>%
addCircleMarkers(lng = ~longitude, lat = ~latitude,
color = ~pal(Parent_Organization),
stroke = FALSE, fillOpacity = 1,
label = ~Facility_Name,
popup = popup_text,
clusterOptions = markerClusterOptions() ) %>%
addLegend(position = "topright", pal = pal,
values = ~Parent_Organization,
labels = ~Parent_Organization,
title = "Parent Organization",
opacity = 1) %>%
addLayersControl(baseGroups = c("OSM (default)", "Toner", "Positron",
"OpenTopoMap", "NatGeoWorldMap"),
position = "bottomright") %>%
addMeasure(
position = "bottomleft",
primaryLengthUnit = "meters",
primaryAreaUnit = "sqmeters",
activeColor = "#3D535D",
completedColor = "#7D4479") %>%
addEasyButton(easyButton(
icon = "fa-globe", title = "Zoom to Original Level",
onClick = JS("function(btn, map){ map.setZoom(6); }"))) %>%
addEasyButton(easyButton(
icon = "fa-crosshairs", title = "Locate Me",
onClick = JS("function(btn, map){ map.locate({setView: true}); }")))