Geo

The Geo object provides methods for translating between geo space and map space. An instance is created via the geo() method on a Unit Map instance.

You can also view a code example on codesandbox.io.

map.on("ready", function() {
  // Create a Geo instance via `map.geo()`, which can only be called once a unit map has loaded.

  var geo = map.geo();  
});

🚧

Georeference Support

This feature is only supported for georeferenced unit maps. When implementing, you can feature detect using the isSupported() method described in the documentation below.

Geo.isSupported()

If the loaded unit map is georeferenced, this method returns true, otherwise false.

if (geo.isSupported()) {
  console.log("The unit map is georeferenced.");
} else {
  console.log("The unit map is not georeferenced.");
}

Returns

A Boolean.

Geo.inBounds(latLng)

Determine if the given position is within the viewport (screen space).

if (geo.inBounds([32.9348319, -96.8533228])) {
  console.log("The position is within the viewport.");
} else {
  console.log("The position is outside the viewport.");
}

Arguments

ArgumentRequiredTypeDefault ValueDescription
latLngYesArrayundefinedAn array representing a latitude and longitude coordinate pair of a position in geo space.
Example: [32.9348319, -96.8533228].

Returns

A Boolean.

Geo.inMapBounds(latLng)

Determine if the given position is within the unit map's bounds.

if (geo.inMapBounds([32.9348319, -96.8533228])) {
  console.log("The position is within the unit map's bounds.");
} else {
  console.log("The position is outside the unit map's bounds.");
}

Arguments

ArgumentRequiredTypeDefault ValueDescription
latLngYesArrayundefinedAn array representing a latitude and longitude coordinate pair of a position in geo space.
Example: [32.9348319, -96.8533228].

Returns

A Boolean.

Geo.toLocal(latLng)

Translate the given position from geo space into map space.

// Translate the map to a `[latitude, longitude]` coordinate.

map.translate(geo.toLocal([32.9348319, -96.8533228]));

Arguments

ArgumentRequiredTypeDefault ValueDescription
latLngYesArrayundefinedAn array representing a latitude and longitude coordinate pair of a position in geo space.
Example: [32.9348319, -96.8533228].

Returns

An [x, y] coordinates Array or null if the loaded unit map is not georeferenced.

Geo.fromLocal(point)

Translate the given position from map space into geo space.

var mapElement = document.getElementById("map");

// Output the latitude, longitude of the current mouse position to the console.

mapElement.addEventListener("mousemove", function(event) {
  var latLng = geo.fromLocal(map.toLocal([event.pageX, event.pageY]))
  
  console.log(latLng);
});

Arguments

ArgumentRequiredTypeDefault ValueDescription
pointYesArrayundefinedAn array representing an x and y coordinate pair of a position in map space.
Example: [1, 5].

Returns

An [latitude, longitude] coordinates Array or null if the loaded unit map is not georeferenced.