How to get coordinates of kml file android

2019-09-08 15:51发布

问题:

I want to receive the coordinates of the points that are in the kml file. I created a object that receives the coordinates but I can not find how to take them from the file.

An example of what has been done so far:
(The amount of points is 132, but I shortened it to be able to present here.)

Kml file:

<LinearRing>
                <coordinates>
                    34.79991805485883,32.070779943443,0
                    34.799829164854,32.07080649750882,0
                    34.79971023480251,32.07083335300256,0
                    34.79959122858838,32.07086022634235,0
                    34.79947508289758,32.07091343448649,0
                    34.79935881388468,32.07096669690968,0
                    34.79923664471844,32.07096729415576,0
                    34.79912177286835,32.07104658864036,0
                </coordinates>
<LinearRing>

In code:

   int i =0; 
   for (KmlContainer containers : kmlLayer.getContainers()) {
       poly[i] =new PointPoly(containers);
       i++;
     }

The code does not work. Would appreciate help.

回答1:

First make sure you got the String containing the coordinates.
(the text between the <coordinates> tag)
Then use

String[] coords3d = coordStr.split(" ");

to split into an array of lon,lat,altitude string. Loop over that array and split each string with

String[] coordLatLonAltitude = coord.split(",");

to split into separate latitude, longitude and altitude values.



回答2:

List<LatLng> points = new ArrayList<>();
for (KmlContainer c : kmlLayer.getContainers()) {
    for (KmlPlacemark p : c.getPlacemarks()) {
        KmlGeometry g = p.getGeometry();
        if (g.getGeometryType().equals("LineString")) {
            points.addAll((Collection<? extends LatLng>) g.getGeometryObject());
        }
    }
}

GeometryType can be different on your kml file, like "LinearRing". But basicaly this is working code for me.



标签: android gps kml