Skip to content

Commit

Permalink
feat: add new arguments for addSearchOSM and addReverseSearchOSM,…
Browse files Browse the repository at this point in the history
… normalize longitude for wraped tiles, include examples
  • Loading branch information
trafficonese committed Apr 4, 2024
1 parent df79d4d commit dba1227
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 67 deletions.
13 changes: 10 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
- `addDrawToolbar` offers some new options:
- `handlersOptions` and `toolbarOptions`, with which you can customize the drawing toolbar and the tooltips
- `addDrawToolbar` got a new argument `drag`. When set to `TRUE`, the drawn features will be draggable during editing, utilizing the [Leaflet.Draw.Drag](https://www.npmjs.com/package/leaflet-draw-drag) plugin. Otherwise, this library will not be included. Fix #115
- `searchOSMText` let's you set the OSM search text from R
- `clearSearchOSM` let's you clear the search marker for `addSearchOSM`. Fix #158, Fix #209
- `clearSearchFeatures` let's you clear the search marker
- `searchOSMText` enables setting the OpenStreetMap (OSM) search text directly from R
- `clearSearchOSM` allows clearing the search marker associated with `addSearchOSM`. Fix #158, Fix #209
- `clearSearchFeatures` provides functionality to clear the search marker
- `addReverseSearchOSM` gained new arguments:
- `marker` allows customization of the icon for the found feature
- `showFeatureOptions` enables styling options for the found feature
- `showBoundsOptions` allows customization of the style for the boundary of a found feature
- `showHighlightOptions` enables customization of the hover effect for a found feature

- `addBounceMarkers` behave now like normal Markers (except for clustering)
- **Update Geodesics** functions to show default or custom statistics in Control Info, show (draggable) Markers, use layerManager, enable popup/label/markerOptions/highlightOptions, emit Mouse Events, accept icons.
- `addLatLng` for `addGeodesicPolylines` allows you to append line vertices
Expand All @@ -17,6 +23,7 @@
- Extend BingTiles `imagerySet` and emit warnings for deprecated sets
- Changed default values in `searchOptions` for `url` / `propertyLoc` / `propertyName`
- Draw events trigger correctly using `priority: "event"`. Fix #89
- In `addReverseSearchOSM`, longitudes are now normalized to ensure that the Nomiatim search returns features for wrapped tiles.
- Updated most JavaScript dependencies, changed Webpack Build, removed **source-maps** as package size was an R-CMD-check issue
- Include Tests for R functions
- Use Github Actions (R-CMD-check, pkgdown, test-coverage, NPM builds, Styler, PR-commands)
Expand Down
50 changes: 45 additions & 5 deletions R/search.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ addSearchOSM <- function(map,
options = searchOptions(autoCollapse = TRUE, minLength = 2),
icon = NULL) {
map$dependencies <- c(map$dependencies, leafletSearchDependencies())

result <- makeSearchIcon(map, options)
map <- result$map
options$marker$icon <- result$icon

invokeMethod(
map,
getMapData(map),
Expand Down Expand Up @@ -182,6 +187,10 @@ clearSearchOSM <- function(map) {
#' For this to be effective one of \code{showSearchLocation}, \code{showBounds}, \code{showFeature} shoule also be TRUE.
#' @param displayText Boolean. If TRUE show a text box with found location's name on the map.
#' @param group String. An optional group to hold all the searched locations and their results.
#' @param marker Let's you set the icon. Can be an icon made by \code{\link[leaflet]{makeIcon}} or \code{\link[leaflet]{makeAwesomeIcon}}
#' @param showFeatureOptions A list of styling options for the found feature
#' @param showBoundsOptions A list of styling options for the bounds of the found feature
#' @param showHighlightOptions A list of styling options for the hover effect of a found feature
#' @return modified map
#' @rdname search-geocoding
#' @export
Expand All @@ -192,14 +201,42 @@ addReverseSearchOSM <- function(
showFeature = TRUE,
fitBounds = TRUE,
displayText = TRUE,
group = NULL) {
group = NULL,
marker = list(
icon = NULL
),
showFeatureOptions = list(
weight = 2,
color = "red",
dashArray = '5,10',
fillOpacity = 0.2,
opacity = 0.5
),
showBoundsOptions = list(
weight = 2,
color = "#444444",
dashArray = '5,10',
fillOpacity = 0.2,
opacity = 0.5
),
showHighlightOptions = list(
opacity = 0.8,
fillOpacity = 0.5,
weight = 5
)) {

map$dependencies <- c(map$dependencies, leafletSearchDependencies())
if (displayText == TRUE) {
map <- map %>%
addControl("Click anywhere on the map to reverse geocode",
position = "topright", layerId = "reverseSearchOSM"
)
}

result <- makeSearchIcon(map, list("marker" = marker))
map <- result$map
marker$icon <- result$icon

invokeMethod(
map,
getMapData(map),
Expand All @@ -208,7 +245,11 @@ addReverseSearchOSM <- function(
showSearchLocation = showSearchLocation,
fitBounds = fitBounds,
showBounds = showBounds,
showFeature = showFeature
showFeature = showFeature,
marker = marker,
showFeatureOptions = showFeatureOptions,
showBoundsOptions = showBoundsOptions,
showHighlightOptions = showHighlightOptions
),
group
)
Expand Down Expand Up @@ -416,7 +457,6 @@ makeSearchIcon <- function(map, options) {

if (!is.null(icon)) {
if (isTRUE(icon)) {
# Do nothing
} else {
if (inherits(icon, "leaflet_awesome_icon")) {
map <- addAwesomeMarkersDependencies(map, icon$library)
Expand All @@ -430,9 +470,9 @@ makeSearchIcon <- function(map, options) {
icon$iconSize <- list(icon$iconSize)
}
}
options$marker$icon <- leaflet::filterNULL(icon)
icon <- leaflet::filterNULL(icon)
}
}

return(list(map = map, icon = options$marker$icon))
return(list(map = map, icon = icon))
}
58 changes: 53 additions & 5 deletions inst/examples/shiny/search/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ library(leaflet.extras)
library(shiny)
options(shiny.autoreload = TRUE)

greenLeafIcon <- makeIcon(
iconUrl = "http://leafletjs.com/examples/custom-icons/leaf-green.png",
iconWidth = 38, iconHeight = 95,
iconAnchorX = 22, iconAnchorY = 94,
shadowUrl = "http://leafletjs.com/examples/custom-icons/leaf-shadow.png",
shadowWidth = 50, shadowHeight = 64,
shadowAnchorX = 4, shadowAnchorY = 62
)
awesomeicon <- leaflet::makeAwesomeIcon(
icon = "ios-close", iconColor = "black",
library = "ion", markerColor = "green")

ui <- fluidPage(
h4("OSM Search")
, actionButton("clearReverseSearchOSM", "clearReverseSearchOSM")
Expand All @@ -20,17 +32,53 @@ ui <- fluidPage(
server <- function(input, output, session) {
output$map <- renderLeaflet({
leaflet() %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addProviderTiles(providers$CartoDB.Positron,
options = tileOptions(noWrap = FALSE)) %>%
addSearchOSM(options = searchOptions(
position = "topleft",
hideMarkerOnCollapse = TRUE)) %>%
hideMarkerOnCollapse = TRUE,
marker = list(
# icon = greenLeafIcon,
icon = awesomeicon,
circle = list(
radius = 50,
weight = 3,
color = 'yellow',
stroke = TRUE,
fill = TRUE
)
))) %>%
addReverseSearchOSM(
showSearchLocation = TRUE,
showBounds = FALSE,
showBounds = TRUE,
showFeature = TRUE,
fitBounds = TRUE,
displayText = TRUE,
group = "revosm") %>%
group = "revosm",
marker = list(
icon = greenLeafIcon
# icon = awesomeicon
)
,showFeatureOptions = list(
weight = 7,
color = "purple",
dashArray = '2,5',
fillOpacity = 0.8,
opacity = 1
)
,showBoundsOptions = list(
weight = 4,
color = "orange",
dashArray = '10,20',
fillOpacity = 0.1,
opacity = 1
)
,showHighlightOptions = list(
opacity = 1,
fillOpacity = 0.8,
weight = 9
)
) %>%
leaflet::addLayersControl(overlayGroups = "revosm")
})

Expand All @@ -39,7 +87,7 @@ server <- function(input, output, session) {
print(input$map_search_location_found)
})
output$rev_search_found <- renderPrint({
print("Reverse Search FOund")
print("Reverse Search Found")
print(input$map_reverse_search_feature_found)
})

Expand Down
Loading

0 comments on commit dba1227

Please sign in to comment.