Skip to content

ym.services.GeoCoder

Initialization

GeoCoder is initialized as follows:

var geocoder = new ym.services.GeoCoder();

Constructor(options)

The options parameter is not required. The constructor uses the standard definition from ym.provider.GeoJSON and extends it with some properties:

Name Description Data type Default value
channel A string that is logged; it can be freely chosen by the user. string

Methods

GeoCoder.geocode(location, options)

You can use this method to geocode an address as a location object. The following components of a postal address can be passed:

Parameter: location

Name Description Data type Default value
country Country string
district State or province string
zip Postal code string
city City string
cityPart City part string
cityAddon Additional information about the city or place string
street Street name string
houseNo House number string

Parameter: option

The options correspond to those of the constructor. By default, the options from the constructor and the parent class are used. If the options are set here, they overwrite the previously set options for this call.

Call

Code example: GeoCoder.geocode(location, options) Call Example
geocoder.geocode({
    zip: "76131",
    city: "ka"
},
{
    coordFormat: "GEODECIMAL_POINT",
    locale: "de-DE",
    channel: "mobile device"
});

GeoCoder.geocodeString(singleSlot, options)

This method is similar to the geocode() method. The difference is that a free text can be passed.

Parameter: singleSlot

All address information is passed as single-line information, for example, "CAS-Weg 4, 76131 Karlsruhe" (comma optional). The method geocodeString() is especially useful if there is a single text input field on the page where an address is to be entered.

Parameter: option

The options correspond to those of the constructor. By default, the options from the constructor and the parent class are used. If the options are set here, they overwrite the previously set options for this call.

Call

Code example: GeoCoder.geocodeString(singleSlot, options) Example call
geocoder.geocodeString("Marktplatz, Karlsruhe");

GeoCoder.reverseGeocode(latLng, options)

Parameter: latLng

Allowed data types are: ym.modules.provider.LatLng, {lat:Number, lng:Number} or an array in format [latitude, longitude].

Parameter: options

The options correspond to those of the constructor. By default, the options from the constructor and the parent class are used. If the options are set here, they overwrite the previously set options for this call.

Call

Code example: GeoCoder.reverseGeocode(latLng, options) Example call
geocoder.reverseGeocode({lat: 48.9941241, lng: 8.3510868});
geocoder.reverseGeocode([48.9941241, 8.3510868]);
geocoder.reverseGeocode(ym.latLng(48.9941241, 8.3510868));
geocoder.reverseGeocode(new ym.modules.provider.LatLng(48.9941241, 8.3510868));

Events

To track a geocoding, the Geocoder is equipped with an event emitter that supports two events:

Code example: Events
geocoder.on('success', function (request, response) {
    if (response.body) {
        var geoJson = ym.geoJson(response.body);
        geoJson.addTo(map);
    }
});

geocoder.on('error', function (request, response) {
    console.log(response.xhr);
    console.log(response.event);
});