Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closetlayer function not working with GeoJson #100

Open
ayushjoshi1 opened this issue Sep 13, 2023 · 0 comments
Open

Closetlayer function not working with GeoJson #100

ayushjoshi1 opened this issue Sep 13, 2023 · 0 comments

Comments

@ayushjoshi1
Copy link

I am trying to use the closetlayer function to find the closest point on a geoJson file but keep getting a "Cannot read properties of null (reading 'lat')" error. May geojson file is a linestring. Any ideas on what I may be doing wrong?

var geojsonLayer = new L.GeoJSON.AJAX("XXX.geojson", {
    style: {color: 'red', 
            weight: 4}
});       
geojsonLayer.addTo(map);
const coordinates_array = geojsonLayer.getLayers().map(l => l.feature.geometry.coordinates)

markerClosestPolyline1 = null

map.on('click', function(e) {
  
  const coordinates_array = geojsonLayer.getLayers().map(l => l.feature.geometry.coordinates)
  let closest_latlng = L.GeometryUtil.closestLayer(map, coordinates_array, e.latlng)

  markerClosestPolyline1 = L.marker(closest_latlng).addTo(map).bindPopup('Closest point on polyline1');
})

The error is pointing me to this portion of the code (Im guessing its an error related to the way im loading my geojson file)

	project: function (latlng) {
		var d = Math.PI / 180,
		    max = this.MAX_LATITUDE,
		    lat = Math.max(Math.min(max, latlng.lat), -max),
		    sin = Math.sin(lat * d);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant