Example: Create circumference of 50km around 10 Downing Street and check if Buckingham Place falls within the circumference.
await LocationManager.addCircumference({
centre: new LM.Address("10 Downing Street, London"),
range: 50
});
const isInRange = await LocationManager.checkInRange(
new LM.Address("Buckingham Palace, London")
);
Inspiration
We at Shopolivery needed a way to manage the service area of the Shopolivery service; the Shopolivery service isn't available in all locations, so we needed a convenient, robust library to verify whether user locations of all types (such as postcodes and addresses) were within irregular, defined-by-us service areas, so we created location manager. Location manager helps check whether physical locations are within defined areas such as circumferences and bounds.
Basic Usage
You can install location-manager from NPM:
$ npm i location-manager
You can then initialise the library in Node:
const LM = require("location-manager");
const LocationManager = new LM();
You can then add a valid area to the manager using functions on the LocationManager:
await LocationManager.addBounds({
bounds: [
new LM.Address("10 Downing Street, London"),
new LM.Address("Imperial War Museum, London")
new LM.Address("Buckingham Palace, London"),
]
});
This will in effect create a triangle with the three locations as the points, with any address inside the triangle being a valid location for LocationManager.checkInRange()
.
You can then check for a valid address within the bounds like:
const bigBenLocation = new LM.Address("Big Ben, Westminster");
await LocationManager.checkInRange(bigBenLocation);
Which evaluates to true
because Big Ben is within the area defined by the bounds above.
An address such as Victoria Station will evaluate to false
because it is outside the bounds above.
const victoriaStationLocation = new LM.Address("Victoria Station, London");
await LocationManager.checkInRange(victoriaStationLocation);
You can also combine multiple area types, as seen below, to create a powerful and refined chosen area.
await LocationManager.addBounds({
bounds: [
new LM.Address("Stratford, London"),
new LM.Address("Bromley, London"),
new LM.Address("Twickenham, London"),
new LM.Address("Wembley, London")
]
});
await LocationManager.addCircumference({
centre: new LM.Address("Kensington, London"),
radius: 10
});
await LocationManager.addLine({
type: "vertical",
side: "right",
alignment: new LM.Address("White City, London")
});
await LocationManager.addLine({
type: "horizontal",
side: "above",
alignment: new LM.Address("Fullham, London")
});
This effectively creates the area seen below. The valid area is above the green line, to the right of the blue line, and inside the black circle and inside the red polygon.
This causes the results below.
await LocationManager.checkInRange(new LM.Address("Kensington, London"));
await LocationManager.checkInRange(new LM.Address("Westminster, London"));
await LocationManager.checkInRange(new LM.Address("Buckingham Palace, London"));
await LocationManager.checkInRange(new LM.Address("Balham, London"));
await LocationManager.checkInRange(new LM.Address("Holloway, London"));
Example Usages
You can create three different types of location references to locations, which can be used with the location manager: address
(can lookup any address on the globe), UKPostcode
(can lookup any postcode in the UK) and coordinates
(can reference any co-ordinates on the globe).
new LM.Address("Buckingham Palace, London");
new LM.Address({
city: "London",
street: "Buckingham Palace Road"
});
new LM.UKPostcode("SW1A 1AA");
new LM.Coordinate(51.501247, -0.142437);
You can create three different types of area references: circumference
, line
and bounds
. You can add them to the location manager with their relevant add
method.
await LocationManager.addBounds({
bounds: [
new LM.Address("Stratford, London"),
new LM.Address("Bromley, London"),
new LM.Address("Twickenham, London"),
new LM.Address("Wembley, London")
]
});
await LocationManager.addLine({
type: "horizontal",
side: "above",
alignment: new LM.Address("Fullham, London")
});
await LocationManager.addCircumference({
centre: new LM.Address("Kensington, London"),
radius: 10
});
You can instead create and return an area reference (but not add it to the location manager). Each area reference has a relevant method, e.g.
const circumference = await LocationManager.createCircumference({
centre: new LM.Address("Buckingham Palace, London"),
radius: 5
});
Test if location reference is valid in current location manager.
await LocationManager.checkInRange(locationObject);
Test whether specified locations are within specified area definitions.
await LocationManager.checkInModelRange(circumferenceArea, addressReference);
await LocationManager.checkInModelRange(
[circumferenceArea, boundsArea],
postcodeReference
);
Credit
Made with 💖 by . We at Shopolivery love open source software - we recognise the internet (and by extent Shopolivery) is only possible with the hard work of open source maintainers - so we are committed to contribute by maintaining and creating open source software under permissive licenses.
Lead Maintainer: Tom
License
MIT