在R-Shiny中显示一个包含KML文件的交互式地图时,我遇到了一些问题。我安装了传单-插件,并能够创建一个在浏览器中自己正确显示的HTML,但不能在闪亮的范围内显示。这种尝试遵循了一个示例这里 --如果您查看源代码,代码是可用的。
因为这个初始版本不需要修改HTML本身,所以我尝试按照示例这里在我的页面中包含原始的HTML,但是当我试图将它作为一个iframe包含在R-Shiny中(遵循这一讨论)时,我收到了一个404错误。然后,我为KML文件和HTML文件设置了一个外部面向服务器,并得到了相同的结果。
我能够在没有使用有光泽的传单的KML文件的情况下获得一个映射,但坦率地说,我不知道如何添加KML文件,并且在文档中看不到这一点。
最后,我尝试了rMaps,它确实有一个"addKML“方法,但是我无法让它处理服务器上不同位置的文件(
map1 = Leaflet$new()
map1$setView(c(45.5236, -122.675), 13)
map1$tileLayer("http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png")
map1$addKML('http://kml-samples.googlecode.com/svn/trunk/kml/Placemark/placemark.kml')
map1它不需要$addKML行就可以工作。值得注意的是,示例1 这里上的分片线也产生了一个空白地图。
实际上,我有一些可能类似的问题,托管仍然是未解决的派生标题层,这也是我选择在这个应用程序的演示版本中使用KML层的原因之一,这也是为什么我在这里标记networking。我用的是数字海洋。
谢谢你的想法和建议。
发布于 2014-05-13 10:27:41
我认为rMaps库中可能有一个小问题。如果您检查config.yml文件https://github.com/ramnathv/rCharts/blob/master/inst/libraries/leaflet/config.yml,您将看到对于content (cdn),有对"http://harrywood.co.uk/maps/examples/leaflet/leaflet-plugins/layer/vector/KML.js“的引用。这个KML阅读器是一个从https://github.com/shramov/leaflet-plugins/blob/master/layer/vector/KML.js的传单插件。当内容在本地交付时:
css: [external/leaflet.css, external/leaflet-rCharts.css, external/legend.css]
jshead:
- external/leaflet.js
- external/leaflet-providers.js
- external/Control.FullScreen.js没有对此javascript文件的引用。我们可以解决这个问题:
require(yaml)
leafletLib <- file.path(find.package("rMaps"), "libraries", "leaflet")
rMapsConfig <- yaml.load_file(file.path(leafletLib, "config.yml"))
# add a kml library
kmlLib <- readLines("http://harrywood.co.uk/maps/examples/leaflet/leaflet-plugins/layer/vector/KML.js")
write(kmlLib, file.path(leafletLib, "external", "leaflet-kml.js"))
# add the library to config.yml
rMapsConfig$leaflet$jshead <- union(rMapsConfig$leaflet$jshead , "external/leaflet-kml.js")
write(as.yaml(rMapsConfig), file.path(leafletLib, "config.yml"))现在,config.yml将包含指向KML读取器的必要链接,并且现在external/leaflet-kml.js中存储了一个本地副本。然而,我们的示例仍然不能工作,因为我们将获得一个Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://kml-samples.googlecode.com/svn/trunk/kml/Placemark/placemark.kml.,可以通过将资源移动到相同的域或启用CORS来修复这个问题。
我们需要在本地提供这个文件。我们可以将它作为临时措施放在rMaps包中的传单文件夹中。创建映射时,此文件夹将被复制到临时文件中:
require(rMaps)
map1 = Leaflet$new()
map1$setView(c(45.5236, -122.675), 13)
map1$tileLayer("http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png")
map1$addKML('leaflet/placemark.kml')
# temp copy http://kml-samples.googlecode.com/svn/trunk/kml/Placemark/placemark.kml
# to rMaps
sampleKml <- readLines('http://kml-samples.googlecode.com/svn/trunk/kml/Placemark/placemark.kml')
write(sampleKml, file.path(leafletLib, 'placemark.kml'))
# finally try the map
map1
# remove the temp file
file.remove(file.path(leafletLib, 'placemark.kml'))更新:在rCharts中有一个addAssets方法,允许您添加.js文件。这允许我们简化一些事情,并且不需要我们编写js文件的副本,也不需要编辑config.yml文件。
require(rMaps)
map1 = Leaflet$new()
map1$setView(c(45.5236, -122.675), 13)
map1$tileLayer("http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png")
map1$addAssets(css = NULL, jshead = 'http://harrywood.co.uk/maps/examples/leaflet/leaflet-plugins/layer/vector/KML.js')
map1$addKML('leaflet/placemark.kml')
leafletLib <- file.path(find.package("rMaps"), "libraries", "leaflet")
sampleKml <- readLines('http://kml-samples.googlecode.com/svn/trunk/kml/Placemark/placemark.kml')
write(sampleKml, file.path(leafletLib, 'placemark.kml'))
# finally try the map
map1
# remove the temp file
file.remove(file.path(leafletLib, 'placemark.kml'))

https://stackoverflow.com/questions/23618813
复制相似问题