拆分具有经度和纬度点的字符串

Splitting a String which has longitude and latitude points

我有一个 kmlLayer,上面有几个地标。我想遍历这些地标并检索它们的坐标,以便找到从我的位置到这些点的距离,并且只将标记设置在一定距离内可见。 我有以下代码

 for (KmlPlacemark placemark: layer.getPlacemarks()) {
            String s = ("placemarks",placemark.getGeometry()..getGeometryObject().toString());

字符串 s 具有以下格式: lat/lng: (55.94569390835889,-3.190410055779333) 我想从中提取两个坐标。我该怎么做?

因此,如果您不想使用 Regex,请手动操作。首先删除“(”之前和“)”之后的所有内容

s = s.substring(s.indexOf("(")+1,s.indexOf(")")); //this will get you "55...,-3.19..."

然后用","拆分。

String[] strngs = s.split(",");
double lat = Double.parseDouble(strings[0]);
double lon = Double.parseDouble(string[1]);

如果我们可以保证要解析的字符串始终以规范方式定义,即始终定义为纬度+经度,那么使用正则表达式将是最简单的方法:

示例:

 String latLong = "lat/lng: (55.94569390835889,-3.190410055779333)";
 Pattern patte = Pattern.compile("-?[0-9]+(?:.[0-9]+)?");
 Matcher matcher = patte.matcher(latLong);
 while (matcher.find()) {
      System.out.println(Double.parseDouble(matcher.group()));
 }