Select to view content in your preferred language

Custom Javascript - Based on Lat long from Geopoint return Segment ID

258
0
04-03-2024 11:25 AM
SteveBrewer
Occasional Contributor

Below is the java script but trying to pull a "label" of a line segment saved in a json file by the nearest point in a geopoint question using the geopoint lat long it keeps returning [object Object]

/**
* Fetches 'geojson' data from a file
* @param {string} url - File url
* @returns {Promise<Object>} Promise resolving to 'geojson' data
*/
async function fetchGeoJsonData(url);
const response = await fetch(url);
if (!response.ok) {
throw new Error('Failed to fetch 'geojson' data');
}

return response.json();
}

export { fetchGeoJsonData };
// math.js

/**
* Calculates distance between two points
* @param {number} lat1 - Point 1 latitude
* @param {number} lon1 - Point 1 longitude
* @param {number} lat2 - Point 2 latitude
* @param {number} lon2 - Point 2 longitude
* @returns {number} Distance between points
*/
export function calculateDistance(lat1, lon1, lat2, lon2) {
// distance calculation logic
}

/**
* Finds the closest point on a segment to a given point
* @param {number} x0 - Point x
* @param {number} y0 - Point y
* @param {number} x1 - Segment point 1 x
* @param {number} y1 - Segment point 1 y
* @param {number} x2 - Segment point 2 x
* @param {number} y2 - Segment point 2 y
* @returns {Object} Object with x and y of closest point
*/
export function findClosestSegmentPoint(x0, y0, x1, y1, x2, y2) {
// find closest logic
return {
x: closestX,
y: closestY
};
}
// geo.js

import { fetchGeoJsonData } from './data.js';
import { calculateDistance, findClosestSegmentPoint } from './math.js';

/**
* Finds the closest line segment to a point in geojson data
*/
export async function findNearestSegment(point, geojsonUrl) {

const geoData = await fetchGeoJsonData(geojsonUrl);

// Find closest logic using imported math functions

return nearestSegment;
}
// index.js

import { findNearestSegment } from './geo.js';

const point = {
lat: 0,
lon: 0
};

const geojsonUrl = 'data.json';

try {
const nearest = await findNearestSegment(point, geojsonUrl);
console.log('Nearest segment:', nearest);
} catch (err) {
console.error('Error finding nearest segment', err);
}

 

0 Kudos
0 Replies