Skip to content

Commit

Permalink
updated code & docs for issue SymbolixAU#167
Browse files Browse the repository at this point in the history
  • Loading branch information
SymbolixAU committed Jun 28, 2018
1 parent cbd1435 commit 0e00d8d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 38 deletions.
71 changes: 39 additions & 32 deletions R/google_places.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
#' @param radius \code{numeric} Defines the distance (in meters) within which to
#' return place results. Required if only a \code{location} search is specified.
#' The maximum allowed radius is 50,000 meters. Radius must not be included if
#' \code{rankby="distance"} is specified. see Details.
#' \code{rankby} is used. see Details.
#' @param rankby \code{string} Specifies the order in which results are listed.
#' Possible values are \code{"prominence"}, \code{"distance"} or \code{"location"}.
#' Possible values are \code{"prominence"} or \code{"distance"}.
#' If \code{rankby = distance}, then one of \code{keyword}, \code{name} or
#' \code{place_type} must be specified. If a \code{search_string} is used then
#' \code{rankby} is ignored.
Expand Down Expand Up @@ -138,22 +138,23 @@
#' }
#' @export
#'
google_places <- function(search_string = NULL,
location = NULL,
radar = FALSE,
radius = NULL,
rankby = NULL,
keyword = NULL,
language = NULL,
name = NULL,
place_type = NULL,
price_range = NULL,
open_now = NULL,
page_token = NULL,
simplify = TRUE,
curl_proxy = NULL,
key = get_api_key("places")
){
google_places <- function(
search_string = NULL,
location = NULL,
radar = FALSE,
radius = NULL,
rankby = NULL,
keyword = NULL,
language = NULL,
name = NULL,
place_type = NULL,
price_range = NULL,
open_now = NULL,
page_token = NULL,
simplify = TRUE,
curl_proxy = NULL,
key = get_api_key("places")
) {

## check if both search_string & location == NULL
if(is.null(search_string) & is.null(location))
Expand Down Expand Up @@ -182,7 +183,7 @@ google_places <- function(search_string = NULL,
## construct the URL
## if search string is specified, use the 'textsearch' url
## if no search_string, use the 'lat/lon' url
if(isTRUE(radar)){
if (isTRUE( radar )) {
map_url <- "https://maps.googleapis.com/maps/api/place/radarsearch/json?"
}else{
if(!is.null(search_string)){
Expand All @@ -193,19 +194,25 @@ google_places <- function(search_string = NULL,
}
}

map_url <- constructURL(map_url, c("location" = location,
"radius" = radius,
"rankby" = rankby,
"keyword" = keyword,
"language" = language,
"name" = name,
"type" = place_type,
"minprice" = price_range[1],
"maxprice" = price_range[2],
"opennow" = open_now,
"pagetoken" = page_token,
"key" = key))
map_url <- constructURL(
map_url
, c("location" = location
, "radius" = radius
, "rankby" = rankby
, "keyword" = keyword
, "language" = language
, "name" = name
, "type" = place_type
, "minprice" = price_range[1]
, "maxprice" = price_range[2]
, "opennow" = open_now
, "pagetoken" = page_token
, "key" = key
)
)

return(downloadData(map_url, simplify, curl_proxy))
return(
downloadData(map_url, simplify, curl_proxy)
)

}
21 changes: 17 additions & 4 deletions R/parameter_checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -371,12 +371,25 @@ validateHeading <- function(heading){
validateLocationSearch <- function(location, search_string, radius, rankby, keyword, name, place_type){
if(is.null(location)) return(NULL)

## if using rankby="distance"
if(!is.null(rankby)) {
if(rankby == "distance") {
if(!is.null(radius)) {
stop("If using rankby, radius must not be specified")
}
}
}

if(!is.null(radius) && !is.null(rankby)) {
stop("rankby must not be included if radius is specified")
}

## radius must be included if using a location search
if(is.null(search_string) & !is.null(location) & is.null(radius))
if(is.null(search_string) && is.null(radius) & is.null(rankby))
stop("you must specify a radius if only using a 'location' search")

## if rankby == distance, then one of keyword, name or place_type must be specified
if(!is.null(rankby) & !is.null(location)){
if(!is.null(rankby)){
if(rankby == "distance" &
is.null(keyword) & is.null(name) & is.null(place_type))
stop("you have specified rankby to be 'distance', so you must provide one of 'keyword','name' or 'place_type'")
Expand Down Expand Up @@ -517,8 +530,8 @@ validateRankBy <- function(rankby, location, search_string){

## rankby has correct arguments
if(!is.null(location))
if(!rankby %in% c("prominence","distance","location"))
stop("rankby must be one of either prominence, distance or location")
if(!rankby %in% c("prominence","distance"))
stop("rankby must be one of either prominence or distance")

## warning if rankby used with search_string
if(!is.null(search_string))
Expand Down
4 changes: 2 additions & 2 deletions man/google_places.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0e00d8d

Please sign in to comment.