【问题标题】:How to convert String values into LatLng() objects?如何将字符串值转换为 LatLng() 对象?
【发布时间】:2017-10-03 13:04:31
【问题描述】:

我在 String 中有 latlng 值。我想将该 String 转换为 LatLng 对象。赞LatLng latlng = new LatLng(lat, lng);

这是我的数据:

String latlan =" 
    [[13.041695199971244, 77.61311285197735], 
    [13.042000923637021, 77.61313531547785], 
    [13.041830750574812, 77.61335827410221], 
    [13.041507062142946, 77.61269208043814]]
";

提前致谢

【问题讨论】:

  • 你需要解析JSON格式的字符串。
  • 此数据不是 JSON 格式。它是字符串。
  • 什么是LatLan?可能你的意思是LatLng
  • 您需要将latlan 字符串解析为JsonArray,然后遍历它们。见here
  • 确实是JSON格式。不知道为什么你认为它不是

标签: java android google-maps


【解决方案1】:

按如下方式解析您的数据:

List<LatLng> coordinates = new ArrayList<>();
try {
    JSONArray jsonArray = new JSONArray(latlan);
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONArray latLong = jsonArray.getJSONArray(i);
        double lat = latLong.getDouble(0);
        double lon = latLong.getDouble(1);
        coordinates.add(new LatLng(lat, lon));
    }
} catch (JSONException e) {
    e.printStackTrace();
}

System.err.println(Arrays.toString(coordinates.toArray()));

for (LatLng latLng : coordinates) {
    //use the coordinates.
}

【讨论】:

  • 我得到这样的输出 lat lan : lat/lng: (13.0,77.0)
  • 这行不通
  • @Robot 你期待什么类型的输出?
  • [纬度/经度:(13.042027380474938,77.61307530105115),纬度/经度:(13.042088133203173,77.61339481920004),纬度/经度:(13.041947030069515,77.6134082302451),纬度/经度:(13.041881377889627,77.61312458664179)]等这个
  • 看看我没有以完美的方式获得价值
【解决方案2】:

我会使用一堆拆分和 replaceAlls 来划分它,然后在 foreach 循环中使用 LatLan 构造函数。

String latlng = "[[13.041695199971244, 77.61311285197735], [13.042000923637021, 77.61313531547785], [13.041830750574812, 77.61335827410221], [13.041507062142946, 77.61269208043814]]";

String[] latlngParts = latlng.split("\\], \\[");

for (String ll: latlngParts) {
    String llReplaced = ll.replaceAll("\\[", "").replaceAll("\\]", "");
    String[] llReplacedParts = llReplaced.split(", ");

    LatLng latlngObj = new LatLng(llReplacedParts[0], llReplacedParts[1]);

    // Then add latlngObj to some collection of LatLng objects
}

【讨论】:

  • 在 string.split("] , [");什么是字符串
  • 我的错,修正了错别字
  • 哦,你必须转义那些字符,像这样split("\\], \\[")
【解决方案3】:
private  void doConvertToLatLan(){
        String latlan = "[[13.041695199971244, 77.61311285197735], [13.042000923637021, 77.61313531547785], [13.041830750574812, 77.61335827410221], [13.041507062142946, 77.61269208043814]]";
        latlan = latlan.replace("[[","[");
        latlan = latlan.replace("]]","]");
        latlan = latlan.replace("[","");
        latlan = latlan.replace("],","@");
        String[] latlanDParts = latlan.split("@");
        ArrayList<LatLan> data = new ArrayList<>();
        for (String value: latlanDParts) {
            String[] llReplacedParts = value.split(",");
            data.add(new LatLan(llReplacedParts[0], llReplacedParts[1]));
        }
        Log.d("Data",data.toString());
    }

    private class LatLan{
        private String lat,lan;

        public LatLan(String lat, String lan) {
            this.lat = lat;
            this.lan = lan;
        }

        public String getLat() {
            return lat;
        }

        public void setLat(String lat) {
            this.lat = lat;
        }

        public String getLan() {
            return lan;
        }

        public void setLan(String lan) {
            this.lan = lan;
        }
    };

我希望这会有所帮助!

【讨论】:

    【解决方案4】:

    如果你只有一个值,它就会起作用。

    LatLng newlatlng = new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-11
      • 2021-02-03
      • 2018-07-19
      • 1970-01-01
      • 2023-01-12
      • 2019-07-07
      相关资源
      最近更新 更多