- 修改:
- gcode將"台北車站"字串以iconv從big5編碼為utf8
- 取得個地點打卡次數時,順便將location寫入fields取回lat, lng等地理座標資訊
install.packages("ggmap") install.packages("leaflet") install.packages("DT") install.packages("jsonlite") install.packages("curl") install.packages("dplyr") library(ggmap) library(leaflet) library(DT) options(stringsAsFactors = F) (myplace <- geocode(iconv("台北車站", "big5", "utf8")))顯示台北火車站座標結果
lon lat 1 121.517 25.04774繪製出台北火車站方圓5公里(radius=5000)的範圍
leaflet() %>% addTiles() %>% fitBounds(0, 40, 10, 50) %>% setView(myplace$lon, myplace$lat, zoom = 12) %>% addCircles(myplace$lon, myplace$lat, radius = 5000)
台北火車站方圓5公里 |
library(jsonlite) 經緯度 <- paste(myplace$lat, myplace$lon, sep=",") 方圓幾公尺 <- 5 * 1000 token <- "EAACEdEose0(略)ppeaL" url <- sprintf("https://graph.facebook.com/search?type=place¢er=%s&distance=%s&limit=50000&access_token=%s", 經緯度, 方圓幾公尺, token) tmp <- fromJSON(url) result <- tmp$data[c("category","name","id")] while(!is.null(tmp$paging$`next`)){ tmp <- fromJSON(tmp$paging$`next`) result <- rbind(result, tmp$data[c("category","name","id")]) } datatable(result, extensions = 'Scroller', options = list( deferRender = TRUE, scrollY = 500, scroller = TRUE ))##執行結果
取得每個地點的 名稱(name)、打卡數(checkins)、地點類別(category)、和座標資料(location)
library(dplyr) fields <- "name,checkins,category,location" checkin <- sapply(result$id, function(id){ url <- sprintf("https://graph.facebook.com/%s?fields=%s&access_token=%s", id, fields, token) #unlist(fromJSON(url)) fromJSON(url) }) mycheckin <- as.data.frame(t(checkin)) mycheckin$checkins <- as.integer(mycheckin$checkins) mycheckin <- mycheckin[order(mycheckin$checkins, decreasing = T),] row.names(mycheckin) <- NULL datatable(mycheckin, extensions = 'Scroller', options = list( deferRender = TRUE, scrollY = 200, scroller = TRUE ))##執行結果 #