API.R

#' This is an API package 
#'  
#'  
#'  This package intracts with Google API 
#'  
#'  @param  place is a character
#'  
#'  @return the long and lat
#'  
#'  @examples     
#'  latlong(Tehran)
#'  
#'  @export



library(shiny)
library(magrittr)
library(leaflet)
library(httr)
library(jsonlite)
library("knitr")



#internal function to parse
parsing<-function(req)
{x<-content(req,"text")
if (identical(x,"")) warning ("HI AMIGO, THIS IS EMPTY! BE CAREFUL!")
fromJSON(x)}
f <- "AIzaSyAGdetT_wO2o2Q6LfHFVmEw7yxFnvVpCbo"
# Using geocode api
latlong<-function(place,f)
  #place <- "Vellore"
{
  z<-list(address=place,key=f) #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)
}

# Using directions api
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
}

# Using place api
nearby_place<-function(placename)
{
  #placename="museums in linköping"
  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)
}
pedism/5 documentation built on May 8, 2019, 12:57 p.m.