Created
July 26, 2016 19:09
-
-
Save aagarw30/90bf8847bebd21fe6842dc584ca01187 to your computer and use it in GitHub Desktop.
Lat.Long
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
# install.packages("XML") | |
library(XML) | |
library(plyr) | |
# Preparing the data | |
cities <- c("Varanasi, India", "Jaipur, India", "Residency Road, Bangalore, India" ,"ITPL, Bangalore, India", "Phoenix, USA", | |
"Guadalajara, Mexico", "Marthahalli, Bangalore, India", "Melbourne, Australia","Bangalore, India") | |
desc = c("School @ St. John's School", "Graduation @ SKIT, Jaipur", "Job @ Think Ahead Advisory Services Ltd.", | |
"Job @ TCS Bang.", "Job @ TCS Phoenix", "Job @ TCS Mexico", "Job @ NICE Interactive Solutions", "Job @ NICE Systems Aus.","Job @ NICE Interactive Solutions") | |
# Function to get the latitude and longitude | |
lat.long <- function(place) | |
{ | |
theURL <- sprintf('http://maps.google.com/maps/api/geocode/xml?sensor=false&address=%s', place) | |
doc <- xmlToList(theURL) | |
data.frame(Place=place, | |
Latitude=as.numeric(doc$result$geometry$location$lat), | |
Longitude=as.numeric(doc$result$geometry$location$lng), | |
stringsAsFactors=FALSE) | |
} | |
places <- adply(cities,1,lat.long) | |
str(places) | |
df = data.frame(places, desc, stringsAsFactors = F) | |
df | |
knitr::kable(df[, -1], digits=3) | |
# Plotting on map | |
library(leaflet) | |
icon = makeIcon(iconUrl = "pin_yellow.png", iconWidth = 12, iconHeight = 12) | |
m=leaflet(df) %>% | |
addTiles(group = "OSM (default)") %>% | |
# addProviderTiles("Stamen.Toner", group = "Toner") %>% | |
addProviderTiles("CartoDB.DarkMatter", group = "Stamen.Watercolor") %>% | |
#setView(100, 15, zoom=2) %>% | |
setView(10, 20, zoom=2) %>% | |
addPolylines(~Longitude, ~Latitude, data=df, color = "white") %>% | |
icon = makeIcon(iconUrl = "pin_yellow.png", iconWidth = 12, iconHeight = 12) | |
addMarkers(data= df, lng = ~Longitude, lat =~Latitude ,icon = icon, | |
options = markerOptions(title = ~desc) ) | |
m | |
# %>% | |
addLayersControl( | |
baseGroups = c("OSM (default)", "Toner", "Stamen.Watercolor"), | |
options = layersControlOptions(collapsed = FALSE) | |
) | |
# %>% | |
# addPopups(data=df, lng = ~Longitude, lat = ~Latitude, popup = ~desc, | |
# options = popupOptions(maxWidth = 150, closeButton = FALSE)) | |
# | |
# popupOptions(maxWidth = 10, minWidth = 50, maxHeight = NULL, autoPan = TRUE, | |
# keepInView = FALSE, closeButton = TRUE, zoomAnimation = TRUE, closeOnClick = NULL, | |
# className = "") | |
# | |
library(htmlwidgets) | |
saveWidget(m, file="test2.jpeg") | |
library(leaflet) | |
m = leaflet(df) %>% | |
# addTiles() %>% | |
addProviderTiles("CartoDB.Positron", | |
options = providerTileOptions(opacity = 1)) %>% | |
addProviderTiles("CartoDB.DarkMatter", | |
options = providerTileOptions(opacity = 0.8)) %>% | |
#setView(100, 15, zoom=2) %>% | |
setView(10, 20, zoom=2) %>% | |
# color and weigth for the color and weight of the line | |
addPolylines(~Longitude, ~Latitude, data=df, color = "white", | |
weight = 2, opacity = 0.6) %>% | |
addCircleMarkers(~Longitude, ~Latitude, data=df, color = "#daa520", | |
opacity = 0.6, radius = 2, popup=~desc, popupOptions(keepInView=TRUE)) | |
# Saving the leaflet map (as image or web page or pdf) | |
library(htmlwidgets) | |
saveWidget(m, file="place.html") | |
###### | |
# Let us prepare data for maping | |
# Character vector cities with places (cities & countries) I have been during my academics, job | |
cities <- c("Varanasi, India", | |
"Jaipur, India", | |
"Bangalore, India", | |
"Phoenix, Arizona, USA", | |
"Guadalajara, Mexico", | |
"Melbourne, Australia", | |
"Bangalore, India") | |
# Character vector with the description (school or job along with the year I was at that place in vector cities) | |
desc = c("School(1988-2000)", | |
"College(2001-2005)", | |
"Job(2005 - 2008)", | |
"Job(2008 - 2011)", | |
"Job(2011 - 2014)", | |
"Job(2015)", | |
"Job(2015)") | |
# Using the Google Maps API to fetch the geocodes (latitude and longitude) corresponding to the places in | |
latlong <- function(place) | |
{ | |
theURL <- sprintf('http://maps.google.com/maps/api/geocode/xml?sensor=false&address=%s', place) | |
geo <- xmlToList(theURL) | |
data.frame(Place=place, | |
Latitude=as.numeric(geo$result$geometry$location$lat), | |
Longitude=as.numeric(geo$result$geometry$location$lng), | |
stringsAsFactors=FALSE) | |
} | |
# using adply to apply the function lat.long to each row in the vector cities | |
places <- adply(cities, 1, latlong) | |
str(places) | |
df = data.frame(places, desc, stringsAsFactors = F) | |
df | |
# optional | |
knitr::kable(df[, -1], digits=3) | |
# using the leaflet package to put the info on map | |
library(leaflet) | |
icon = makeIcon(iconUrl = "pin_yellow.png", iconWidth = 12, iconHeight = 12) | |
mymap = leaflet() %>% | |
addProviderTiles("CartoDB.Positron", | |
options = providerTileOptions(opacity = 1)) %>% | |
addProviderTiles("CartoDB.DarkMatter", | |
options = providerTileOptions(opacity = 0.8)) %>% | |
setView(10, 20, zoom=2) %>% | |
# color and weight arguments for the color and weight of the line | |
addPolylines(data=df, ~Longitude, ~Latitude, color = "white", | |
weight = 2, opacity = 0.6) %>% | |
addCircleMarkers(data=df, ~Longitude, ~Latitude, color = "#daa520", | |
opacity = 0.6, radius = 2) %>% | |
addMarkers(data= df, lng = ~Longitude, lat =~Latitude ,icon = icon, | |
options = markerOptions(title = ~desc)) | |
# print the map | |
mymap | |
# Saving the leaflet map (as image or web page or pdf) | |
library(htmlwidgets) | |
saveWidget(m, file="place.html") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment