Advertisement
hctorres02

Leaflet with geocoder

May 4th, 2024
688
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.71 KB | Source Code | 0 0
  1. <div class="row">
  2.     <div class="col-md-12">
  3.         <div class="form-field-default">
  4.             <label>Localização:</label>
  5.             <div id="location-map" style="background:#fff;border:none;height:440px;width:100%;box-sizing:border-box"></div>
  6.             <input type="hidden" name="endereco_latlng" value="">
  7.         </div>
  8.         <div class="clear"></div>
  9.     </div>
  10. </div>
  11. <div class="row">
  12.     <div class="col-md-3">
  13.         <div class="form-field-default">
  14.             <label>CEP</label>
  15.             <input class="maskcep" type="text" name="endereco_cep" placeholder="CEP" value="">
  16.         </div>
  17.     </div>
  18.     <div class="col-md-7 col-xs-9">
  19.         <div class="form-field-default">
  20.             <label>Rua</label>
  21.             <input type="text" name="endereco_rua" placeholder="Rua" value="">
  22.         </div>
  23.     </div>
  24.     <div class="col-md-2 col-xs-3">
  25.         <div class="form-field-default">
  26.             <label></label>
  27.             <input type="text" name="endereco_numero" placeholder="Nº" value="">
  28.         </div>
  29.     </div>
  30. </div>
  31. <div class="row">
  32.     <div class="col-md-12">
  33.         <div class="form-field-default">
  34.             <label>Bairro</label>
  35.             <input type="text" name="endereco_bairro" placeholder="Bairro" value="">
  36.         </div>
  37.     </div>
  38. </div>
  39. <link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.3/dist/leaflet.css" />
  40. <link rel="stylesheet" href="https://unpkg.com/esri-leaflet-geocoder@3.1.3/dist/esri-leaflet-geocoder.css">
  41. <script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet.js"></script>
  42. <script src="https://unpkg.com/esri-leaflet@3.0.8/dist/esri-leaflet.js"></script>
  43. <script src="https://unpkg.com/esri-leaflet-geocoder@3.1.3/dist/esri-leaflet-geocoder.js"></script>
  44. <script>
  45.     function showMap() {
  46.       setTimeout(function() {
  47.         const targetId = "location-map";
  48.         const $map = $('#' + targetId);
  49.         const $latlng = $("[name='endereco_latlng']");
  50.         const $nome = $("[name='nome']");
  51.         const latlng = JSON.parse($latlng.val());
  52.         const origin = L.marker(latlng);
  53.         const apikey = "ARCGIS_API_KEY";
  54.         if ($map.data('already-mounted')) {
  55.           return;
  56.         }
  57.         $map.attr('data-already-mounted', true)
  58.         const map = L.map(targetId, {
  59.           maxZoom: 16,
  60.           minZoom: 12,
  61.           zoomControl: false
  62.         });
  63.         const geocodeService = L.esri.Geocoding.geocodeService({
  64.           apikey
  65.         });
  66.         const providers = [
  67.           L.esri.Geocoding.arcgisOnlineProvider({
  68.             apikey,
  69.             countries: ['BRA'],
  70.           })
  71.         ];
  72.         map.setView(origin.getLatLng(), 16).on("click", handleClick);
  73.         origin.addTo(map);
  74.         L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
  75.           attribution: "Map data &copy;  < a href = \"https://openstreetmap.org\">OpenStreetMap < /a>",
  76.         }).addTo(map);
  77.         L.control.zoom({
  78.           position: 'bottomright'
  79.         }).addTo(map);
  80.         L.esri.Geocoding.geosearch({
  81.           providers,
  82.           expanded: true,
  83.           useMapBounds: false,
  84.           collapseAfterResult: false,
  85.           placeholder: 'Pesquisar',
  86.         }).addTo(map).on("results", handleClick);
  87.    
  88.         function handleClick(e) {
  89.           geocodeService.reverse().latlng(e.latlng).run(function(error, result) {
  90.             if (Boolean(error) == false) {
  91.               $("[name='endereco_cep']").val(result.address.Postal).trigger("change");
  92.             }
  93.             map.setView(e.latlng, 16);
  94.             origin.setLatLng(e.latlng);
  95.             $latlng.val(JSON.stringify(Object.values(e.latlng)));
  96.           });
  97.         }
  98.       }, 100);
  99.     }
  100. </script>
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement