getting results from "place_type" using googleway::access_results()

56 Views Asked by At

i've been using successfully for almost a year the followig code to retrieve places from googgle maps using googleway R package.

Recently i've been asked to retrieve also the place_type info from the API results, It looks to me as simple as adding access_result(res, "place_type") and declare it in following functions.

line 11= googleway::access_result(res, "place_type") ##added to get place type

line 13 = , c("lat", "lon", "name", "place_id", "place_type") ##addedd place_type

line 42 = , place_type = vector("character", 0L) ##added place_type

But the results returns error. :/

Do someone else has any experience with this kind of problem. Any clue to solve it?

Thanks in advance from Guatemala

library(googleway)
library(tidyverse)
library(plyr)

format_res <- function(res) {
  setNames(
    cbind(
      googleway::access_result(res, "coordinates"),
      googleway::access_result(res, "place_name"),
      googleway::access_result(res, "place"), ## store the unique place_id as well
      googleway::access_result(res, "place_type") ##added to get place type
    )
    , c("lat", "lon", "name", "place_id", "place_type") ##addedd place_type
  )
}

do_search <- function(search_string, key, location, radius, page_token = NULL) {
  
  google_places(
    search_string = search_string,
    location = location,
    key = key,
    radius = radius,
    page_token = page_token
  )
  
}

full_search <- function(search_string, key, location, radius) {
  
  counter <- 0
  
  page_token <- NULL ## can start on NULL because it means we're doing the first query
  is_another_page <- TRUE 
  
  # initialise a data.frame to store the results
  df <- data.frame(
    lat = vector("numeric", 0L)
    , long = vector("numeric", 0L)
    , name = vector("character", 0L)
    , place_id = vector("character", 0L)
    , place_type = vector("character", 0L) ##added place_type
  )
  
  while( is_another_page ) {
    
    res <- do_search(search_string, key, location, radius, page_token)
    
    if( res$status == "OK" ) { ## check a valid result was returned
      
      if( counter == 0 ) {
        df <- format_res( res )
      } else {
        df <- rbind(df, format_res( res ) )
      }
      
      counter <- counter + 1
    } else {
      # print a message for not-OK results
      print(paste0(res[["status"]], " for ", paste0(location, collapse = ", ") ))
    }
    
    page_token <- res[["next_page_token"]]
    is_another_page <- !is.null( page_token )
    Sys.sleep(3)  ## Sleep the function before the next call because there's a time limit
  }
  return(df)
}

# I've added a 3rd example that actually has results
dfCoords <- data.frame(
  coords = c("14.8494558471176, -91.626666651125", "14.8725498686528, -91.626666651125", "14.8610028578852, -91.6066666418")
)

key <- c("myAPIkey")
search_string <- "searchTerm" 
radius <- 1500

# create a list to store the results
lst_results <- vector("list", length = nrow(dfCoords))
# Using a list will be more efficient that `rbind`-ing a data.frame in each iteration

# loop through the indexes of the coordinates
# this wy we can assign the results to the correct index of the list
for (i in 1:nrow(dfCoords) ) {
  
  location <- dfCoords[i, "coords"]
  
  # the coordiantes must be a numeric vector
  location <- as.numeric(strsplit(location, ",")[[1]])
  
  
  lst_results[[ i ]] <- full_search(
    search_string = search_string
    , key = key
    , location = location
    , radius = radius
  )
}

lapply(lst_results, head)

#


lst_results <- lst_results[lengths(lst_results) != 0]

dataJoin <- dplyr::bind_rows(lst_results)



#write.csv(dataJoin, file = "D:/home/results.csv")
0

There are 0 best solutions below