Extract address components from coordiantes

后端 未结 3 616
有刺的猬
有刺的猬 2021-01-27 05:15

I\'m trying to reverse geocode with R. I first used ggmap but couldn\'t get it to work with my API key. Now I\'m trying it with googleway.

newframe[,c("Front         


        
3条回答
  •  感动是毒
    2021-01-27 05:48

    Google's API returns the response in JSON. Which, when translated into R naturally forms nested lists. Internally in googleway this is done through jsonlite::fromJSON()

    In googleway I've given you the choice of returning the raw JSON or a list, through using the simplify argument.

    I've deliberately returned ALL the data from Google's response and left it up to the user to extract the elements they're interested in through usual list-subsetting operations.

    Having said all that, in the development version of googleway I've written a few functions to help accessing elements of various API calls. Here are three of them that may be useful to you

    ## Install the development version
    # devtools::install_github("SymbolixAU/googleway")
    
    res <- google_reverse_geocode(
      location = c(df[1, 'Front.lat'], df[1, 'Front.long']), 
      key = apiKey
      )
    
    geocode_address(res)
    # [1] "45 Clarke St, Southbank VIC 3006, Australia"                    
    # [2] "Bank Apartments, 275-283 City Rd, Southbank VIC 3006, Australia"
    # [3] "Southbank VIC 3006, Australia"                                  
    # [4] "Melbourne VIC, Australia"                                       
    # [5] "South Wharf VIC 3006, Australia"                                
    # [6] "Melbourne, VIC, Australia"                                      
    # [7] "CBD & South Melbourne, VIC, Australia"                          
    # [8] "Melbourne Metropolitan Area, VIC, Australia"                    
    # [9] "Victoria, Australia"                                            
    # [10] "Australia"
    
    geocode_address_components(res)
    #        long_name short_name                                  types
    # 1             45         45                          street_number
    # 2  Clarke Street  Clarke St                                  route
    # 3      Southbank  Southbank                    locality, political
    # 4 Melbourne City  Melbourne administrative_area_level_2, political
    # 5       Victoria        VIC administrative_area_level_1, political
    # 6      Australia         AU                     country, political
    # 7           3006       3006                            postal_code
    
    geocode_type(res)
    # [[1]]
    # [1] "street_address"
    # 
    # [[2]]
    # [1] "establishment"      "general_contractor" "point_of_interest" 
    # 
    # [[3]]
    # [1] "locality"  "political"
    # 
    # [[4]]
    # [1] "colloquial_area" "locality"        "political"  
    

提交回复
热议问题