Zeus
Zeus

Reputation: 1590

Leaflet map won't render in browser

I have a map using Leaflet which works in the html output of r markdown, but the map won't render when I open the html in Google or IE. Ggmap works fine, but I'd prefer an interactive map. Any ideas?

```{r setup, include=FALSE}
knitr::opts_chunk$set(
  echo = FALSE,
  warning = FALSE,
  message = FALSE,
  comment=NA,
  fig.height=7,
  fig.width = 9)
```

```{r Libraries}
library(leaflet)
library(ggmap)
```

Plot map with leaflet package.    
```{r Leaflet}
#using leaflet
Pin <- data.frame(t(c(ID=1,Lat=-27.474176,Lng=153.024901)))

Bne2 <- leaflet() %>%
  addTiles()  %>%
  addMarkers(data=Pin)
Bne2

```

Plot map with ggmap package.   
```{r Ggmap}
#using ggmap
Sa2 <- data.frame(t(c(ID=1,Lat=-27.474176,Lng=153.024901)))
Brisbane <- get_map("Brisbane,Australia",zoom=11)

BNE <- ggmap(Brisbane)+
  geom_point(data=Sa2,aes(x=Lng,y=Lat),color="black",size=3)

plot(BNE)

```

Upvotes: 2

Views: 1607

Answers (1)

SymbolixAU
SymbolixAU

Reputation: 26248

I'm seeing the same issue, and it appears to be related to the default tiles not rendering in the browser. But, I have no idea why that's the case.

So a couple of options are

  1. Change the tile layer (https://leaflet-extras.github.io/leaflet-providers/preview/)
  2. Use a different mapping package. Here I'm demonstrating my googleway package for plotting interactive Google Maps

```{r Libraries}
library(leaflet)
library(ggmap)
library(googleway)
```

Plot map with leaflet package.    
```{r Leaflet}
#using leaflet
Pin <- data.frame(t(c(ID=1,Lat=-27.474176,Lng=153.024901)))

Bne2 <- leaflet() %>%
  addTiles(urlTemplate = 'http://{s}.tile.opentopomap.org/{z}/{x}/{y}.png')  %>%
  addMarkers(data=Pin)
Bne2

```

Plot map with ggmap package.   
```{r Ggmap}
#using ggmap
Sa2 <- data.frame(t(c(ID=1,Lat=-27.474176,Lng=153.024901)))
Brisbane <- get_map("Brisbane,Australia",zoom=11)

BNE <- ggmap(Brisbane)+
  geom_point(data=Sa2,aes(x=Lng,y=Lat),color="black",size=3)

plot(BNE)

```

Plot map with googleway
```{r googleway}
## you need an api key to use google maps
mapKey <- 'your_api_key'

google_map(key = mapKey) %>%
    add_markers(data = Pin)

```

Here are all three working in Chrome

enter image description here

Upvotes: 1

Related Questions