In this lab we have created a package to connect to web API using R.We have integrated Google Geocode Api for search a place by text string and by latitude and longitude.We have implemented two functions for thiis purpose which are as follows:
search_place()It takes one parameter as input:
search_place(search_text)
search_text (for example: search_text = "McDonald")
The text is the input given by user which should be of character type
search_by_latlng()It takes two parameters as input:
search_by_latlng(lat,lng)
lat (for example: lat = 30.032486)
lat is the input given by user which should be of numeric type
lng (for example: lng = 70.640244)
lng is the input given by user which should be of numeric type
# search by lat & lng library(httr) library(jsonlite) search_lat_lng_api_call<- function(lat,lng) { lat_lng <- paste(lat, ",", lng, sep = '' ) request<- httr::GET("https://maps.googleapis.com/maps/api/geocode/json", query = list(latlng = lat_lng, key = "AIzaSyBSfGtZ5DvujPUa1sZMmcdImaN8FmL3fqQ" ), encode = "json" ) r <- jsonlite::fromJSON(httr::content(request, "text")) return (r) } luid<- "samza595 and rabsh696" name<- "saman zahid & rabnawaz jansher" #' @title Google Place Search by lat lng #' @name search_by_latlng #' @param lat i.e is latitdue is the double type numeric value #' @param lng i.e is lngitutde is the double type numeric value #' @return result dataframe response #' @description search place by latitude and longitude api integration where you can get the location of any place by its latitude and longitude using geocoding api #' @references \url{https://developers.google.com/maps/documentation/geocoding/intro#ReverseGeocoding} #' @export search_by_latlng<- function(lat,lng) { if(!is.numeric(lat) || !is.numeric(lng) || !is.double(lat) || !is.double(lng) ) { stop() } response <- search_lat_lng_api_call(lat,lng) if(response$status == "OK") { result<- parse_lat_lng_response(response) return(result) } else if(response$status == "ZERO_RESULTS") { return("No match result found") } else if(response$status == "ZERO_RESULTS") { return("No match result found") } else if(response$status == "REQUEST_DENIED") { return("Api key expried Contact package owner") } else if(response$status == "OVER_QUERY_LIMIT") { return("your request limit is has been exceed") } } parse_lat_lng_response<- function(response) { len <- length(response$results$place_id) place <- c() i<- 1 for(x in response$results$address_components) { place[i] <- x[1]$long_name[1] i<- i + 1 if(i > len) break } i <- 1 type <- c() for(x in response$results$types) { type[i] <- x[1] i<- i +1 if(i > len) break } result <- data.frame( lat= response$results$geometry$location$lat, lng = response$results$geometry$location$lng, place = place, id= response$results$place_id, address = response$results$formatted_address, place_type= type ) return(result) }
result<-search_by_latlng(59.334591,18.063240) print(result)
install package geocodeApi
#devtools::install_github("rjkhan/RCourse-lab5")
it install a package from repository
Load a library geocodeApi #library(geocodeApi)
install a leaflet package#install.packages("leaflet")
Load a leaflet library #library(leaflet)
shiny::runGitHub(repo = "rjkhan/gApi")
Repo link"Here you can find a link for repo" (Package-repo ) (shiny-application-repo )
rabnsh696@student.liu.se samza595@student.liu.se Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.