Playing with Google Place API

Google Place API

I was playing around with the API to obtain lat-long for my geo analytics work.

I entered my credit card info but it seems that I’m not charged even with 9000+ API calls. Unsure if it’s because I’ve a 400+ dollars free cloud credit?

Anyway, what I did here was to make API calls and storing the data into my local database.

If you’re interested, you may visit this stackoverflow link (https://stackoverflow.com/questions/52565472/get-map-not-passing-the-api-key-http-status-was-403-forbidden/52617929#52617929) to understand how to set up the credentials.

#Load util function
source("connect_db.R")
vf_connect_db("google_place.db")

#load packages
sapply(c("ggmap", "RSQLite"), require, character.only = T)

#Google key information
register_google(key = "<intentionally left blank>") 

#Load dataframe into the memory
query = paste0('select TripAdv_Key, address from "trip_advisor_full_dataset_without_lat_long"')
origAddress = dbGetQuery(con, query)

# Loop through the addresses to get the latitude and longitude of each address and add it to the
# origAddress data frame in new columns lat and lon
for(i in 1:nrow(origAddress))
{
  print(i)
  tryCatch({
    
    #Geocoding based on address
    result <- geocode(origAddress$address[i], output = "latlona", source = "google")
    
    #Extracting results values and inserting into database
    TripAdv_Key = origAddress$TripAdv_Key[i] 
    lat = as.numeric(result[2])
    long = as.numeric(result[1])
    add = as.character(result[3])
    values = paste0("(",TripAdv_Key, ",", lat, ",", long, ",'", add,"')")
    query = paste0('INSERT INTO lat_long_info VALUES', values)
    dbSendQuery(con, query)
    
    #Print lat long
    print(c(lat, long))
  }, error=function(e){})
}

Related

comments powered by Disqus