function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Pradeep BandarpalliPradeep Bandarpalli 

[Leaflet] When a certain location is searched, I want the location latitude and longitude details and the marker on the leaflet map for Lightning component.

HI, 
I have a requirement when a certain location is searched, I want its latitude and longitude details and a marker pointing on the map. I have tried with L.GeoSearch and L.Control.Geocoder.Nominatim() but nothing worked for me. Any help here is very much appreciated.

My code with L.GeoSearch
---Component----
<aura:component >
 <aura:attribute name="map" type="Object"/>
     <ltng:require styles="/resource/leaflet/leaflet.css,
                          /resource/lgeosearch/l.geosearch.css" />
    <ltng:require scripts="/resource/leaflet/leaflet.js,
                           /resource/lgeosearch/l.control.geosearch.js,
                           /resource/lgeosearch/l.geosearch.provider.openstreetmap.js"
                  afterScriptsLoaded="{!c.jsLoaded}"/>
    <div id="map"></div>
</aura:component>

---Controller----
({
    jsLoaded : function(component, event, helper) {
        var osmTileUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
        var basemap = new L.TileLayer(osmTileUrl, {maxZoom: 18});
        var map = new L.Map('map', {
            layers: [basemap],
            center: new L.LatLng(53.2, 5.8), zoom: 12
        });
        new L.Control.GeoSearch({
            provider: new L.GeoSearch.Provider.Google()
        }).addTo(map);
        var googleGeocodeProvider = new L.GeoSearch.Provider.Google(),
            addressText = 'Amsterdam';
      googleGeocodeProvider.GetLocations( addressText, function ( results ) {
            latLng= new L.LatLng(results[0].center.lat, results[0].center.lng);
            marker = new L.Marker (latLng);
            map.addlayer(marker);
});

code with Control.Geocoder:
<aura:component >
<aura:attribute name="map" type="Object"/>
  <ltng:require styles="/resource/leaflet/leaflet.css,
                          /resource/leafletcontrolgeocoder/Control.Geocoder.css" />
    <ltng:require scripts="/resource/leaflet/leaflet.js,
                           /resource/leafletcontrolgeocoder/Control.Geocoder.js"
                  afterScriptsLoaded="{!c.jsLoaded}"/>
    <div id="map"></div>
</aura:component>

controller:
({
    jsLoaded : function(component, event, helper) {
        var map = L.map('map').setView([17.3700, 78.4800], 12);
        L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
            attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
        }).addTo(map);
        geocoder = new L.Control.Geocoder.Nominatim();
        window.alert(geocoder);
        var yourQuery = "New York NY";
        geocoder.geocode(yourQuery, function(results) {
            latLng= new L.LatLng(results[0].center.lat, results[0].center.lng);
            marker = new L.Marker (latLng);
            map.addlayer(marker);
        });
        component.set("v.map", map);
        }
    }

---CSS---> width:100%; height:100%;

Where I am going wrong? Thank you for your support.
Pradeep BandarpalliPradeep Bandarpalli
typo its, I am using OpenStreetMap provider in L.GeoSearch:
var googleGeocodeProvider = new L.GeoSearch.Provider.OpenStreetMap(),
David Roberts 4David Roberts 4
Can someone outline the steps required to implement Leaflet.GeoSearch, please?

Here's what I've done so far:
Downloaded the leaflet-geosearch-develop code..I zipped this and created a static resource, leafletgeosearch, in my sandbox.
[I wasn't sure about npm stuff but installed Node.js anyway and therefore also have a ...Users...node_modules folder with a leaflet-geosearch sub folder. I zipped this and tried it in SF but got other errors].

My lightning component is
<aura:component >
<!-- LeafletGeoSearch.cmp -->    
    <ltng:require styles="/resource/leafletgeosearch/leaflet-geosearch/assets/css/leaflet.css" />
    <ltng:require scripts="/resource/leafletgeosearch/leaflet-geosearch/lib/providers/openStreetMapProvider.js"
             afterScriptsLoaded="{!c.jsLoaded}"/>   
</aura:component>

This fails with error:
Uncaught ReferenceError: exports is not defined throws at https://.....lightning.force.com/resource/leafletgeosearch/leaflet-geosearch/lib/providers/openStreetMapProvider.js:5:23

As you can see, I didn't get very far.
Hope someone can help.