I've seen that similar questions have been asked, bust most of them are over a year old. Is there a way to add a KmlLayer from a KML string or a local file?
I tried the following, but it only works when file is a url.
string file = @"C:\devel\test.kml"; KmlLayer kmlLayer = new KmlLayer(new Uri(file)); MyMapView.Map.Layers.Add(kmlLayer);
Solved! Go to Solution.
For anyone interested, I was able to make it work by adding the KML layer with
MyMap.Layers.Add(kmlLayer);
instead of
MyMapView.Map.Layers.Add(kmlLayer);
string file = @"C:\devel\test.kml"; KmlLayer kmlLayer = new KmlLayer(new Uri(file)); MyMap.Layers.Add(kmlLayer);
I wasn't able to do it with a string, but a file is good enough.
Jason,
I think it only takes a URI, you have to convert it no matter what the source is. Here's their official example of dragging and dropping a local file to the Map, and it looks like they did the same thing you did:
private async void MyMapView_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
KmlLayer kmlLayer = new KmlLayer(new Uri(files[0]));
await kmlLayer.InitializeAsync();
//Add the kml layer
MyMapView.Map.Layers.Add(kmlLayer);
//Zoom to the kml layer if available
if (kmlLayer.RootFeature.Viewpoint != null)
await MyMapView.SetViewAsync(kmlLayer.RootFeature.Viewpoint);
}
}Hope this helps.
For anyone interested, I was able to make it work by adding the KML layer with
MyMap.Layers.Add(kmlLayer);
instead of
MyMapView.Map.Layers.Add(kmlLayer);
string file = @"C:\devel\test.kml"; KmlLayer kmlLayer = new KmlLayer(new Uri(file)); MyMap.Layers.Add(kmlLayer);
I wasn't able to do it with a string, but a file is good enough.