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

latLng = (latitude?: LatLngValue | number, longitude?: number): 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 the LatLng object is simply 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: {lng: 41.3874, longitude: '2.1686'}
  • A LatLng object.
  • A google.maps.LatLng object.

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 = point.lat;

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

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

latlng.longitude = 2.1686;
const lng = point.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, longitude?: number): LatLng

ParameterTypeRequiredDescription
latitudeLatLngValueYesThe latitude value, an array with the latitude/longitude values, or an object containing the latitude/longitude values.
longitudenumberThe 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): LatLng

ParameterTypeRequiredDescription
latnumberYesThe latitude value.

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): LatLng

ParameterTypeRequiredDescription
lngnumberYesThe longitude value.

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.

const gmLatLng = latLng.toGoogle();

toJson

toJson(): google.maps.LatLngLiteral

Get the latLng JSON literal value.

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