本文介绍了R:基于数据集中纬度和经度范围的SetView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ggmap中,您可以根据输入数据的经度和纬度来设置地图视图,其中csv的两列分别是经度和纬度,即

In ggmap, you can set the view of the map based on longitude and latitude of input data, where two columns of csv are Longitude and Latitude, i.e.

sep <- read.csv("31R_SEP_assets_csv.csv")

# get the map
bbox <- make_bbox(sep$Longitude, sep$Latitude, f = 0.3)
map <- get_map(bbox)

我正在传单中搜索类似的函数,但到目前为止,我只找到setView(),它接受了经度和纬度的实际值,即

I am searching for similar function in leaflet, but so far, I only find setView() that takes in an actual value for latitude and longitude, i.e.

m <- leaflet() %>% setView(lng = -71.0589, lat = 42.3601, zoom = 12)
m %>% addTiles()

要使用什么功能?

到目前为止,这是我的代码

Here's my code so far

library(ggmap)
library(ggplot2)
library(historydata)
library(leaflet)
library(rgdal)

setwd("d:/GIS/31R")
sep <- read.csv("31R_SEP_assets_csv.csv")

Sub1 <- sep[grep("SEP.12", names(sep))]
sep$newCol <- 100*rowSums(Sub1)/rowSums(sep[4:7])

# create a new grouping variable
Percent_SEP12_Assets <- ifelse(sep[,8] <= 33, "Less than 33%", ifelse(sep[,8] >= 66, "More than 66%", "Between 33% and 66%"))


leaflet(data = sep[]) %>% addTiles() %>%
  addMarkers(~Longitude, ~Latitude,  popup = ~as.character(paste(Site, Percent_SEP12_Assets , sep=", "))) 

并且输出会一直缩小

dput(sep)
structure(list(Site = structure(1:5, .Label = c("Staten Island\\31R001", 
"Staten Island\\31R002", "Staten Island\\31R003", "Staten Island\\31R004", 
"Staten Island\\31R005"), class = "factor"), Latitude = c(40.508874, 
40.577256, 40.520825, 40.552373, 40.529697), Longitude = c(-74.244048, 
-74.100135, -74.211845, -74.195516, -74.187532), Windows.SEP.11 = c(63L, 
174L, 11L, 85L, 163L), Mac.SEP.11 = c(0L, 1L, 4L, 0L, 0L), Windows.SEP.12 = c(124L, 
185L, 9L, 75L, 23L), Mac.SEP.12 = c(0L, 1L, 32L, 1L, 0L), newCol = c(66.3101604278075, 
51.5235457063712, 73.2142857142857, 47.2049689440994, 12.3655913978495
)), .Names = c("Site", "Latitude", "Longitude", "Windows.SEP.11", 
"Mac.SEP.11", "Windows.SEP.12", "Mac.SEP.12", "newCol"), row.names = c(NA, 
-5L), class = "data.frame")

更新

尝试

addTiles(do.call(fitBounds, args = c(list(map = leaflet()), as.list(setNames(make_bbox(sep$Longitude, sep$Latitude, f = 0.3), c("lng1", "lat1", "lng2", "lat2")))))

并且地图为空白

然后尝试

leaflet(data = sep[]) %>% addTiles()  %>%
  fitBounds(~min(sep$Longitude), ~min(sep$Latitude), ~max(sep$Longitude), ~max(sep$Latitude)) %>%
  addMarkers(~Longitude, ~Latitude,  popup = ~as.character(paste(Site, Percent_SEP12_Assets , sep=", "))) 

地图仍然缩小...

我正在使用使用经度和纬度平均值的setView

I am using setView that uses average of Longitude and Latitude

leaflet(data = sep[]) %>% 
  setView(lng = mean(sep$Longitude), lat = mean(sep$Latitude), zoom = 12) %>% addTiles()  %>%
  addMarkers(~Longitude, ~Latitude,  popup = ~as.character(paste(Site, Percent_SEP12_Assets , sep=", "))) 

到目前为止似乎仍在工作....

Seems to work so far ....

推荐答案

使用

fitBounds(lng1 = min(table$lng), 
          lat1 = min(table$lat), 
          lng2 = max(table$lng), 
          lat2 = max(table$lat))

这篇关于R:基于数据集中纬度和经度范围的SetView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 20:20