admin管理员组

文章数量:1416083

I'm trying to convert a layer.GetLatLngs to an array with only numbers,

[[17.68397, 1.98629],[17.6777, 1.96555],[17.66679, 1.9648],[17.6611, 1.99233]]

Problem is that the output is something along

LatLng(17.50438, 1.04772),LatLng(17.48686, 1.05775)

What's the best way to do a number only conversion to an array?

//Snippet of the code

map.on('draw:created', function (e) {

var type = e.layerType,
    layer = e.layer;
if (type === 'polygon') {
    layer.on('click', function() {
        window.alert(polys);    
    });
}

drawnItems.addLayer(layer);
polys = layer.getLatLngs();
});

I'm trying to convert a layer.GetLatLngs to an array with only numbers,

[[17.68397, 1.98629],[17.6777, 1.96555],[17.66679, 1.9648],[17.6611, 1.99233]]

Problem is that the output is something along

LatLng(17.50438, 1.04772),LatLng(17.48686, 1.05775)

What's the best way to do a number only conversion to an array?

//Snippet of the code

map.on('draw:created', function (e) {

var type = e.layerType,
    layer = e.layer;
if (type === 'polygon') {
    layer.on('click', function() {
        window.alert(polys);    
    });
}

drawnItems.addLayer(layer);
polys = layer.getLatLngs();
});
Share Improve this question edited Jul 17, 2018 at 10:58 darkertime asked Jul 17, 2018 at 9:58 darkertimedarkertime 331 silver badge7 bronze badges 4
  • like you need the output to be [[17.68397, 1.98629],[17.6777, 1.96555],[17.66679, 1.9648],[17.6611, 1.99233]] input is LatLng(17.50438, 1.04772),LatLng(17.48686, 1.05775) – Beginner Commented Jul 17, 2018 at 10:06
  • They are two examples, but yes, in general I need the coordinates in an array without Latlng. – darkertime Commented Jul 17, 2018 at 10:14
  • so the "LatLng(17.50438, 1.04772),LatLng(17.48686, 1.05775)" is a string isn't – Beginner Commented Jul 17, 2018 at 10:19
  • Actually, it counts as an array getLatLngs() LatLng[] Returns an array of the points in the path, or nested arrays of points in case of multi-polyline. – darkertime Commented Jul 17, 2018 at 10:25
Add a ment  | 

2 Answers 2

Reset to default 7

getLatlngs return an array of LatLng objects. To get a list of coordinate pairs, you can use Array.map to transform each object. For example,

layer.getLatLngs().map(function(point) {
    return [point.lat, point.lng];
});

And a demo

var latlngs = [
    [45.51, -122.68],
    [37.77, -122.43],
    [34.04, -118.2]
];
var layer = L.polyline(latlngs);
var resp = layer.getLatLngs().map(function(point) {
    return [point.lat, point.lng];
});

console.log(resp)
<script src="https://cdnjs.cloudflare./ajax/libs/leaflet/1.3.1/leaflet.js"></script>

or if your intent is rather to get a representation of your layer, use toGeoJSON instead to get the underlying geometry:

layer.toGeoJSON().geometry.coordinates

Here's another demo with a polygon

var latlngs = [[37, -109.05],[41, -109.03],[41, -102.05],[37, -102.04]];
var layer = L.polygon(latlngs);
console.log(layer.toGeoJSON().geometry.coordinates)
<script src="https://cdnjs.cloudflare./ajax/libs/leaflet/1.3.1/leaflet.js"></script>

i hope the below code works. Please check and let me know. happy Coding :)

let string = "LatLng(17.50438, 1.04772),LatLng(17.48686, 1.05775)",
    splittedArray = string.split("LatLng"),
    outputArray = [];
 
splittedArray.forEach(o => {
  if(o){
   let extractedLTLNG =  o.match(/\(([^)]+)\)/)[1].split(",");
   let tempArray = [extractedLTLNG[0], extractedLTLNG[1]];
     outputArray.push(tempArray);
  }else{
   return;
  }
})

console.log(outputArray);

本文标签: javascriptLeaflet Getlatlngs to numberStack Overflow