Point, Location, and Coordinate

tmcw edited this page Apr 27, 2012 · 6 revisions

Converting Point/Location/Coordinate

To convert from geographic Locations to on screen Points, use map.pointLocation and map.locationPoint:

// Dijbouti
var l1 = new MM.Location(11.7, 42.5);

// Dijbouti on screen:
var p1 = map.locationPoint(l1);

// some pixels in the map:
var p2 = new MM.Point(100, 100);

// the geographic location of p2:
var l2 = map.pointLocation(p2);

The 'points' that these functions expect are based on the distance from the point to the top-left corner of the map element - map.parent.

There are also equivalent internal functions for converting to/from coordinates: coordinatePoint(coord), coordinateLocation(coord), locationCoordinate(location), and pointCoordinate(point).


Points are places on your screen, within the map element. They are typically not geographic. Modest Maps also uses the point datatype to represent sizes: the third argument to the MM.Map constructor is a point in the form of new MM.Point(width, height).


var point = new MM.Point(x, y)

Where you can provide Point objects, you can usually also provide simple objects with x and y attributes:

var point = { x: 10, y: 20 };

Class Methods

// Get a point between two other points, biased by `t`.
var p3 = MM.Point.interpolate(p1, p2, t)

// Get the euclidean distance between two points
var d = MM.Point.distance(p1, p2);

Point is a simple data object with x and y properties. It's most often used to represent pixel positions on screen or within the map.

var p1 = new MM.Point(10, 20);
var p2 = new MM.Point(20, 20);

// d is 10
var d = MM.Point.distance(p1,p2);

// p3 is half way between p1 and p2: (15,20)
var amount = 0.5;
var p3 = MM.Point.interpolate(p1,p2,amount);


Locations are places in the world: they are in latitude and longitude. If you were to store markers, you'd store their locations as locations, and only convert them into points in order to position them on the map element.


var loc = new MM.Location(latitude, longitude);

Where you can provide Location objects, you can also provide simple objects with lat and lon attributes:

var loc2 = { lat: 37, lon: -120 };

Class Methods

// Get the distance between two points, in
// meters, with a sphere of radius r meters (default 6378000)
var d = MM.Location.distance(l1, l2, r)

// Get the interpolation of two points, biased by f,
// along the great circle
var l3 = MM.Location.interpolate(l1, l2, f)

Location is similar to Point, but for geographic coordinates. It has lat and lon properties (and expects them in that order, unlike Point which works the other way around). It represents locations on the surface of the earth in degrees latitude and longitude. You can use a site like getlatlon.com to find the latitude and longitude of places by name.

// Create a new location at 30 latitude, 114 longitude (Xinjiezhen)
var loc = new MM.Location(30, 114);