library(shiny) library(magrittr) library(leaflet) library(httr) library(jsonlite)
This vignette summarizes functions of API package that gets connected with 3 google APIs and gives examples on how to use them. This package uses three APIs. They are,
The function "parsing" has been used as an internal function to parse the data. Note: Enter the key in "z"
#internal function to parse parsing<-function(req) {x<-content(req,"text") if (identical(x,"")) warning ("HI AMIGO, THIS IS EMPTY! BE CAREFUL!") fromJSON(x)}
The function "latlong" uses geocode api. It takes the place "name" as input and returns the exact latitude and longitude along with the formatted address.
latlong<-function(place) #place <- "Vellore" { z<-list(address=place,key="AIzaSyAGdetT_wO2o2Q6LfHFVmEw7yxFnvVpCbo") #create the list of parameters I will send to google. place<-GET("https://maps.googleapis.com/maps/api/geocode/json",query=z) #this is the actual API connection. stop_for_status(place) parsed_place<-parsing(place) #I just parse what I receive. x<-parsed_place$results #now all my results are in x! x$formatted_address list("latitude and longitude"=x$geometry$location, "Complete name" = x$formatted_address) }
This function "distance" uses directions api. It takes the two place names as origin and destination and returns the exact distance in kilometers between those places.
distance<-function(place1,place2) #place1<-"rydsvagen 246" #place2<-"linkoping university" { z<-list(origin=place1,destination=place2,key="") place<-GET("https://maps.googleapis.com/maps/api/directions/json",query=z) #this is the actual API connection. x<-parsing(place) unlist(x) x$routes$legs z<-x$routes$legs[[1]] z$distance }
This function "nearby_place" uses google place api. It takes the place name as user input and returns the exact nearby place names along with their ratings to suggest people the best place to visit.
nearby_place<-function(placename) { #placename="museums in linkoping" z<-list(input=placename,key="") #create the list of parameters I will send to google. placename<-GET("https://maps.googleapis.com/maps/api/place/textsearch/json?",query=z) #this is the actual API connection. x<-parsing(placename) unlist(x) list("Nearby place names"=x$results$name, "Ratings" = x$results$rating) }
The shiny app is created to visualize the exact location in map. Once the place name is entered, the output will be the text with latitude, longitude and the map pointing the exact location.
Location <- renderText(paste(c(o1,o2,o3))) mymap <- renderLeaflet({ x2 %>% leaflet() %>% addTiles() %>% addMarkers(lng = ~lng, lat = ~lat, popup="Location you was searching for")})
Enter the place name (Example: Linkoping), then enter the key and click the button "Find exact location"
knitr::include_graphics("C:/Users/roshn/Desktop/MS/Advanced R Prog/Lab/lab 5/5-master/5-master/Example.png")
"More to know how to render map outputs" (via)
"More to know how to use leaflets for map outputs" (via)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.