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
You have to geocode just the cities
column (it's a little confusing that you have a data frame called cities
, and within it a column called cities
). When in doubt, try breaking things down into smaller chunks.
For example, try them one at a time ...
cities <- c("ARNHEM","ATHENS","BAAR","CAMBRIDGESHIRE")
library(ggmap)
geocode(cities[1])
## lon lat
## 1 5.89873 51.9851
geocode(cities[2])
## just checking ...
geocode("ATHENS GEORGIA")
## lon lat
## 1 -83.38333 33.95
Now try the vector all at once:
geocode(cities)
## lon lat
## 1 5.8987296 51.98510
## 2 23.7293097 37.98372
## 3 8.5286332 47.19585
## 4 0.0965375 52.27619
Now try with a data frame:
mydat <- read.csv(textConnection("
cities,Freq,lon,lat
ARNHEM,1.00,NA,NA
ATHENS,3.25,NA,NA
BAAR,1.00,NA,NA
BAD VILBEL,1.00,NA,NA
BILTHOVEN,1.00,NA,NA
BOGUS_PLACE,2,NA,NA"))
geocodes <- geocode(as.character(mydat$cities))
mydat <- data.frame(mydat[,1:2],geocodes)
## cities Freq lon lat
## 1 ARNHEM 1.00 5.898730 51.98510
## 2 ATHENS 3.25 23.729310 37.98372
## 3 BAAR 1.00 8.528633 47.19585
## 4 BAD VILBEL 1.00 8.739480 50.18234
## 5 BILTHOVEN 1.00 5.210381 52.13653
## 6 BOGUS_PLACE 2.00 -92.201158 44.49091
I don't know what the result for BOGUS_PLACE
means ...!!
You can use below code to extract information from Bing Map API
for(i in 1:length(PinCode)){
var = PinCode[i]
link=paste("http://dev.virtualearth.net/REST/v1/Locations?postalCode=", var, "&o=xml&maxResults=1&key=[YOurKey]",sep = "")
data<- xmlParse(link)
xml_data <- xmlToList(data)
PinCodeLatLongtemp <- data.frame(PinCode = "Temp", Lat = "Lat", Long = "Long")
PinCodeLatLongtemp$PinCode <- var
PinCodeLatLongtemp$Lat <-
xml_data$ResourceSets$ResourceSet$Resources$Location$Point$Latitude
PinCodeLatLongtemp$Long <-
xml_data$ResourceSets$ResourceSet$Resources$Location$Point$Longitude
PinCodeLatLong <- rbindlist(list(PinCodeLatLongtemp,PinCodeLatLong), fill = T)
}
It will create a new dataframe with you input 'Pincode' and Two new columns with Lat and Long. YOu can get your key from [here] (https://www.bingmapsportal.com)
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"
I have just found that this error message:
Error: is.character(location) is not TRUE
can be due to the address being encoded as a number, not a character. This can happen when you select from a data frame for instance, which was my case.
Do:
typeof(address)
and if it turns out to be numeric, change it to char
a2 <- as.character(address)
geocode(a2)