Created
March 5, 2023 09:15
-
-
Save tetlabo/db8451ea698cd9d50bb17a4bd01c7426 to your computer and use it in GitHub Desktop.
公共交通オープンデータから横浜市営バスの位置情報を取得するプログラム
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(jsonlite) | |
library(leaflet) | |
library(htmlwidgets) | |
library(htmltools) | |
# 横浜市交通局バスロケーション情報の取得 | |
# APIキーはご自身で取得し、以下の "consumerKey=" 以降に付与してください。 | |
url <- "https://api.odpt.org/api/v4/odpt:Bus?odpt:operator=odpt.Operator:YokohamaMunicipal&acl:consumerKey=XXXXXXXXXXXXXXXXXX" | |
# 30秒に1回のループ | |
while(TRUE){ | |
# JSONデータをlistとして取得 | |
bus_data <- fromJSON(url) | |
# 運行番号、緯度、経度を抽出 | |
bus_df <- data.frame(busno = bus_data[["odpt:busNumber"]], lng = bus_data[["geo:long"]], lat = bus_data[["geo:lat"]]) | |
popup_text <- paste0("運行番号: ", bus_df[["busno"]], "<br>緯度: ", round(bus_df[["lng"]], 2) , ", 経度: ", round(bus_df[["lat"]], 2)) | |
title <- tags$div(HTML(bus_data[["dct:valid"]][1])) | |
m <- leaflet(bus_df) %>% | |
addTiles() %>% | |
setView(lng = 139.58, lat = 35.47,zoom = 12) %>% | |
addMarkers(lng = ~lng, lat = ~lat, popup = popup_text) %>% | |
addControl(title, position = "bottomleft") | |
print(m) | |
Sys.sleep(30) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment