Choose markers from various JSON layers within a specified distance

I am looking to merge at least 2 JSON layers to allow all their markers to be searchable within a specified radius after clicking.

There are two files defined in the code as follows:

var url = "Peterborough.json";
var url2 = "Test.json";

The implementation can be found in the example linked below:

up to where we need to set the SelectPoints(lat,lon) function.

Afterwards, I attempted something like this:

https://i.sstatic.net/MJ6q3.png

function SelectPoints(lat,lon){
var dist = document.getElementById("miles").value;

xy = [lat,lon];  //center point of circle

var theRadius = parseInt(dist) * 1609.34  //1609.34 meters in a mile 
//dist is a string so it's convered to an Interger.

selPts.length =0;  //Reset the array if selecting new points

job.eachLayer(function (layer) {
// Lat, long of current point as it loops through.
layer_lat_long = layer.getLatLng();

// Distance from our circle marker To current point in meters
distance_from_centerPoint = layer_lat_long.distanceTo(xy);

// See if meters is within radius, add the to array
if (distance_from_centerPoint <= theRadius) {
     selPts.push(layer.feature);  
}

job2.eachLayer(function (layer) {
// Lat, long of current point as it loops through.
layer_lat_long = layer.getLatLng();

// Distance from our circle marker To current point in meters
distance_from_centerPoint = layer_lat_long.distanceTo(xy);

// See if meters is within radius, add the to array
if (distance_from_centerPoint <= theRadius) {
     selPts.push(layer.feature);  
}

});

However, instead of the desired result, I ended up with a blank space (even the map disappears).

What could be causing this issue?

I have read about the iteration of this function here:

Leaflet eachLayer function does not iterate through all Layers

but it hasn't provided a clear solution for me.

I want both of these layers to be selectable after clicking the marker instead of just one.

When I try to use the second JSON file as a layer and replicate the

$.getJSON(url2, function(data) {

code, I can see everything on the map clearly, but they are not selected upon clicking.

Is there a way to address this issue and make both layers eligible for selection?

Answer №1

Seems like the placement of the ; is incorrect in my code.

Here is the corrected version:

 job.eachLayer(function (layer) {
    // Latitude and longitude of the current point as it loops through.
    layer_lat_long = layer.getLatLng();
    
    // Distance from our circle marker to the current point in meters
    distance_from_centerPoint = layer_lat_long.distanceTo(xy);
    
    // Check if the distance is within the radius, then add to the array
    if (distance_from_centerPoint <= theRadius) {
         selPts.push(layer.feature);  
    }
  })

job2.eachLayer(function (layer) {
    // Latitude and longitude of the current point as it loops through.
    layer_lat_long = layer.getLatLng();
    
    // Distance from our circle marker to the current point in meters
    distance_from_centerPoint = layer_lat_long.distanceTo(xy);
    
    // Check if the distance is within the radius, then add to the array
    if (distance_from_centerPoint <= theRadius) {
         selPts.push(layer.feature);  
    }
  })

Referencing the example provided:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What are the steps to rectify the issue of displaying data accurately on

I am working on my ReactJS project and using devextreme to create a unique circular gauge. However, I'm facing an issue where certain values are not being displayed correctly. For instance, instead of showing the value 5011480286.78, it is displaying ...

Issues with data communication in AJAX and Node JS/Express post requests

I'm currently exploring Node.js and I'm running into an issue with app.post. Everything seems to be working fine, as I can see the console log whenever the action is executed. However, the data sent by AJAX from main.js does not seem to be receiv ...

Experiencing an issue while attempting to retrieve JSON data from an MVC Controller, with the error message of net::ERR_IN

I am attempting to retrieve JSON data from my controller for use in jQuery. The code I have written seems correct, as visiting the URL in my browser returns the JSON data, confirming that the controller is functioning properly. However, I encounter the fol ...

Transform JSON object to a class/interface object using Typescript

I am currently working on converting an API response into a TypeScript class or interface. The API is returning a list of objects with various properties, but I only require a few specific properties from the response object. Example of API Response: ...

Performing a Jquery ajax post to an MVC2 action

I have a script set up to send a POST request to an endpoint, and it seems like the routing is correct as it hits the breakpoint on the server. $(document).ready(function() { var o = new Object(); o.message = 'Hi from the page'; $.ajax({ ...

What method is most effective for iterating through ajax requests?

Currently, I am working on Sharepoint and have developed an app that generates lists within the site collection. The process of creating the list is not a challenge for me. However, my goal is to create 15 columns and incorporate them into the default view ...

When placed above in the template, the ng-model is causing the ng-repeat to not display anything

Currently, I am teaching myself Angular by following a tutorial at https://docs.angularjs.org/tutorial, but with my twist of using different data to keep things interesting. My struggle seems to stem from a simple mistake I might be making, compounded by ...

Completed processing graphical event layer

I am utilizing the Google Maps API v3 to build a map with multiple layers that are loaded upon user request. The layers are loaded in Geojson format using the following code: function getgeojson(json) { proplayer = new google.maps ...

Middleware that is commonly used by both error handling and regular request-response chains

I am currently working on implementing a middleware that can set a variable in the sequence of both error and non-error scenarios. Is there a way to achieve this without disrupting the normal flow of middleware? Specifically, when I include the err param ...

Storing dates using Angular 2 and JSON for efficient data management

I've encountered a challenging issue with my Angular 2 application. I'm attempting to organize my API (MongoDB) in such a way that each new "post" added by the admin can be retrieved by date (not time) on the front end. Here's an example of ...

The Next.js 404 error page seems to be malfunctioning. Any ideas on what might be causing this issue?

Whenever I attempt to access a non-existent route, the home page loads without changing the URL. My expectation was to see a 404 error page. To handle this issue, I created a custom error page called pages/_error.js import Page404 from './404'; ...

What is the process for generating an Electronic Program Guide for television?

Welcome to the forum! I'm a front-end developer at a company for 6 months now, currently working on a TV app. It's my first experience in this field and I'm facing some challenges, particularly with creating an epg for the app. Unfortunately ...

angular-ui-tree, dynamically generate the tree using data retrieved from the DATABASE

I have implemented the angular-ui-tree library to present the folder hierarchy. The nodes are saved in a MongoDB database, with each node object structured as follows: { "node_name" : "Folder 1", "node_path" : "AAABBB", "node_id" : 103, "node ...

Tips for iterating over two models using ng-repeat

I'm a newcomer to Angular and I have an issue that requires a neat solution: I have a div element called "SelectedList" that needs to display a list of active elements from another container. This container contains two tabs, Tab1 and Tab2, which are ...

Implementing the ability to add and remove classes in JavaScript

I am trying to implement an add or remove class feature in JavaScript, but I'm facing some issues with it. Below is the code snippet I have: if (window.location.hash == "#/profile/"){ document.getElementById("tabMenu").removeClass("bottomTa ...

Internet Explorer does not automatically resend AJAX requests after submitting a form

Specifically mentioning IE in my title because my code functions correctly on Chrome. The "Maintenance" view I have is responsible for creating, editing, and deleting. The form is located in a partial view named "_MaintenanceForm" with its own GET/POST met ...

What is the method for displaying character entities on Fullcalendar when PHP is used to retrieve data from the database?

Currently, I am utilizing PHP to retrieve posts from WordPress (WP_Query()) in order to create Fullcalendar event strings. Everything functions properly, except for the issue with character entities, specifically apostrophes. In the Title field of the po ...

Using Array.prototype.map in conjunction with parseInt functions

Recently, I encountered something quite unusual. I am attempting to separate a string containing a time (e.g. "12:00", "13:30") into two distinct integers. I experimented with the following approach: timeString = "12:00" [hours, minutes] = timeString.spl ...

I have implemented a bootstrap modal, but whenever I trigger a function on the JavaScript side, it automatically closes. I am aiming for the modal to remain open

<div id="responsive-modal3" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none; " data-keyboard="false" data-backdrop="static" ng-init = "populateBidSpocData()" > <div cl ...

Guide to making a JavaScript button that triggers an iframe to open upon user clicking the button

I'm currently enhancing the comment section for posts on my website. I am looking to implement a button in javascript that, when clicked, will open an iframe window displaying comments similar to how Facebook does on their post. If there are other lan ...