获取地图视图的当前范围(R,单位为)
我有一个小闪亮的应用程序代码如下。用户可以编辑、添加和删除点和多边形。因为最终产品将是两个shapefile(一个点,一个多边形),所以我需要将数据分开(在任何情况下,它们很可能具有不同的属性,因此将它们合并到一个数据集中不是一个选项) 我的解决方案是让mapview根据用户选择要编辑的内容每次生成。我很高兴这一切都很好——但每次发生这种情况时,地图视图的范围都会重置为数据的范围。我想在更新之前找到一些方法来捕获当前区段,这样我就可以在获取地图视图的当前范围(R,单位为),r,shiny,leaflet,android-mapview,R,Shiny,Leaflet,Android Mapview,我有一个小闪亮的应用程序代码如下。用户可以编辑、添加和删除点和多边形。因为最终产品将是两个shapefile(一个点,一个多边形),所以我需要将数据分开(在任何情况下,它们很可能具有不同的属性,因此将它们合并到一个数据集中不是一个选项) 我的解决方案是让mapview根据用户选择要编辑的内容每次生成。我很高兴这一切都很好——但每次发生这种情况时,地图视图的范围都会重置为数据的范围。我想在更新之前找到一些方法来捕获当前区段,这样我就可以在setView中使用它来保持相同的区段。有办法做到这一点吗?
setView
中使用它来保持相同的区段。有办法做到这一点吗?Mapview似乎没有这个功能,我不知道如何才能从这一点上与传单对话来实现它
谢谢你的帮助
应用程序:
库(闪亮)
图书馆(单张)
图书馆(RColorBrewer)
图书馆(rgdal)
图书馆(单张、附加资料)
库(地图编辑)
图书馆(地图视图)
图书馆(sp)
工作空间
library(shiny)
library(leaflet)
library(RColorBrewer)
library(rgdal)
library(leaflet.extras)
library(mapedit)
library(mapview)
library(sp)
workSpace <- "C:/temp"
myPoints <<- readOGR(workSpace,"Points")
myPolys <<- readOGR(workSpace,"Polys")
## Remove a few unneccesary attributes (for this exercise, anyway):
myPolys$Shape_Leng <- NULL
myPolys$Shape_Area <- NULL
myPoints$Shape_Leng <- NULL
myPoints$Shape_Area <- NULL
myPolys$OBJECTID <- NULL
myPoints$OBJECTID <- NULL
ui <- bootstrapPage(
tags$style(type= "text/css", "html, body {width:100%;height:100%}"),
editModUI("test-edit", height = "100%"),
absolutePanel(top=10, right=10,width=300,
wellPanel(style = "opacity: 0.80",
fluidRow(
tags$div(title = "Title",
tags$h4("Select Edit Layer")
)
),
fluidRow(
radioButtons(inputId = "layerChoice", label = NULL, choices = list("Polys" = 1, "Points" = 2))
)
)
)
)
server <- function(input, output, session) {
cntr_coords <- c(mean(coordinates(myPolys)[,1]), mean(coordinates(myPolys)[,2]))
myMap <<- (mapview(myPolys) + mapview(myPoints))@map %>% setView(cntr_coords[1], cntr_coords[2], zoom = 17)
result <- callModule(editMod, "test-edit", myMap, "myPolys")
observeEvent(input$layerChoice, {
if (input$layerChoice == 1)
{
result <- callModule(editMod, "test-edit", myMap, "myPolys")
}
else
{
result <- callModule(editMod, "test-edit", myMap, "myPoints")
}
})
observe({req(result()$finished)
## Code here to save edits
showModal(modalDialog(title = "Confirm save",
"Do you want to save your edit?",
footer = tagList(
modalButton("Cancel"),
actionButton("ok", "OK")
)))
})
observeEvent(input$ok, {
## Write the data to the existing data
## firstly get the result.
newData <- result()$finished
if (input$layerChoice == 1) {
## Coerce into spatial data frame
newPoly <- as(newData, "Spatial")
## Add required columns and give them a value (this will become a menu)
newPoly$NAME <- "NewName"
newPoly$CODE <- "CodeNEW"
newPoly$Attrib1 <- "New Attrib"
newPoly$Attrib2 <- 8888
## Drop the attributes we don't want
newPoly$X_leaflet_id <- NULL
newPoly$feature_type <- NULL
## Now merge with the existing data
myPolys <<- rbind(myPolys, newPoly)
}
else
{
newPoint <- as(newData, "Spatial")
## Add required columns etc
newPoint$NAME <- "NewPointName"
newPoint$CODE <- "CodePt"
## Drop the attributes we don't want
newPoint$X_leaflet_id <- NULL
newPoint$feature_type <- NULL
## Now merge with the existing data
myPoints <<- rbind(myPoints, newPoint)
}
## Refresh the map
myMap <<- (mapview(myPolys) + mapview(myPoints))@map
## Close the menu
removeModal()
})
}
# Run the application
shinyApp(ui = ui, server = server)