[英]Is this done with a regular expression? Formatting Latitude Longitude
I am trying to format a polygon (several lat/long coordinates connected together) using Leaflet Draw. 我正在尝试使用Leaflet Draw格式化多边形(连接在一起的多个纬度/经度坐标)。 Here is the format return and what I have done so far:
这是格式返回以及到目前为止我所做的:
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 is this: 不变的坐标是这样的:
//Unaltered Coordinates: LatLng(46.58907, -102.74414),LatLng(46.58907, -102.74414),LatLng(46.58907, -102.74414)
PolyParse is this (only numbers, dashes, and decimal points left): PolyParse是这样的(仅保留数字,破折号和小数点):
//Polyparse: 46.58907,-102.74414,46.58907,-102.74414,46.58907,-102.74414
PolyparseRegExpt is this(it keeps removing ALL commas :-( ): PolyparseRegExpt是这个(它会删除所有逗号:-():
//Polyparse2: 46.58907 -102.74414 46.58907 -102.74414 46.58907 -102.74414
What is need: Comma 1, 3, 5, 7.... etc removed so that I have: 需要什么:删除逗号1、3、5、7 ...等,以便获得:
number number, number number, number number,........ 号码,号码,号码.....
Basilcally, every odd numbed comma. 基本地,每个奇数麻木的逗号。 right now it's removing all commas for some reason.
目前,出于某种原因,它删除了所有逗号。
The simplest solution (?) - add the other coordinate (preceded by it's comma), making it a pair, capturing it separately without the comma: 最简单的解决方案-添加其他坐标(由它的前面逗号),使之成为一对,分别捕获它没有逗号(?):
([^,]+[^,]),([^,]+[^,],)
replacing it with 替换为
$1 $2
(space between and trailing) $1 $2
(空格和尾随空格)
reforms the coordinate pair, without the separating comma but leaving the trailing. 重新设置坐标对,不使用逗号,但保留结尾。
It looks like coords
is an array of Leaflet's LatLng
s . 看起来
coords
是Leaflet的LatLng
的数组。 Using toString()
is an odd thing to do, considering that you can use .lat
and .lng
to access the LatLng
's properties. 考虑到可以使用
.lat
和.lng
来访问LatLng
的属性,因此使用toString()
是一件奇怪的事情。
It also looks like you're trying to convert your data into WKT (Well-Known-Text) due to yur use of spaces and commas. 由于您使用空格和逗号,因此您似乎还试图将数据转换为WKT(Well-Known-Text)。
So you can convert a LatLng
into a WKT coordinate pair with something like: 因此,您可以使用以下方法将
LatLng
转换为WKT坐标对:
var latlng = L.latLng(...)
var wktPoint = latlng.lng + ' ' + latlng.lat;
And, if you have an array of LatLng
s, you can use Array.prototype.map
and Array.prototype.join
to great effect here: 而且,如果您有一个
LatLng
数组,则可以在这里使用Array.prototype.map
和Array.prototype.join
发挥巨大作用:
var coords = [ L.latLng(...), L.latLng(...), ... ];
var wktLineString = coords.map(function(ll){
// For every item in the array, return a string.
// The call to .map() will thus return an array of strings
return ll.lng + ' ' + ll.lat;
// Once the array of strings is ready, join those strings
// with the given separator.
}).join(',');
As I'm assuming you're using WKT, note the swapping of the coordinates so you can have lng-lat instead of lat-lng. 当我假设您正在使用WKT时,请注意坐标的交换,以便可以使用lng-lat而不是lat-lng。 Also note you can use
toFixed()
and the like to have better control of the output. 还要注意,您可以使用
toFixed()
之类的控件更好地控制输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.