Reputation: 10581
I have this
var markersList = document.getElementById('markersList');
L.MarkerCluster.include({
spiderfy: function(e) {
var childMarkers = this.getAllChildMarkers();
this._group._unspiderfy();
this._group._spiderfied = this;
// Fill the markersList.
markersList.innerHTML = childMarkers
.map((marker, index) => `<li>Marker ${index + 1}:
${marker.getLatLng()}</li>`)
.join('');
// Show the modal.
console.log(markersList);
$(".modalResult").modal('show');
},...
The above gives me in console:
<li>Marker 1: LatLng(45.49934, 9.37966)</li>
<li>Marker 2: LatLng(45.49934, 9.37966)</li>
I need to be able to get as a string 45.49934, 9.37966
since they are the same values, I only need one and I need it as a string as I will have to insert it as an input value later on:
<input type="hidden" value="45.49934, 9.37966">
Upvotes: 1
Views: 49
Reputation: 30370
If I understand your question correctly, then you can extract the lat and lng values from the resut of getLatLng()
via the following regular expression:
/(-?\d+.\d*)/gi
passed to .match()
This regular expression will extract the two numbers from the result of getLatLng()
; one for lat and the other for lng, which can then be combined to aquire the requiredString
that you'd use for the input elements value attribute:
{
spiderfy: function(e) {
var childMarkers = this.getAllChildMarkers();
this._group._unspiderfy();
this._group._spiderfied = this;
// Fill the markersList.
markersList.innerHTML = childMarkers.map((marker) =>
`<li>Marker: ${marker.getLatLng()}</li>`).join('');
// If there are any childMarkers
if(childMarkers.length > 0) {
// Match the lat and lng numbers from the string returned by getLatLng()
const [lat, lng] = `${ childMarkers[0].getLatLng() }`
.match(/(-?\d+.\d*)/gi);
// Construct the required string value from the extracted numbers
const requiredString = `${ lat }, ${ lng }`;
// Use requiredString to populate the value attribute of the
// input field in OP
console.log(requiredString);
}
// Show the modal.
console.log(markersList);
$(".modalResult").modal('show');
}
}
Upvotes: 1