Learn R Programming

geoChina (version 1.3.0)

geocode: Geocode

Description

geocodes an address using Google or Baidu Maps Geocoding API. Note that in most cases by using this function you are agreeing to the Google Maps API Terms of Service at https://developers.google.com/maps/terms or the Baidu Maps API Terms of Use at http://developer.baidu.com/map/law.htm.

Usage

geocode(address, api = c("google", "baidu"), key = "", ocs = c("WGS-84",
  "GCJ-02", "BD-09"), output = c("latlng", "latlngc", "latlnga"),
  messaging = FALSE, time = 0)

Arguments

address
a character vector specifying a location of interest (e.g., "Beijing Railway Station").
api
use Google or Baidu Maps Geocoding API. When using Baidu Maps Geocoding API, the address must be in Chinese.
key
an API key must be provided when calling Baidu Maps Geocoding API. When calling Google Maps Geocoding API, you'd better provide an API key, though it is not necessary.
ocs
output coordinate systems including WGS-84, GCJ-02 and BD-09, which are the GCSs of Google Earth, Google Map in China and Baidu Map, respectively. For address out of China, ocs is automatically set to 'WGS-84' and other values are igored.
output
lat/lng coordinates or lat/lng coordinates with location type (Goolge Map) | confidence #' (Baidu Map) or lat/lng coordinates with formated address and address components (only available for #' Google Map API).
messaging
turn messaging on/off. The default value is FALSE.
time
the time interval to geocode, in seconds. Default value is zero. When you geocode multiple addresses, set a proper time interval to avoid exceeding usage limits. For details see https://developers.google.com/maps/documentation/geocoding/usage-limits

Value

a data.frame with variables lat/lng or lat/lng/conf

Details

note that the google maps api limits to 2,500 free requests per day and 50 requests per second.

See Also

revgeocode, geohost. Google Maps API at https://developers.google.com/maps/documentation/geocoding/ and Baidu Maps API at http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding

Examples

Run this code
## Not run: ------------------------------------
# geocode('Beijing Railway Station', api = 'google', ocs = 'GCJ-02')
# geocode('Beijing Railway Station', api = 'google', ocs = 'WGS-84', 
#         messaging = TRUE)
# geocode('Beijing Railway Station', api = 'google', ocs = 'WGS-84', 
#         output = 'latlngc')
# geocode('Beijing Railway Station', api = 'google', ocs = 'WGS-84', 
#         output = 'latlnga')
# geocode(c('Beijing Railway Station', 'Beijingxi Railway Station'), api = 'google', 
#         ocs = 'GCJ-02')
# geocode(c('Beijing Railway Station', 'Beijingxi Railway Station'), api = 'google', 
#         ocs = 'WGS-84', output = 'latlngc', messaging = TRUE)
# geocode(c('Beijing Railway Station', 'Beijingxi Railway Station'), api = 'google', 
#         ocs = 'WGS-84', output = 'latlnga', messaging = TRUE)
# geocode(c('Beijing Railway Station', 'Beijingxi Railway Station'), api = 'google', 
#         ocs = 'WGS-84', output = 'latlngc', messaging = TRUE, time = 2)
# geocode('Beijing Railway Station', api = 'baidu', key = 'your baidu maps api key', 
# ocs = 'BD-09')
# geocode('Beijing Railway Station', api = 'baidu', key = 'your baidu maps api key', 
# ocs = 'GCJ-02', messaging = TRUE)
# geocode('Beijing Railway Station', api = 'baidu', key = 'your baidu maps api key', 
# ocs = 'BD-09', output = 'latlngc')
# geocode(c('Beijing Railway Station', 'Beijingxi Railway Station'), api = 'baidu', 
# key = 'your baidu maps api key', ocs = 'BD-09')
# geocode(c('Beijing Railway Station', 'Beijingxi Railway Station'), api = 'baidu', 
# key = 'your baidu maps api key', ocs = 'WGS-84', output = 'latlngc')
## ---------------------------------------------

Run the code above in your browser using DataLab