我有一个使用 Leaflet 的 map ,它在 r markdown 的 html 输出中工作,但是当我在 Google 或 IE 中打开 html 时, map 不会呈现。 Ggmap 工作正常,但我更喜欢交互式 map 。有什么想法吗?
```{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)
```
最佳答案
我遇到了同样的问题,它似乎与未在浏览器中呈现的默认图 block 有关。但是,我不知道为什么会这样。
所以有几个选项是
- 更改图 block 层 ( https://leaflet-extras.github.io/leaflet-providers/preview/ )
- 使用不同的映射包。在这里,我演示了用于绘制交互式 Google map 的 googleway 包
```{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)
```
这三个都在 Chrome 中工作
关于html - 传单 map 不会在浏览器中呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44895440/