【发布时间】:2017-07-07 15:27:49
【问题描述】:
我正在尝试使用 Leaflet Draw 格式化一个多边形(几个连接在一起的纬度/经度坐标)。这是格式返回和我到目前为止所做的:
map.on("draw:drawstop", polyPointToString);
function polyPointToString(e){
console.log("Unaltered Coordinates: " + coords);
polyStringParse = coords.toString().replace(/[^\d,.-]/g, '');
console.log("Polyparse: " + polyStringParse);
polyStringParseRegExp = polyStringParse.replace(/([^,]+[^,]),/g,'$1 ');
console.log("PolyparseRegExp: " + polyStringParseRegExp);
}
原坐标是这样的:
//Unaltered Coordinates: LatLng(46.58907, -102.74414),LatLng(46.58907, -102.74414),LatLng(46.58907, -102.74414)
PolyParse 是这样的(只剩下数字、破折号和小数点):
//Polyparse: 46.58907,-102.74414,46.58907,-102.74414,46.58907,-102.74414
PolyparseRegExpt 是这个(它不断删除所有逗号:-():
//Polyparse2: 46.58907 -102.74414 46.58907 -102.74414 46.58907 -102.74414
需要什么:逗号 1、3、5、7.... 等已删除,以便我拥有:
号码号码,号码号码,号码号码,........
基本上,每个奇怪的麻木逗号。现在由于某种原因它正在删除所有逗号。
【问题讨论】:
-
coords是什么,不是作为字符串,而是作为对象?将您的console.log更改为console.log("Unaltered Coordinates: ", coords)。将对象转换为字符串表示形式,然后将其解析出来会使这变得不必要地复杂。 -
coords 是地图上的点数组。
-
我认真地认为您使用正则表达式格式化此数组的方法是the XY problem
标签: javascript regex leaflet