Class: IWGeoLocationUtils

IWGeoLocationUtils

A class to simplify usage of the Javascript geolocation API.

The geolocation API provides functions to locate a computer or a mobile device by various methods, e.g. GPS, WLAN, or the IP address of the device. The API is available in all current browsers for desktops, Android and iOS devices. For details see the specification and Wikipedia entry.

This class provides methods to easily track the location on a map and visualize the results on the screen.

Options used by the methods of this class:

Property Description Type Default
updateMap updates the map to the location of the user boolean true
showRadius draws a circle to visualize the location and accuracy boolean true
maxDrawingRadius the maximum radius (i.e. accuracy) in meters to draw the circle or 0 to always draw number 0
defaultCoordinate a coordinate to use if no geolocation is possible IWCoordinate show germany
defaultZoom a zoom level to use if no geolocation is possible number show germany
timeout a timeout in milliseconds to wait for a geolocation result number 300 ms
enableHighAccuracy use high accuracy like GPS if possible boolean true
attributes rendering attributes for the circle object { fill: 'blue', fillOpacity: 0.2, strokeWidth: 3, stroke: 'blue', strokeOpacity: 0.6 }

Example:

if (IWGeoLocationUtils.isAvailable())
{
    IWGeoLocationUtils.locate(map, {enableHighAccuracy: false, timeout: 2000, showRadius: true, maxDrawingRadius: 100});
}

This class is part of the module navigation.

This class requires the package graphics to render the radius. See the section about modules in the FAQ for details.


new IWGeoLocationUtils()

Singleton constructor

Methods


findAddress(options)

Finds the nearest address at the users location.

Triggers one of the events onlocationfound and onerror. If the location could be determined, the event object will contain the coordinate, the accuracy, the nearest address and the distance between the coordinate and the address.

If the geolocation API is not available or the user gave no permission to use the location, the error handler is called.

Parameters:
Name Type Description
options object options as described above
Returns:
Type
void

isAvailable()

Checks whether geolocation functionality is available or not.
Returns:
Type
boolean

locate(map, options)

Locates the user on the map. Triggers one of the events onpositionchange and onerror.

Example:

// set the map to the geo location if a result is found in 500 ms
IWGeolocationUtils.locate(map, { updateMap: true, showRadius: false, timeout: 500, enableHighAccuracy: false });
Parameters:
Name Type Description
map IWMap a map
options object options as described above
Returns:
Type
void

stopTracking()

Stops tracking the user. This stops map updates and onpositionchange events.
Returns:
Type
void

track(map, options)

Tracks the user and updates the map every time a new location is found (unless stopTracking() is called). Everytime there is a new location a new onpositionchange event will be fired.

Example:

// draw a red circle every time there is a new location with an accuracy of at least 100 meters
var options = {
	updateMap: false,
	enableHighAccuracy: true,
	showRadius: true,
	maxDrawingRadius: 100
	attributes: { fill: 'red', fillOpacity: 0.2, strokeWidth: 3, stroke: 'red', strokeOpacity: 0.6 }
};
IWGeoLocationUtils.track(map, options);
Parameters:
Name Type Description
map IWMap a map
options object options as described above
Returns:
Type
void