问题
Using this plugin: https://github.com/sushihangover/SushiHangover.Android.Maps.Utils
I am successfully adding a route to a my Google Map by adding a kml-layer to it. I foreach the lat, lng and via a Polyline I create a line! The problem I have is that my KML-file however has multiple routes and with my current code only one of the routes gets added to the map.
How do I adjust my code in order to get every single route stored in my KML-file?
My KML looks like this:
<Folder>
<name>Tracks</name>
<description>A list of tracks</description>
<visibility>1</visibility>
<open>0</open>
<Placemark>
<visibility>0</visibility>
<open>0</open>
<styleUrl>#red</styleUrl>
<name>trackone</name>
<description>Track no. 1</description>
<LineString>
<extrude>true</extrude>
<tessellate>true</tessellate>
<altitudeMode>clampToGround</altitudeMode>
<coordinates>
10.366653,26.281982,106.075562 10.366759,56.282024,99.504028 10.366846,26.282043,95.945312
</coordinates>
</LineString>
</Placemark>
</Folder>
<Folder>
<name>Tracks</name>
<description>A list of tracks</description>
<visibility>1</visibility>
<open>0</open>
<Placemark>
<visibility>0</visibility>
<open>0</open>
<styleUrl>#green</styleUrl>
<name>tracktwo</name>
<description>Track no. 2</description>
<LineString>
<extrude>true</extrude>
<tessellate>true</tessellate>
<altitudeMode>clampToGround</altitudeMode>
<coordinates>
10.299386,26.278042,84.550720
10.299453,26.278004,83.942444
10.299522,26.277962,85.036560
10.299572,26.277916,85.828735
</coordinates>
</LineString>
</Placemark>
</Folder>
<Folder>
<name>Waypoints</name>
<description>A list of waypoints</description>
<visibility>1</visibility>
<open>0</open>
<Placemark>
<name>name</name>
<visibility>1</visibility>
<open>0</open>
<description>
No info available </description>
<LookAt>
<longitude>18.581586295142404</longitude>
<latitude>36.313142255580445</latitude>
<range>500</range>
<tilt>45</tilt>
<heading>0</heading>
</LookAt>
<Point>
<coordinates>
10.581586295142404,36.313142255580445
</coordinates>
</Point>
</Placemark>
<Placemark>
<name>name</name>
<visibility>1</visibility>
<open>0</open>
<description>
No info available </description>
<LookAt>
<longitude>10.378910994617264</longitude>
<latitude>36.285880605439296</latitude>
<range>500</range>
<tilt>45</tilt>
<heading>0</heading>
</LookAt>
<Point>
<coordinates>
11.378910994617264,26.285880605439296
</coordinates>
</Point>
</Placemark>
</Folder>
...Etc with a few more routes in there. My code looks like this:
var container = (KmlContainer)kmlLayer.Containers.Iterator().Next();
container = (KmlContainer)container.Containers.Iterator().Next();
var placemark = (KmlPlacemark)container.Placemarks.Iterator().Next();
if (placemark.HasGeometry && placemark.Geometry is KmlLineString)
{
var lineString = placemark.Geometry as KmlLineString;
var latlngArray = lineString.GeometryJavaObject() as Java.Util.ArrayList;
var polylineOptions = new PolylineOptions();
polylineOptions.InvokeColor(0x66FF0000);
using (var builder = new LatLngBounds.Builder())
{
foreach (LatLng latLng in latlngArray.ToEnumerable())
{
builder.Include(latLng);
polylineOptions.Add(new LatLng(latLng.Latitude, latLng.Longitude));
}
}
map.AddPolyline(polylineOptions);
I am very new with KML-files but if i understand it correctly do I need to foreach out each single Cointaner? I tried to do something along these lines:
for (var contain = (KmlContainer)kmlLayer.Containers.Iterator(); contain.Containers.Iterator().Next();)
{
//add above code in here
}
Or something like this where I store it as a list:
List<KmlContainer> cointainers = kmlLayer.Containers.Iterator().Next() as List<KmlContainer>;
And add all of the above code inside that for-loop. But the syntax is completely wrong and I am not sure on how to loop out each container.
How do I successfully get out all of the routes in my KML-file and it's coordinates?
UPDATED CODE:
foreach (KmlContainer container in kmlLayer.Containers.ToEnumerable())
{
System.Diagnostics.Debug.WriteLine("1"); // i reach this
foreach (var property in container.Properties.ToEnumerable())
{
//This is a Java HashMap<string, string> ....
System.Diagnostics.Debug.WriteLine("2"); //i reach this
}
if (container.HasPlacemarks)
{
System.Diagnostics.Debug.WriteLine("3"); //i do not reach this which is strange since I use the same KML as b4 that found placemarks.
}
foreach (KmlPlacemark placemark in container.Placemarks.ToEnumerable())
{
System.Diagnostics.Debug.WriteLine("4"); // i do not reach this
if (placemark.HasGeometry && placemark.Geometry is KmlLineString)
{
System.Diagnostics.Debug.WriteLine("5"); //not this
var lineString = placemark.Geometry as KmlLineString;
var latlngArray = lineString.GeometryJavaObject() as Java.Util.ArrayList;
var polylineOptions = new PolylineOptions();
polylineOptions.InvokeColor(0x66FF0000);
using (var builder = new LatLngBounds.Builder())
{
foreach (LatLng latLng in latlngArray.ToEnumerable())
{
builder.Include(latLng);
polylineOptions.Add(new LatLng(latLng.Latitude, latLng.Longitude));
}
}
map.AddPolyline(polylineOptions);
}
else
{
System.Diagnostics.Debug.WriteLine("6"); //not this
}
}
}
回答1:
A KML Folder will be turned into KmlContainer, you need to loop over all the KMLLayer containers and extract the elements you need:
Note: this is using Linq's ToEnumerable to convert the Java Iterator
Example:
if (kmlLayer.Containers != null)
{
foreach (KmlContainer container in kmlLayer.Containers.ToEnumerable())
{
foreach (var property in container.Properties.ToEnumerable())
{
//This is a Java HashMap<string, string> ....
Log.Debug(Constants.TAG, $"{property.ToString()} : {container.GetProperty(property.ToString())}");
}
foreach (KmlPlacemark placemark in container.Placemarks.ToEnumerable())
{
Log.Debug(Constants.TAG, placemark.ToString());
}
}
}
Output:
SomeTag: visibility : 1
SomeTag: name : Tracks
SomeTag: description : A list of tracks
SomeTag: open : 0
SomeTag: Placemark{
SomeTag: style id=#red,
SomeTag: inline style=null
SomeTag: }
SomeTag: visibility : 1
SomeTag: name : Tracks
SomeTag: description : A list of tracks
SomeTag: open : 0
SomeTag: Placemark{
SomeTag: style id=#green,
SomeTag: inline style=null
SomeTag: }
To Iterate Container w/ Containers:
if (kmlLayer.HasContainers)
{
void IterateProperties(KmlContainer containers)
{
foreach (var property in containers.Properties.ToEnumerable())
Log.Debug(Constants.TAG, $"{property.ToString()} : {containers.GetProperty(property.ToString())}");
}
void IterateLineString(KmlLineString lineString)
{
var latlngArray = lineString.GeometryJavaObject() as Java.Util.ArrayList;
foreach (LatLng item in latlngArray.ToEnumerable())
{
Log.Debug(Constants.TAG, $"{item.Latitude}:{item.Longitude}");
}
}
void IteratePlaceMarks(KmlContainer container)
{
foreach (KmlPlacemark placemark in container.Placemarks.ToEnumerable())
{
IterateProperties(container);
Log.Debug(Constants.TAG, placemark.ToString());
if (placemark.HasGeometry & placemark.Geometry is KmlLineString)
IterateLineString(placemark.Geometry as KmlLineString);
}
}
void IterateSubContainers(KmlContainer container)
{
IterateProperties(container);
IteratePlaceMarks(container);
if (container.HasContainers)
{
foreach (KmlContainer subContainer in container.Containers.ToEnumerable())
IterateSubContainers(subContainer);
}
}
foreach (KmlContainer container in kmlLayer.Containers.ToEnumerable())
IterateSubContainers(container);
}
来源:https://stackoverflow.com/questions/45786038/how-to-get-out-all-routes-placemark-linestring-in-a-kml-file-when-added-to-go