Calling the Geocode/Reverse Geocode APIs using Geocoder
Using the Geocoder submodule, you can call the geocode (converting addresses to coordinates) and reversegeocode (converting coordinates to addresses) APIs.
The APIs can be normally called only on the page where the registered Client Id and Web service URL match.
For examples of using the geocode and reversegeocode APIs, refer to Using the geocode/reverse geocode APIs.
Load submodules
To use Geocoder, you should load the JavaScript file of the Geocoder submodule. Load the file as in the code below.
This submodule requires the NAVER Maps API v3 to be loaded in advance. For more information, refer to Submodule System.
The NAVER Maps API v3 supports both
http
andhttps
protocols.
<script type="text/javascript" src="https://oapi.map.naver.com/openapi/v3/maps.js?ncpClientId=YOUR_CLIENT_ID&submodules=geocoder"></script>
naver.maps.Service object
For the server API service, use the naver.maps.Service static object.
The Service object provides parameters and methods required to call the server API.
Server API calls made by using the NAVER Maps API v3
arelimited by the daily quota
of each application.
CoordType object
CoordType object represents coordinate system names to be used for server API calls and results.
Available coordinate systems are listed below:
- LATLNG: Latitude and longitude coordinate system
Default
- TM128: TM128 coordinate system
Encoding object
Encoding object represents the text encoding type used for server API calls and results.
Available text encoding types are listed below:
- UTF_8: utf-8 encoding (
default
) - EUC_KR: euc-kr encoding
Status object
Status object represents the status of the server API call results.
- OK: The API request was successful.
- ERROR: The API request failed.
Status is passed as the first parameter of the callback function passed when the server API is called, and is used to decide whether the API request was successful or not.
naver.maps.Service.geocode({ address: 'unknown address' }, function(status, response) {
if (status === naver.maps.Service.Status.ERROR) {
return alert('Something wrong!');
}
// Handle a response when successful
});
ServiceOptions
ServiceOptions defines default parameters to pass when the server API is called.
You can define coordType and encoding parameters, and use the CoordType and Encoding objects as a parameter value.
Parameter | Default | Description |
---|---|---|
coordType | naver.maps.Service.CoordType.LATLNG | Coordinate system name |
encoding | naver.maps.Service.Encoding.UTF_8 | Encoding of the service response |
Call the geocode API
Using the Geocoder submodule, you can call the geocode API, which uses an address to search for coordinates.
You can use the naver.maps.Service.geocode or naver.maps.Service.fromAddrToCoord method to call the API.
Pass options which adds the address
parameter to a ServiceOptions object as the first parameter, where the address
option is required.
The
address
parameter is a character string of the address to search.
The second parameter is the callback function to handle the result.
The fromAddrToCoord also calls the geocode method internally, and works the same way.
naver.maps.Service.geocode({
address: '6, Buljeong-ro'
}, function(status, response) {
if (status !== naver.maps.Service.Status.OK) {
return alert('Something wrong!');
}
var result = response.result, // Container of the search result
items = result.items; // Array of the search result
// do Something
});
If the request is successful, a json object response as in the code below is passed as the second parameter of the callback function.
{
"result": {
"total": 1,
"userquery": "6, Buljeong-ro",
"items": [
{
"address": "Green Factory, 6, Buljeong-ro, Bundang-gu, Seongnam-si, Gyeonggi-do",
"addrdetail": {
"country": "Republic of Korea",
"sido": "Gyeonggi-do",
"sigugun": "Bundang-gu, Seongnam-si",
"dongmyun": "Buljeong-ro",
"rest": "Green Factory, 6"
},
"isRoadAddress": true,
"point": {
"x": 127.1052133,
"y": 37.3595316
}
}
]
}
}
For more information on response types, refer to GeocodeResponse.
Server API calls made by using the NAVER Maps API v3 are
limited by the daily quota
of each application.
Call the reversegeocode API
Using the Geocoder submodule, you can call the reversegeocode API, which uses coordinates to search for an address.
You can use the naver.maps.Service.reverseGeocode or naver.maps.Service.fromCoordToAddr method to call the API.
Pass options which adds the location
parameter to a ServiceOptions object as the first parameter, where the location
option is required.
The
location
parameter is coordinates to search, which is passed as a Coord object or CoordLiteral.
The second parameter is the callback function to handle the result.
The fromCoordToAddr also calls the reverseGeocode method internally, and works the same way.
naver.maps.Service.reverseGeocode({
location: new naver.maps.LatLng(37.3595316, 127.1052133),
}, function(status, response) {
if (status !== naver.maps.Service.Status.OK) {
return alert('Something wrong!');
}
var result = response.result, // Container of the search result
items = result.items; // Array of the search result
// do Something
});
If the request is successful, a json object response as in the code below is passed as the second parameter of the callback function.
{
"result": {
"total": 2,
"userquery": "127.1052133,37.3595316",
"items": [
{
"address": "178-1, Jeongja-dong, Bundang-gu, Seongnam-si, Gyeonggi-do",
"addrdetail": {
"country": "Republic of Korea",
"sido": "Gyeonggi-do",
"sigugun": "Bundang-gu, Seongnam-si",
"dongmyun": "Jeongja-dong",
"rest": "178-1"
},
"isRoadAddress": false,
"point": {
"x": 127.1052208,
"y": 37.3595122
}
},
{
"address": "Green Factory, 6, Buljeong-ro, Bundang-gu, Seongnam-si, Gyeonggi-do",
"addrdetail": {
"country": "Republic of Korea",
"sido": "Gyeonggi-do",
"sigugun": "Bundang-gu, Seongnam-si",
"dongmyun": "Buljeong-ro",
"rest": "Green Factory, 6"
},
"isRoadAddress": true,
"point": {
"x": 127.1052133,
"y": 37.3595316
}
}
]
}
}
For more information on response types, refer to ReverseGeocodeResponse.
Server API calls made by using the NAVER Maps API v3 are
limited by the daily quota
of each application.
Examples: Using the geocode/reverse geocode APIs