R trying to find latitude/longitude data for cities in europe and getting geocode error messege

后端 未结 4 590
情书的邮戳
情书的邮戳 2021-02-04 09:46

I recently posted a question regarding plotting postions on cities in europe as points on a map. See R, get longitude/latitude data for cities and add it to my dataframe

4条回答
  •  闹比i
    闹比i (楼主)
    2021-02-04 10:23

    Here is an alternative way you could handle this.

    # METHOD 1: Using geocode() from {ggmap}
    library(ggmap)
    adr <- adr <- "Agra, New Delhi"  # define address
    geocode(adr)  # get the latitude and longitude
    
    # Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Agra,+New+Delhi&sensor=false
    #     lon      lat
    1 77.3126 28.54637
    
    # METHOD 2: CODE TO GET THE LATITUDE AND LONGITUDE OF A STREET ADDRESS WITH GOOGLE API
    addr <- '6th Main Rd, New Thippasandra, Bengaluru, Karnataka'  # set your address here
    url = paste('http://maps.google.com/maps/api/geocode/xml?address=', addr,'&sensor=false',sep='')  # construct the URL
    doc = xmlTreeParse(url) 
    root = xmlRoot(doc) 
    lat = xmlValue(root[['result']][['geometry']][['location']][['lat']]) 
    long = xmlValue(root[['result']][['geometry']][['location']][['lng']]) 
    lat
    [1] "12.9725020"
    long
    [1] "77.6510688"
    

提交回复
热议问题