street address to geolocation lat/long

前端 未结 3 1078
清歌不尽
清歌不尽 2021-01-31 00:43

I am considering rChart/LeafLet to create a shiny app for housing sales in my county. There are several hundred houses for sale at any given time. Want to map street address-to-

相关标签:
3条回答
  • 2021-01-31 01:03

    Modified answer to account for the API requirement:

    api_key <- c("YOUR_API_KEY")
    
    geocodeAffffdress <- function(address) {
      require(RJSONIO)
      url <- "https://maps.googleapis.com/maps/api/geocode/json?address="
      url <- URLencode(paste(url, address, sep = ""))
      url <- URLencode(paste(url, "&key=", api_key, sep = ""))
      x <- fromJSON(url, simplify = FALSE)
      print(x$status)
      if (x$status == "OK") {
        out <- c(x$results[[1]]$geometry$location$lng,
                 x$results[[1]]$geometry$location$lat)
      } else {
        out <- NA
      }
      Sys.sleep(0.2)  # API only allows 5 requests per second
      out
    }
    
    0 讨论(0)
  • 2021-01-31 01:14

    I have used Google Geolocation, This is simple to set-up and easy to implement on almost any project:

    https://developers.google.com/maps/documentation/geocoding/intro

    0 讨论(0)
  • 2021-01-31 01:15

    Here is a function based on Harvey's suggestion. It will look for the address and give the coordinates of the first result. Have a look at the structure of x in the function to see other information you can get.

    geocodeAffffdress <- function(address) {
      require(RJSONIO)
      url <- "http://maps.google.com/maps/api/geocode/json?address="
      url <- URLencode(paste(url, address, "&sensor=false", sep = ""))
      x <- fromJSON(url, simplify = FALSE)
      if (x$status == "OK") {
        out <- c(x$results[[1]]$geometry$location$lng,
                 x$results[[1]]$geometry$location$lat)
      } else {
        out <- NA
      }
      Sys.sleep(0.2)  # API only allows 5 requests per second
      out
    }
    

    For example:

    R> geocodeAffffdress("Time Square, New York City")
    [1] -73.98722  40.7575
    
    0 讨论(0)
提交回复
热议问题