如何将String值转换为LatLng()对象?

Rob*_*bot 1 java android google-maps

我在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]]
";
Run Code Online (Sandbox Code Playgroud)

提前致谢

Nab*_*ari 5

解析您的数据如下:

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.
}
Run Code Online (Sandbox Code Playgroud)