You need to sign in to do that
Don't have an account?
Pradeep 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: '© <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.
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: '© <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.
var googleGeocodeProvider = new L.GeoSearch.Provider.OpenStreetMap(),
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.