Skip to main content

LatLng

The LatLng object represents a latitude and longitude pair.

This is used internally such as when setting the center point for a map, or a marker location.

LatLng extends Base.

Usage example​

const latLng = G.latLng(41.3874, 2.1686)

Creating the LatLng object​

G.latLng(latitude?: LatLngValue | number | string, longitude?: number | string): LatLng

There are a few ways to setup the LatLng object.

No parameters.

G.latLng(): LatLng

const latLng = G.latLng();

Pass the latitude and longitude values.

G.latLng(latitude: number, longitude: number): LatLng

ParameterTypeRequiredDescription
latitudenumberYesThe latitude value.
longitudenumberYesThe longitude value.
const latLng = G.latLng(41.3874, 2.1686);

Pass the latitude and lontitude values as an array.

G.latLng([latitude: number, longitude: number]): LatLng

ParameterTypeRequiredDescription
valueArrayYesAn array containing the latitude and longitude values
const latLng = G.latLng([41.3874, 2.1686]);

Pass the latitude and lontitude values as an object.

G.latLng({lat: latitude: number, lng: longitude: number}): LatLng

ParameterTypeRequiredDescription
valueobjectYesAn object containing the latitude and longitude values
const latLng = G.latLng({lat: 41.3874, lng: 2.1686});

G.latLng({latitude: latitude: number, longitude: longitude: number}): LatLng

ParameterTypeRequiredDescription
valueobjectYesAn object containing the latitude and longitude values
const latLng = G.latLng({latitude: 41.3874, longitude: 2.1686});

Pass an existing LatLng object.

G.latLng(latLngObject: LatLng): LatLng

In this case a new LatLng object with the same latitude and longitude values is returned.

ParameterTypeRequiredDescription
latLngObjectLatLngYesA LatLng object.
const latLng = G.latLng(latLngObject);

Pass a google.maps.LatLng object.

G.latLng(latLngObject: google.maps.LatLng): LatLng

ParameterTypeRequiredDescription
latLngObjectgoogle.maps.LatLngYesA google.maps.LatLng object.
const latLng = G.latLng(googleMapsLatLngObject);

LatLngValue type​

The G.latLng() function and other methods that accept the latitude/longitude values accept a LatLngValue as the latitude value.

The LatLngValue can be one of the following values:

  • An array where the first value is the latitude value and the second value is the longitude value. For example:
    • Numbers: [41.3874, 2.1686]
    • Strings: ['41.3874', '2.1686']
    • Mixed values: ['41.3874', 2.1686]
  • An object setting the latitude and longitude values. For example:
    • Short form numbers: {lat: 41.3874, lng: 2.1686}
    • Short forms strings: {lat: '41.3874', lng: '2.168610'}
    • Short forms mixed values: {lat: '41.3874', lng: 2.168610}
    • Long form numbers: {latitude: 41.3874, longitude: 2.1686}
    • Long forms strings: {latitude: '41.3874', longitude: '2.168610'}
    • Long forms mixed values: {latitude: '41.3874', longitude: 2.168610}
    • Mixed keys: {latitude: 41.3874, lng: 2.1686}
    • Mixed keys: {lat: 41.3874, longitude: '2.1686'}
  • A LatLng object.
  • A google.maps.LatLng object.

LatLngLiteral type​

The short form object for a latitude/longitude pair. For example: {lat: 41.3874, lng: 2.1686}.

PropertyTypeDescription
latnumber | stringThe latitude value.
lngnumber | stringThe longitude value.

LatLngLiteralExpanded type​

The long form object for a latitude/longitude pair. For example: {latitude: 41.3874, longitude: 2.1686}.

PropertyTypeDescription
latitudenumber | stringThe latitude value.
longitudenumber | stringThe longitude value.

Properties​

PropertyTypeDescription
latitudenumberThe latitude value
latnumberShort form to get and set the latitude value
longitudenumberThe longitude value
lngnumberShort form to get and set the longitude value
latlng.lat = 41.3874;
const lat = latlng.lat;

latlng.latitude = 41.3874;
const lat = latlng.latitude;

latlng.lng = '2.1686';
const lng = latlng.lng;

latlng.longitude = 2.1686;
const lng = latlng.longitude;

Methods​

  • Methods inherited from Base.

clone​

clone(): LatLng

Clones the LatLng object and returns a new one.

const clone = latLng.clone();

equals​

equals(other: LatLngValue): boolean

Tests to see if the given latitude/longitude pair is equal to this latitude/longitude pair.

ParameterTypeRequiredDescription
otherLatLngValueYesThe latitude/longitude value to compare.
if (latLng.equals(otherLatLngObject)) {
// Do something
}

getLat​

getLat(): number

Get the latitude value.

const lat = latLng.getLat();

getLng​

getLng(): number

Get the longitude value.

const lng = latLng.getLng();

isValid​

isValid(): boolean

Returns whether the latitude/longitude pair is a valid value.

if (latLng.isValid()) {
// Do something here
}

set​

set(latitude: LatLngValue | number | string, longitude?: number | string): LatLng

ParameterTypeRequiredDescription
latitudeLatLngValue | number | stringYesThe latitude value, an array with the latitude/longitude values, or an object containing the latitude/longitude values.
longitudenumber | stringThe longitude value. Only set if latitude is the latitude and not an object or array.

Set the latitude and longitude values. This accepts the values in the following ways:

latLng.set(latitude: number, longitude: number)

ParameterTypeRequiredDescription
latitudenumberYesThe latitude value.
longitudenumberYesThe longitude value.
latLng.set(41.3874, 2.1686);

latLng.set([latitude: number, longitude: number])

ParameterTypeRequiredDescription
valueArrayYesAn array containing the latitude and longitude values
latLng.set([41.3874, 2.1686]);

latLng.set({lat: latitude: number, lng: longitude: number})

ParameterTypeRequiredDescription
valueobjectYesAn object containing the latitude and longitude values
latLng.set({lat: 41.3874, lng: 2.1686});

latLng.set({latitude: latitude: number, longitude: longitude: number})

ParameterTypeRequiredDescription
valueobjectYesAn object containing the latitude and longitude values
latLng.set({latitude: 41.3874, longitude: 2.1686});

latLng.set(latLngObject: LatLng)

ParameterTypeRequiredDescription
latLngObjectLatLngYesA LatLng object. In this case the latitude and longitude from the object are used.
latLng.set(latLngObject);

setLat​

setLat(lat: number | string): LatLng

ParameterTypeRequiredDescription
latnumber | stringYesThe latitude value. Ideally it's a number, but it can be a number string.

Set the latitude value. Use this if you want to change the latitude value after the object is created.

latLng.setLat(41.3874);

Or, you can use chain methods together to create the initial object.

const latLng = G.latLng().setLat(41.3874).setLng(2.1686);

setLng​

setLng(lng: number | string): LatLng

ParameterTypeRequiredDescription
lngnumber | stringYesThe longitude value. Ideally it's a number, but it can be a number string.

Set the longitude value. Use this if you want to change the longitude value after the object is created.

latLng.setLng(2.1686);

Or, you can use chain methods together to create the initial object.

const latLng = G.latLng().setLat(41.3874).setLng(2.1686);

toGoogle​

toGoogle(): google.maps.LatLng|null

Get the Google Maps LatLng object. null is only returned if the Google maps library has not been loaded.

An error is thrown if the latitude or longitude value is missing. Use isValid to check first.

const gmLatLng = latLng.toGoogle();

toJson​

toJson(): google.maps.LatLngLiteral

Get the latLng JSON literal value.

{
lat: 41.3874,
lng: 2.1686
}
const latLngJson = latLng.toJson();