I came across a helpful tutorial that guided me on how to check if a point lies within a specific polygon displayed by a marker:
The tutorial worked smoothly by fetching the polygon geoJSON data using ajax and integrating it into the map. However, I already have my own mapbox map set up with a polygon area defined. I am utilizing their API for displaying my map and now require access to the geoJSON data in order to determine if a point falls within this specified area. How can I achieve this through the API?
Below is how I load the map, the geoJSON data, and a function to verify if a point is located within the geoJSON-defined area:
// Establishing connection with mapbox
var mapID = "<map-id>";
L.mapbox.accessToken = "<my-token>";
// Initializing the map
var map = L.mapbox.map("mapData", mapID, {
attributionControl: false,
zoomControl: false
}).setView([53.799, -1.548], 13, {
pan: { animate: true },
zoom: { animate: true }
});
// Retrieving geoJSON data
var geoJson;
$http.get("https://a.tiles.mapbox.com/v4/" + mapID + "/features.json?access_token=" + L.mapbox.accessToken).success(function(data){
geoJson = data;
});
// Checking point in polygon
function determinePointInPolygon(){
var coords = [-1.3, 5.2];
var layer = leafletPip.pointInLayer(coords, L.geoJson(geoJson), true);
if (!layer.length) // Point does not fall within the polygon
else // Point lies inside the polygon
}