Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将字符串值转换为LatLng()对象?_Java_Android_Google Maps - Fatal编程技术网

Java 如何将字符串值转换为LatLng()对象?

Java 如何将字符串值转换为LatLng()对象?,java,android,google-maps,Java,Android,Google Maps,我在字符串中有latlng值。我想将该字符串转换为latlng对象。likeLatLng LatLng=新LatLng(lat,lng) 这是我的数据: String latlan =" [[13.041695199971244, 77.61311285197735], [13.042000923637021, 77.61313531547785], [13.041830750574812, 77.61335827410221], [13.04150706

我在字符串中有latlng值。我想将该字符串转换为latlng对象。like
LatLng LatLng=新LatLng(lat,lng)

这是我的数据:

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

提前感谢

我只需要使用一组拆分和替换来将其拆分,然后在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
}

按如下方式分析数据:

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.
}
列表坐标=新的ArrayList();
试一试{
JSONArray JSONArray=新JSONArray(拉特兰);
for(int i=0;i
私有void doConvertToLatLan(){
字符串latlan=“[13.041695199971244,77.61311285197735],[13.042000923637021,77.61313531547785],[13.041830750574812,77.61335827410221],[13.041507062142946,77.61269208043814]”;
latlan=latlan。替换(“[[”,“[”);
latlan=latlan.replace(“]]”,“]”);
latlan=latlan.replace(“[”,”);
latlan=latlan.replace(“],”,“@”);
字符串[]latlanDParts=latlan.split(“@”);
ArrayList数据=新的ArrayList();
用于(字符串值:latlanDParts){
字符串[]llReplacedParts=value.split(“,”);
添加(新的LatLan(llReplacedParts[0],llReplacedParts[1]);
}
Log.d(“Data”,Data.toString());
}
私有类拉特兰{
专用字符串lat、lan;
公共LatLan(字符串lat,字符串lan){
this.lat=lat;
this.lan=lan;
}
公共字符串getLat(){
返回lat;
}
公共void setLat(字符串lat){
this.lat=lat;
}
公共字符串getLan(){
返回局域网;
}
公共无效设置lan(字符串lan){
this.lan=lan;
}
};

我希望这会有帮助

如果只有一个值,则它将起作用

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

您需要解析JSON格式的字符串。此数据不是JSON格式。它是字符串。什么是
LatLan
?您的意思可能是
LatLng
?您需要将
latlan
字符串解析为JsonArray,然后遍历它们。看,它确实是JSON格式。不知道为什么你认为它不是字符串。拆分(“],[”);什么是stringMy不好,修复了输入错误。你必须转义这些字符,比如
split(\\],\\[”)
我得到的输出是这样的lat lan:lat/lng:(13.0,77.0)这不是Working@Robot您预期的输出类型是什么?[lat/lng:(13.042027380474938,77.61307530105115),lat/lng:(13.042088133203173,77.61339481920004),lat/lng:(13.041947030069515,77.6134082302451),lat/lng:(13.041881377889627,77.61312458664179)]像这样,我没有得到完美的值
LatLng newlatlng = new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude));