# Leaflet Map Picker - Dokumentasi Komponen reusable untuk memilih lokasi menggunakan Leaflet Maps dengan fitur pencarian dan reverse geocoding. ## Fitur - πŸ—ΊοΈ Interactive map dengan OpenStreetMap - πŸ” Pencarian lokasi dengan Nominatim API - πŸ“ Click-to-select koordinat - 🏠 Reverse geocoding untuk mendapatkan alamat otomatis - πŸ“± Responsive design - ⚑ Mudah dikonfigurasi dan reusable ## Instalasi ### 1. Include CSS dan JavaScript Tambahkan di section `Styles` dan `Scripts` pada view Anda: ```cshtml @section Styles { } @section Scripts { } ``` ## Penggunaan ### Metode 1: Menggunakan Partial View (Recommended) #### Langkah 1: Tambahkan Input Fields untuk Latitude dan Longitude ```cshtml
Latitude
Longitude
``` #### Langkah 2: Include Partial View ```cshtml
@{ ViewData["MapId"] = "map-picker"; ViewData["LatInputId"] = "latitude-input"; ViewData["LngInputId"] = "longitude-input"; ViewData["SearchInputId"] = "search-location"; ViewData["SearchResultsId"] = "search-results"; ViewData["ClearSearchBtnId"] = "clear-search"; ViewData["Label"] = "Pilih Lokasi di Peta"; } @await Html.PartialAsync("_LeafletMapPicker")
``` #### Langkah 3: Initialize JavaScript Component ```cshtml @section Scripts { } ``` ### Metode 2: HTML Manual ```html

Cari lokasi menggunakan search box di atas, atau klik pada peta untuk menentukan lokasi secara manual

``` ## Konfigurasi Options | Option | Type | Default | Deskripsi | |--------|------|---------|-----------| | `mapElementId` | string | `'map-picker'` | ID elemen untuk map container | | `latInputId` | string | `'latitude-input'` | ID input field untuk latitude | | `lngInputId` | string | `'longitude-input'` | ID input field untuk longitude | | `searchInputId` | string | `'search-location'` | ID input field untuk search box | | `searchResultsId` | string | `'search-results'` | ID elemen untuk search results dropdown | | `clearSearchBtnId` | string | `'clear-search'` | ID button untuk clear search | | `alamatInputSelector` | string | `null` | CSS selector untuk input alamat (optional) | | `initialLat` | number | `-6.2088` | Latitude awal map (Jakarta) | | `initialLng` | number | `106.8456` | Longitude awal map (Jakarta) | | `initialZoom` | number | `12` | Zoom level awal | | `maxZoom` | number | `18` | Maximum zoom level | | `minZoom` | number | `4` | Minimum zoom level | | `searchDebounceMs` | number | `500` | Debounce delay untuk search (ms) | | `countryCode` | string | `'id'` | Country code untuk filter pencarian | | `language` | string | `'id'` | Language untuk API results | ## ViewData Options untuk Partial View | Key | Type | Default | Deskripsi | |-----|------|---------|-----------| | `MapId` | string | `'map-picker'` | ID untuk map element | | `LatInputId` | string | `'latitude-input'` | ID untuk latitude input | | `LngInputId` | string | `'longitude-input'` | ID untuk longitude input | | `SearchInputId` | string | `'search-location'` | ID untuk search input | | `SearchResultsId` | string | `'search-results'` | ID untuk search results | | `ClearSearchBtnId` | string | `'clear-search'` | ID untuk clear button | | `Label` | string | `'Pilih Lokasi di Peta'` | Label text | | `HelpText` | string | Default help text | Help text di bawah map | | `ShowLabel` | bool | `true` | Tampilkan label | | `ShowSearch` | bool | `true` | Tampilkan search box | | `ShowHelpText` | bool | `true` | Tampilkan help text | ## Public API Methods ### `getCoordinates()` Mendapatkan koordinat saat ini. ```javascript const coords = leafletMapPickerInstance.getCoordinates(); console.log(coords); // { lat: -6.2088, lng: 106.8456 } ``` ### `setCoordinates(lat, lng, updateMarker = true)` Set koordinat secara programmatic. ```javascript leafletMapPickerInstance.setCoordinates(-6.2088, 106.8456, true); ``` ### `destroy()` Clean up instance dan event listeners. ```javascript leafletMapPickerInstance.destroy(); ``` ## Contoh Lengkap ```cshtml @{ ViewData["Title"] = "Edit Lokasi"; } @section Styles { }
Latitude
Longitude
Alamat
@{ ViewData["MapId"] = "map-picker"; ViewData["LatInputId"] = "latitude-input"; ViewData["LngInputId"] = "longitude-input"; ViewData["SearchInputId"] = "search-location"; ViewData["SearchResultsId"] = "search-results"; ViewData["ClearSearchBtnId"] = "clear-search"; ViewData["Label"] = "Pilih Lokasi di Peta"; } @await Html.PartialAsync("_LeafletMapPicker")
@section Scripts { } ``` ## Custom Styling Anda bisa override CSS classes untuk custom styling: ```css /* Custom map height */ .leaflet-map-picker { height: 500px; } /* Custom search box styling */ .leaflet-map-picker-search-input { border-color: #your-color; } /* Custom search results */ .leaflet-map-picker-result-item:hover { background-color: #your-color; } ``` ## Browser Support - Chrome (latest) - Firefox (latest) - Safari (latest) - Edge (latest) ## Dependencies - Leaflet 1.9.4+ - OpenStreetMap (Tile Layer) - Nominatim API (Geocoding) ## Troubleshooting ### Map tidak muncul - Pastikan Leaflet CSS dan JS sudah ter-load - Periksa console untuk error - Pastikan element ID sudah benar ### Search tidak bekerja - Pastikan ada koneksi internet - Nominatim API memiliki rate limit - Periksa browser console untuk error ### Koordinat tidak update - Pastikan input ID sudah benar - Periksa attribute `step="any"` pada input number ## License MIT License ## Author Bank Sampah Development Team