我是新来的,想要帮助弄清楚如何在 R 的 Leaflet 地图中使用 Google Maps API 调用 Google Maps 基础层。这是我到目前为止所学到的:
- Pavel Shramov 的 Leaflet 插件允许从 Leaflet JS 内部调用 Google 的基本地图,据我所知,这种方式符合 Google 的 ToS。这里有一个例子,效果很好。
- 有关通过 htmltools 和 htmlwidgets在 Leaflet for R 中使用任意 Leaflet JS 插件的一般说明。
基于以上内容,我了解我在 R 中的代码需要如下所示,但我无法加载 Google 底图:
library(leaflet)
library(htmltools)
library(htmlwidgets)
# 1: Tell htmlwidgets where to look for the script and stylesheets
gLeafletPlugin <- htmlDependency("gLeaflet","1.9.0",
src = c(href = "https://cdnjs.cloudflare.com/ajax/libs/leaflet-plugins/1.9.0/layer/tile/"),
script = "Google.js")
# FIRST PLACE WHERE I AM STUCK: I am pretty sure, I also need to pass on
# the Google Maps API script reference before or along with the above command,
# but trying to include two htmlDependency objects together in a list, did not
# work. Maybe I wasn't doing it right.
# 2: Make a map and add the htmlDependency object to it
registerPlugin <- function(map, plugin) {
map$dependencies <- c(map$dependencies, list(plugin))
map
}
leaflet() %>%
setView(76.65, 12.32, zoom = 9) %>%
registerPlugin(gLeafletPlugin) %>%
# 3: Pass on custom JS logic: SECOND PLACE WHERE I AM STUCK
onRender("function(el, x) {
L.Google('TERRAIN').addTo(this);
}")
如果有人能告诉我:(a)当涉及多个脚本时,注册 htmlDependency 的正确方法将不胜感激;(b) 调用显示 Google 地形底图所需的额外 JS 逻辑的正确语法。
谢谢!