# How to use geolib - 10 common examples

## To help you get started, we’ve selected a few geolib examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

flyandi / react-native-maps-navigation / src / modules / Simulator.js View on Github
``````points.forEach((point, index) =&gt; {

const nextPoint = points[index + 1];

if(nextPoint &amp;&amp; !nextPoint.final == true) {

// calculate distance between each point
const distance = Math.round(GeoLib.getDistance(point, nextPoint));
const bearing =  GeoLib.getBearing(point, nextPoint);

if(bearing !== 0) {

if (distance &gt; 1) {

for (var x = 1; x &lt; distance; x++) {

result.push(Object.assign({}, {bearing}, GeoLib.computeDestinationPoint(point, x, bearing)));
}

} else {
result.push(Object.assign({}, {bearing}, point));
}
}
}``````
NetsBlox / NetsBlox / utils / rpc / eclipse-2017 / checkStations.js View on Github
``````function distanceToPath(lat, lon){
let min = 100000; //in KM
let poi = {latitude: lat, longitude: lon};
for (var i = 0; i &lt; pathPoints.length; i++) {
let dist = geolib.getDistance(poi, {latitude: pathPoints[i][0], longitude: pathPoints[i][1]}) / 1000;
if (dist &gt; min) break;
min = dist;
}
return min; //in KM
}
/* eslint-enable no-console */``````
team-copper / captAR / component / Map.js View on Github
``````onCapturePress() {

let team = '';

for (let i=0; i``````
flyandi / react-native-maps-navigation / src / modules / Simulator.js View on Github
``````points.forEach((point, index) =&gt; {

const nextPoint = points[index + 1];

if(nextPoint &amp;&amp; !nextPoint.final == true) {

// calculate distance between each point
const distance = Math.round(GeoLib.getDistance(point, nextPoint));
const bearing =  GeoLib.getBearing(point, nextPoint);

if(bearing !== 0) {

if (distance &gt; 1) {

for (var x = 1; x &lt; distance; x++) {

result.push(Object.assign({}, {bearing}, GeoLib.computeDestinationPoint(point, x, bearing)));
}

} else {
result.push(Object.assign({}, {bearing}, point));
}
}
}
});``````
flyandi / react-native-maps-navigation / src / modules / Tools.js View on Github
``````const d2r = Math.PI / 180;   // degrees to radians
const r2d = 180 / Math.PI;   // radians to degrees
const points = 32;
let result = [];

// find the radius in lat/lon
//const rlng = rlat / Math.cos({coordinate.latitude * d2r);

if (initialBearing &gt; finalBearing) finalBearing += 360;
let deltaBearing = finalBearing - initialBearing;
deltaBearing = deltaBearing/points;

for (let i=0; (i &lt; points+1); i++)
{
}

return result;
};``````
vokkim / tuktuk-chart-plotter / src / client / map.js View on Github
``````function calculateExtensionLine(position, course, speed, extensionLineSetting) {
if (extensionLineSetting === EXTENSION_LINE_OFF) {
return undefined
}
const time = 60 * parseInt(extensionLineSetting)
if (position && position.latitude && position.longitude && course && speed > 0.5) {
const distance = speed * time // Speed in m/s
const start = [position.latitude, position.longitude]
const destination = computeDestinationPoint(
{lat: position.latitude, lon: position.longitude},
distance,
toDegrees(course)
)
const middle = computeDestinationPoint(
{lat: position.latitude, lon: position.longitude},
distance / 2,
toDegrees(course)
)
return {
start,
middle: [middle.latitude, middle.longitude],
end: [destination.latitude, destination.longitude]
}
}
return undefined
}``````
vokkim / tuktuk-chart-plotter / src / client / map.js View on Github
``````function calculateExtensionLine(position, course, speed, extensionLineSetting) {
if (extensionLineSetting === EXTENSION_LINE_OFF) {
return undefined
}
const time = 60 * parseInt(extensionLineSetting)
if (position && position.latitude && position.longitude && course && speed > 0.5) {
const distance = speed * time // Speed in m/s
const start = [position.latitude, position.longitude]
const destination = computeDestinationPoint(
{lat: position.latitude, lon: position.longitude},
distance,
toDegrees(course)
)
const middle = computeDestinationPoint(
{lat: position.latitude, lon: position.longitude},
distance / 2,
toDegrees(course)
)
return {
start,
middle: [middle.latitude, middle.longitude],
end: [destination.latitude, destination.longitude]
}
}
return undefined``````
flyandi / react-native-maps-navigation / src / modules / Simulator.js View on Github
``````const nextPoint = points[index + 1];

if(nextPoint &amp;&amp; !nextPoint.final == true) {

// calculate distance between each point
const distance = Math.round(GeoLib.getDistance(point, nextPoint));
const bearing =  GeoLib.getBearing(point, nextPoint);

if(bearing !== 0) {

if (distance &gt; 1) {

for (var x = 1; x &lt; distance; x++) {

result.push(Object.assign({}, {bearing}, GeoLib.computeDestinationPoint(point, x, bearing)));
}

} else {
result.push(Object.assign({}, {bearing}, point));
}
}
}
});``````
hackclub / finder / src / pages / index.js View on Github
``````} = this.state
const hasSearchValue = searchValue.trim().length > 0
const isPositionSet = searchLat !== null && searchLng !== null
let filteredClubs = [] // We want to show every club by default, but it'll cause a significant decrease in performance
if (searchByLocation) {
if (isPositionSet) {
const center = { latitude: searchLat, longitude: searchLng }
const conversionConstant = useImperialSystem
? MILE_TO_METER
: KILOMETER_TO_METER
const filteredPoints = getPointsInCircle(
clubs,
center,
)
filteredClubs = geolib
.orderByDistance(center, filteredPoints)
.map(({ key }) => filteredPoints[key]) // Needed because Geolib returns a differently-shaped object
}
} else {
if (hasSearchValue) {
filteredClubs = this.fuse.search(searchValue)
}
}
return filteredClubs
}``````
kiwicom / mobile / app / hotels / src / map / allHotels / MapView.js View on Github
``````getDelta = (selectedCoordinate: LatLng, coordinates: LatLng[]) => {
const distances = orderByDistance(selectedCoordinate, coordinates);

if (coordinates.length === 1) {
return {
longitudeDelta: 0.005,
latitudeDelta: 0.005,
};
}

// Use median to filter out extreme positions to compute more appropriate region
const lowMiddle = Math.floor((distances.length - 1) / 2);
const highMiddle = Math.ceil((distances.length - 1) / 2);
const median = (distances[lowMiddle].distance + distances[highMiddle].distance) / 2;
const validDistances =
distances.length > 2
? distances
.filter(({ distance }) => median * 1.5 > distance)``````

## geolib

Library to provide basic geospatial operations like distance calculation, conversion of decimal coordinates to sexagesimal and vice versa, etc. This library is currently **2D**, meaning that altitude/elevation is not yet supported by any of its functions!

MIT