Positioning

The user's location is one of the key pieces of information in building a Web map application. The HERE Maps API for JavaScript supports the use of W3C browser positioning, making it easy for an application to use positioning information.

Precision depends on the positioning technology and ranges from rough IP Positioning (only supported in a few browsers such as Mozilla Firefox), cell positioning (usually available on tablets and notebooks) up to very precise WiFi detection (all WiFi-enabled modern browsers, among them mobile browsers). In addition, The API supports GPS or A-GPS on tablets and smart phones, which delivers very accurate positioning.

Visual presentation of positioning data on the map includes proximity rendering.

Example

The example below demonstrates the use of W3C geolocation API to obtain positioning information (location) and to display it as a marker on the map. A positioning request is submitted if the application user's mouse hover over the map. On success, a message confirms this and, in addition, the map displays a marker in a circle that reflects the accuracy of the positioning information. On failure, the application displays an error message. The image below shows the success scenario, with a marker and the accuracy circle marking the detected location.

Figure 1. Using the W3C geolocation API (only works in browsers that support this functionality)

As in all the previous examples, the code creates an instance of map Display, but it also obtains an object representing the positioning Manager. It also defines a callback function named getPos(), which obtains the current geographic position from the browser and then creates a StandardMarker plus the accuracy circle and sets the map zoom level to ensure the entire circle is visible. The callback is associated with the "mouseover" event for the HTML element that contains the map.

var map = new nokia.maps.map.Display(
	document.getElementById("mapContainer"), {
		components: [new nokia.maps.map.component.Behavior()],
		zoomLevel: 13,
		center: [52.51, 13.4]
});

if (nokia.maps.positioning.Manager) {
	var positioning = new nokia.maps.positioning.Manager();

	// Get the current position. If available, the first callback is run,
	// otherwise the second.
	positioning.getCurrentPosition(
		function (position) {
			var coords = position.coords;
			var marker =
				new nokia.maps.map.StandardMarker(coords);
			var accuracyCircle =
				new nokia.maps.map.Circle(coords, coords.accuracy);
			map.objects.addAll([accuracyCircle, marker]);
			map.zoomTo(accuracyCircle.getBoundingBox(), false, "default");
		},
		// Handle errors (display message):
		function (error) {
			var errorMsg = "Location could not be determined: ";

			// Determine what caused the error and show error message:
			if (error.code == 1)
				errorMsg += "PERMISSION_DENIED";
			else if (error.code == 2)
				errorMsg += "POSITION_UNAVAILABLE";
			else if (error.code == 3)
				errorMsg += "TIMEOUT";
			else errorMsg += "UNKNOWN_ERROR";

			alert(errorMsg);
		};
	)
}