Last active
November 4, 2016 02:29
-
-
Save kashitan/6b028bb306521570677fd6c9bdbf76c2 to your computer and use it in GitHub Desktop.
逆ジオコーディング結果を地図にプロット
This file contains 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(RPostgreSQL) | |
library(leaflet) | |
# データベースに接続 | |
con <- dbConnect(PostgreSQL(), host="host_name", port=port_number, dbname="database_name", user="user_name", password="password") | |
# クエリ文字列を定義 | |
query <- paste("SELECT pref, city, town, block,", | |
"ST_Y(ST_Transform(the_geom, 4326)) AS lat,", | |
"ST_X(ST_Transform(the_geom, 4326)) AS lon", | |
"FROM loc_ref_info", | |
"WHERE representative = 1", | |
"ORDER BY ST_SETSRID(ST_POINT(139.745433, 35.658581), 4326)", | |
"<-> the_geom", | |
"LIMIT 10") | |
# クエリの結果を取得 | |
df <- dbGetQuery(con, query) | |
# データベースから切断 | |
dbDisconnect(con) | |
# Leafletにプロットするマーカーを定義 | |
markerIcons <- iconList(blue = makeIcon("http://maps.google.com/mapfiles/ms/icons/blue.png"), | |
red = makeIcon("http://maps.google.com/mapfiles/ms/icons/red.png")) | |
# クエリの結果(青)と東京タワー(赤)を地図にプロット | |
leaflet(df) %>% | |
addProviderTiles("OpenStreetMap.DE") %>% | |
addMarkers(~lon, ~lat, popup = ~paste0(pref, city, town, block), icon = ~markerIcons["blue"]) %>% | |
addMarkers(139.745433, 35.658581, popup = "東京タワー", icon = ~markerIcons["red"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment