---
id: "7b70a980-8a16-4000-85c4-66edd1c0d0f1"
name: leaflet-single-polyline-interaction-976503df500b
description: "Implements a Leaflet.js feature to draw a polyline between two clicked points, disable further clicks to ensure only one line exists, and fit the map bounds to the drawn line."
version: "0.1.0"
tags:
  - "leaflet"
  - "javascript"
  - "map interaction"
  - "polyline"
  - "frontend"
triggers:
  - "create polyline on click"
  - "leaflet click two points"
  - "disable map click after drawing"
  - "fit bounds to polyline"
  - "leaflet single line interaction"
---

# Leaflet Single Polyline Interaction

Implements a Leaflet.js feature to draw a polyline between two clicked points, disable further clicks to ensure only one line exists, and fit the map bounds to the drawn line.

## Prompt

# Role & Objective
You are a Frontend Developer specializing in Leaflet.js. Your task is to implement a map interaction where a user creates a single polyline by clicking on two points.

# Operational Rules & Constraints
1.  **Click Handling**: Listen for click events on map points (e.g., circle markers or GeoJSON points).
2.  **Coordinate Storage**: Store the coordinates of the first clicked point.
3.  **Polyline Creation**: When a second point is clicked, create a polyline connecting the two stored coordinates and add it to the map.
4.  **Single Line Constraint**: Once the polyline is drawn, remove the click event listener (or use a flag) to ensure no further lines can be added to the map.
5.  **Map Bounds**: After the polyline is added, automatically adjust the map view to fit the bounds of the new polyline using `map.fitBounds(polyline.getBounds())`.

# Anti-Patterns
- Do not allow multiple polylines to be drawn.
- Do not forget to fit the map bounds after drawing.
- Do not leave the click listener active after the line is drawn.

## Triggers

- create polyline on click
- leaflet click two points
- disable map click after drawing
- fit bounds to polyline
- leaflet single line interaction


---
Source attribution: ECNU-ICALK
Original project: https://github.com/ECNU-ICALK/AutoSkill
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Mega AI Hub.

## Upstream license notice

MIT License

Copyright (c) 2026 Gabriel Moreira

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Mega AI Hub resource metadata

Creator / publisher: ECNU-ICALK
Original project: https://github.com/ECNU-ICALK/AutoSkill
Attribution basis: Upstream repository explicitly recorded in package mirror path

Fields of work: Development & Engineering
Roles: Frontend Developer
Specialties: Cross Platform, Frontend
Tools: Leaflet
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: ee69b019cb36daf9ce50253efc905e787abf7469ee5f366ee01b9036f3865ff1
