0.1 Carregar o shapefile

unidades_embrapa <- st_read("unidades_embrapa_todas.shp", options = "ENCODING=latin1") 
## options:        ENCODING=latin1 
## Reading layer `unidades_embrapa_todas' from data source 
##   `/Users/dani/Desktop/Projetos R/leaflet/unidades_embrapa_todas.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 46 features and 8 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -67.8061 ymin: -31.77194 xmax: -35.88111 ymax: 2.819722
## Geodetic CRS:  SOURCECRS
guess_encoding(file = "unidades_embrapa_todas.shp")
## # A tibble: 1 × 2
##   encoding     confidence
##   <chr>             <dbl>
## 1 windows-1252       0.42
brasil <- st_read("BR_UF_2021.shp")
## Reading layer `BR_UF_2021' from data source 
##   `/Users/dani/Desktop/Projetos R/leaflet/BR_UF_2021.shp' using driver `ESRI Shapefile'
## Simple feature collection with 27 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -73.99045 ymin: -33.75118 xmax: -28.84764 ymax: 5.271841
## Geodetic CRS:  SIRGAS 2000

0.2 Extrai as coordenadas em colunas separadas

XXXX:

1 criando mapa

mapa <- leaflet() %>% 
  addTiles() %>% 
  addMarkers(lat = unidades_embrapa2$latitude, lng = unidades_embrapa2$longitude)
mapa

2 Alterando ícone de localização

icon_star <- makeAwesomeIcon(icon = "home", library = "fa", markerColor = "green")

mapa <- leaflet(unidades_embrapa2) %>% 
  addTiles() %>% 
  addAwesomeMarkers(lat = ~latitude, lng = ~longitude, icon = icon_star, popup = ~paste0(sep=" ", 
                                                                                         "<b>Unidade: </b>", Unidade, "<br>",                                                                                      "<b>Sigla: </b>", Sigla))
mapa

3 Adicionar camada de informações ao passar o mouse

mapa <- leaflet(unidades_embrapa2) %>% 
  addProviderTiles(providers$CartoDB.Positron) %>% 
  addAwesomeMarkers(lat = ~latitude, lng = ~longitude, icon = icon_star, popup = ~paste0(sep=" ", 
                                                                                         "<b>Unidade: </b>", Unidade, "<br>", 
                                                                                         "<b>Sigla: </b>", Sigla), 
                    clusterOptions = markerClusterOptions()) %>% 
  addPolygons(data = brasil, weight = 1, color = "#666666", fillOpacity = 0.3, fillColor = "#CCCCCC", smoothFactor = 0.5,
              highlightOptions = highlightOptions(color = "white", weight = 2, bringToFront = TRUE),
              labelOptions = labelOptions(noHide = TRUE, direction = "auto"))
## Warning: sf layer has inconsistent datum (+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs).
## Need '+proj=longlat +datum=WGS84'
mapa