在.CSV的R中的传单地图上显示地址

时间:2017-04-26 15:26:15

标签: r leaflet

我的妻子与一群人一起工作,为我们居住的城市的学区提供家庭治疗,帮助他们学习残疾儿童。我正在尝试创建代码,以获取一个充满客户地址的.CSV文件以及哪个员工与每个员工合作并使用它创建一个地图,显示每个员工的地理范围。然后,当他们获得新客户时,他们可以查看地图并查看哪些员工已经在客户附近工作,以便最大限度地缩短旅行时间。我想到的地图如下(他们在一个城市工作,但我正在使用全国城市进行开发),我正在使用的粘贴.CSV数据也在下面。我一直在玩代码,但是在获取传单的语法方面遇到了麻烦。任何帮助都非常感谢,因为我是新的传单包,并且大多使用R进行统计,而不是像这样的可视化。谢谢!

.CSV数据(以分号分隔)

Client No.; Address; Employee;
1; Los Angeles, CA; Tom;
2; San Diego, CA; Tom;
3; Las Vegas, NV; Tom;
4; Albuquerque, NM; Tom;
5; Madison, WI; Laura;
6; Minneapolis, MN; Laura;
7; Chicago, IL; Laura;
8; Iowa City, IA; Laura;
9; Orlando, FL; Joe;
10; Mobile, AL; Joe;
11; Atlanta, GA; Joe;
12; Nashville, TN; Joe;
13; Albany, NY; Jamie;
14; Philadelphia, PA; Jamie;
15; Columbus, OH; Jamie;

Example map, trying to create this

UPDATE 我取得了一些进展,但仍然需要弄清楚多边形的添加。

library(leaflet)
require(RJSONIO)

geocodeAdddress_lat <- function(address) {
  require(RJSONIO)
  url <- "http://maps.google.com/maps/api/geocode/json?address="
  url <- URLencode(paste(url, address, "&sensor=false", sep = ""))
  x <- fromJSON(url, simplify = FALSE)
  if (x$status == "OK") {
    out <- c(x$results[[1]]$geometry$location$lat)
  } else {
    out <- NA
  }
  Sys.sleep(0.2)  # API only allows 5 requests per second
  out
}

geocodeAdddress_lng <- function(address) {
  require(RJSONIO)
  url <- "http://maps.google.com/maps/api/geocode/json?address="
  url <- URLencode(paste(url, address, "&sensor=false", sep = ""))
  x <- fromJSON(url, simplify = FALSE)
  if (x$status == "OK") {
    out <- c(x$results[[1]]$geometry$location$lng)
  } else {
    out <- NA
  }
  Sys.sleep(0.2)  # API only allows 5 requests per second
  out
}

mydata <- read.table("C:/Users/marast01.SJM/Documents/20170426, Leaflet     map.csv", 
                     header=TRUE, sep=",")#, row.names="id")

head(mydata)

for (i in 1:nrow(mydata)) {
  mydata$lat[i] <- geocodeAdddress_lat(mydata$Address[i])
  mydata$lng[i] <- geocodeAdddress_lng(mydata$Address[i])
}

head(mydata)

m <- leaflet(data = mydata) %>% addTiles() %>%
  addMarkers(mydata$lng, mydata$lat)

m  # Print the map

更改标记的颜色:

pal <- colorFactor(c("red", "blue", "yellow", "green"), 1:4)
pal(c(mydata$Employee))

mm <- leaflet(data = mydata) %>% addTiles() %>%
  addCircleMarkers(mydata$lng, mydata$lat, popup=mydata$Employee, 
                   color = pal(c(mydata$Employee)), fillOpacity = 0.5, stroke = FALSE)
mm

0 个答案:

没有答案