KML string to KmlLayer

2773
2
Jump to solution
08-04-2015 07:20 AM
JasonTrinidad1
New Contributor III

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);
0 Kudos
1 Solution

Accepted Solutions
JasonTrinidad1
New Contributor III

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.

View solution in original post

2 Replies
KennethLathan
New Contributor II

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.

0 Kudos
JasonTrinidad1
New Contributor III

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.