Leaflet marker instead of circles for search result - r

Leaflet allows to implement an interactive search feature into the map like this
library(leaflet)
library(leaflet.extras)
leaflet() %>% addTiles() %>% addSearchOSM()
However, the results are always showed with a circle marker. How do I change it to the classical pin marker? I'd expect it to be part of the searchOptions() function, but I can't find an according paramater.

Related

Create labels dynamically in leaflet

I am looking for a way to define labels in my leaflet map dynamically, such that they don't need to be precalculated but are generated just in time.
Let us assume we want to show the current time on the label:
leaflet() %>%
addCircleMarkers(lat=0, lng=0, label=lubridate::now())
However, this will always show the time of the map creation. Is there a way to delay the evaluation until the mouseover is actually shown?

How do I use pictures as custom icons in Leaflet?

I am trying to create an interactive map of football stadiums in the UK with the leaflet package in R. I understand that I can use a custom image as the icon on the map. For a given stadium, I would like to use the badge of the club that plays there as the icon. Initially I am trying to solve the simpler problem of just using one custom image for all the stadiums with the hope that once I can solve this, I will be able to generalise and have one custom image per stadium. I will provide a simplified data set of just two stadiums for the purposes of this question.
So far I have tried using this picture (https://upload.wikimedia.org/wikipedia/en/e/e5/AFC_Bournemouth_%282013%29.svg) of the Bournemouth badge however it does not appear on the map when run. I think R cannot properly access this image but I am not sure what types it can access or how it accesses them.
So far my code looks like this
library(leaflet)
# Create Data
finalData <- data.frame(Club = c("A.F.C. Bournemouth", "A.F.C. Wimbledon"), Latitude = c("50.7352","51.4051"), Longitude = c("-1.83839","-0.281984"), stringsAsFactors = FALSE)
# Create map using leaflet
m <- leaflet() %>%
addTiles()
# Attempt to create custom icon using the linked picture
BournemouthIcon = makeIcon("https://upload.wikimedia.org/wikipedia/en/e/e5/AFC_Bournemouth_%282013%29.svg", iconWidth = 8, iconHeight = 8)
m %>% addMarkers(lat = as.numeric(finalData$Latitude),
lng = as.numeric(finalData$Longitude),
clusterOptions = markerClusterOptions(),
icon = BournemouthIcon)
I expect to see the 2 stadiums plotted on the map with the Bournemouth badge as the marker but instead the markers are blank.1 It appears R knows where to plot the points (since if you zoom out it detects the cluster of points), however it does not know what to mark the point with.2 This links back to my misunderstanding of how R is reading the picture I supplied. Any help with how to properly build a custom marker and then pass it to addMarkers would be greatly appreciated. Furthermore some guidance on generalising this to having a unique custom picture for each marker would also be great!

Q: R plot building footprint / outline on map using OpenStreetMap data

I'm rather new to creating maps and stuff in R, so please bear with me.
I'm trying to plot a building outline in R using data from OpenStreetMap, instead of having to define a polygon myself for plotting the outline of the building. But I cannot get my head around it.
The example below is what I have done so far to get the location:
library(leaflet)
leaflet() %>%
setView(lng=10.153523, lat=57.207181, zoom = 16) %>%
addTiles() %>%
addMarkers(lng=10.153523, lat=57.207181, popup="My building example")
And the example building data is available on https://www.openstreetmap.org/way/481541280
How do I integrate this building outline data into my R script? Is there a neat way of doing this other than extracting the points one by one and making a polygon using leaflet in R?

Leaflet Polylines do not show up

I have a very small SpatialLinesDataFrame which I need to plot with Leaflet in R.
Unfortunately, for some reason I'm not able to do so.
This is the data (it's 12KB).
I try to do:
library(leaflet)
load("mylines.Rdata")
leaflet() %>% addTiles() %>% addPolylines(data=mylines)
However the resulting map does not make sense, I can only see a line in the top of the screen and this is not what should be plotted.
This is the result:
Instead, If I do:
library(mapview)
mapview(mylines)
Result:
It works perfectly, despite mapview using leaflet underneath.
What am I doing wrong in the Leaflet syntax?
I am used to working with leaflet providing rasters, so I usually use the addRasterImage function, which needs data projected over leaflet's display projection (EPSG:3857). However, for polygons and lines, as #TimSalabim correctly pointed out, this is not the case.
So the solution in this case was to just not reproject the data to the leaflet projection beforehand, and provide it in lat-lon coordinates (EPSG:4326).
mapview worked since it does this automagically.

R fastest Leaflet map to load

Background: I am currently drawing a leaflet map using the following code:
library(leaflet)
leaflet()%>%
addProviderTiles("Stamen.TerrainBackground")%>%
setView(lng=-81,lat=45,zoom=6)
Problem: The problem is that it takes around 4 seconds to have the map appear. This is an undesirable feature when I integrate the map with Rshiny, given that the website appears blank until the map finally loads.
I'm wondering two things:
1) Is there an open source map that is known to load quickly compared to other maps.
2) If 1) is not possible, I have noticed that in google maps, the tiles of the map start appearing in sequence. At least like that the user is able to know that the map is loading. Is there a way of having the tiles be drawn sequentially like that?
If you mean the tiles are slow to load, that is most likely either a problem with the tile provider or a problem with your connection. Leaflet itself is usually very fast in my experience.
As far as the tile provider, I've always had good success with open street map in JS.
With R it looks like this link https://rstudio.github.io/leaflet/ has the following example using OSM:
library(leaflet)
m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
m # Print the map
So calling "addTiles() %>% should use open street map as default.

Resources