Select to view content in your preferred language

Showing Layer on SceneView works in Windows XAML but not iOS

620
2
12-10-2019 05:57 PM
KeithMcCall
Emerging Contributor

My goal is to load a KML file to display a layer on a SceneView using C# and Arcgis in both Windows and iOS.

The following code works in Windows XAML but it does not work on iOS.

// Create a new basemap.
Basemap imageryBasemap = Basemap.CreateImageryWithLabels();

// Create and show a new scene with imagery basemap.
MySceneView.Scene = new Scene(imageryBasemap);

KmlLayer layer;

layer = new KmlLayer(new Uri(name)); //name is a local filename

// Add the selected layer to the map.
MySceneView.Scene.OperationalLayers.Add(layer);
await layer.LoadAsync();

await MySceneView.SetViewpointAsync(new Viewpoint(layer.FullExtent));

This displays a KML layer at a specific location and zooms into it.

--------------------------------------------

Using iOS, the file has been included in the application package in a kml directory bundled with the application.

// Create a new basemap.
Basemap imageryBasemap = Basemap.CreateImageryWithLabels();

// Create and show a new scene with imagery basemap.
MySceneView.Scene = new Scene(imageryBasemap);

KmlLayer layer;
try
{
   var uriname = new Uri("file:///./kml/mylayer.kml");
   layer = new KmlLayer(uriname);
}
catch (Exception e)
{
    Console.WriteLine("ERROR OPENING KML FILE: {0}", e.Message);  // No error here.
    return;
}

Console.WriteLine("KML FILE named {0} loaded...", layer.Name); // layer has no Name here which indicates to me that although the above didn't throw an exception the layer wasn't actually loaded.

// Add the selected layer to the map.
MySceneView.Scene.OperationalLayers.Add(layer);

// Zoomed into the approximate area but there is no layer shown on the map.

Envelope myEnvelope = new Envelope(-118.45, 45.92, -118.42, 45.95, SpatialReferences.Wgs84);
MySceneView.SetViewpointAsync(new Viewpoint(myEnvelope));

Any help is appreciated.

Also, any help to load a KmlLayer from a local data element or stream rather than from a URI would be great.

0 Kudos
2 Replies
dotMorten_esri
Esri Notable Contributor

You wouldn't see an error until you try and load the KML Dataset. Ie
await kmlLayer.Dataset.LoadAsync();

KeithMcCall
Emerging Contributor

Thanks that was a good suggestion.  I tried that but still have the same problem on IOS.

The call fails at layer = new KmlLayer(uriname);  I have tried all variants of the file name I can think of, including getting IOS to resolve the path from the file attributes.

The KML layer file is not found by the call to load it.  Here is the code:

Console.WriteLine("------------------------ Can I read the file attributes to find the file name I should be loading??? ---------------------------");
listallfiles();
Console.WriteLine("------------------------ Confirmed ---------------------------------------------");

var text = File.ReadAllText("kml/SevenHills.kml");
Console.WriteLine("------------------------ Can I read the file data??? ---------------------------");
Console.WriteLine(text);
Console.WriteLine("------------------------ Confirmed ---------------------------------------------");

string apppath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)+"PrecisionView.app/";

KmlLayer layer;
try
{
///private/var/containers/Bundle/Application/0DE39905-B65F-4D3B-89E8-03733F22CD97/PrecisionView.app/kml
//BAD: file:///var/mobile/Containers/Data/Application/0EA9D36C-0FCF-470F-8330-A05AB0EB8078/DocumentsPrecisionView.app//PrecisionView.app/kml/SevenHills.kml
//string uripath = String.Format("file://{0}/{1}", apppath, "PrecisionView.app/kml/SevenHills.kml");
string uripath = String.Format("/private/var/containers/Bundle/Application/2B4FAC7A-2D95-4930-B8B3-EDB74E5EE5C1/PrecisionView.app/kml/SevenHills.kml");

Console.WriteLine("------------------------ Can I load the layer??? ---------------------------");

Console.WriteLine("Opening layer at {0}", uripath);

var uriname = new Uri(uripath);
Console.WriteLine("Got uriname *{0}*", uriname);
layer = new KmlLayer(uriname);
await layer.Dataset.LoadAsync();

// Add the selected layer to the map.
MySceneView.Scene.OperationalLayers.Add(layer);
}
catch (Exception e)
{
Console.WriteLine("ERROR OPENING KML LAYER: {0}", e.Message);
}

Code output...

------------------------ Can I read the file attributes to find the file name I should be loading??? ---------------------------
File entry /private/var/containers/Bundle/Application/575AAF82-F750-4EC5-B447-519B475B5162/PrecisionView.app/kml/SevenHills.kml was created on Tuesday, December 10, 2019
 ------------------------ Confirmed ---------------------------------------------
------------------------ Can I read the file data??? ---------------------------
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>SevenHills.kml</name>
<Style id="s_ylw-pushpin">
<IconStyle>
<scale>1.1</scale>
<Icon>
<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
</Icon>
<hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>
</IconStyle>
</Style>
<StyleMap id="m_ylw-pushpin">
<Pair>
<key>normal</key>
<styleUrl>#s_ylw-pushpin</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#s_ylw-pushpin_hl</styleUrl>
</Pair>
</StyleMap>
<Style id="s_ylw-pushpin_hl">
<IconStyle>
<scale>1.3</scale>
<Icon>
<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
</Icon>
<hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>
</IconStyle>
</Style>
<Placemark>

<name>SevenHills</name>
<styleUrl>#m_ylw-pushpin</styleUrl>
<LineString>
<tessellate>1</tessellate>
<coordinates>
-118.4446238031191,45.94000421858365,0 -118.4450094178675,45.93994794787083,0 -118.4498222134527,45.93992062957039,0 -118.4500906993986,45.93985975936327,0 -118.4505174909842,45.93983704458147,0 -118.4506740720017,45.93984775259004,0 -118.4525384944131,45.94040390603141,0 -118.4529241486569,45.94065919135618,0 -118.453620877357,45.94091070219196,0 -118.4547612503623,45.94172367299246,0 -118.4550541786503,45.94188449253917,0 -118.455239083684,45.94199524572046,0 -118.4555545265985,45.94236607539884,0 -118.4559509010189,45.94274036876337,0 -118.4560208178186,45.94329440929713,0 -118.4560468388909,45.94360971654032,0 -118.4561156245048,45.94385465236871,0 -118.4560522801767,45.94408463509967,0 -118.4551206805496,45.94530381009788,0 -118.4533325449118,45.94740862523928,0 -118.4518443683923,45.94898830919681,0 -118.4514731586695,45.94944601639191,0 -118.451028383202,45.94974797366869,0 -1
18.4493239543043,45.95048011909979,0 -118.4477220268395,45.95113079524753,0 -118.447339088874,45.95108785542429,0 -118.4468485708434,45.95039280290377,0 -118.4466113293553,45.95011731022146,0 -118.446356216983,45.94994690914195,0 -118.4461523221155,45.94982942741466,0 -118.4460285096415,45.94968884730044,0 -118.4460329814595,45.94956775407266,0 -118.4461439704801,45.94927619962923,0 -118.4460596672124,45.94916727943071,0 -118.4458574282867,45.94914489020882,0 -118.4455750473521,45.94914791492524,0 -118.4453368324822,45.94898700191206,0 -118.4449551293189,45.94896331920968,0 -118.4444213851763,45.94890554776818,0 -118.4439528720892,45.94885975957928,0 -118.4434563634693,45.94887390132582,0 -118.4430032656326,45.94888625665448,0 -118.442772548067,45.94888083432534,0 -118.4426436296657,45.94876062941377,0 -118.4426003787763,45.94855011202757,0 -118.4424216099796,45.94834787625387,0 -118.4420682021547,45.94809749861648,0 -118.4418570593747,45.94794806768007,0 -118.4416575461201,45.94781784342278,0 -118.4415081315
729,45.94770373683252,0 -118.4415404378177,45.94735740639674,0 -118.4446230976549,45.94733122500705,0 -118.4445857085007,45.94605786991772,0 -118.4446238031191,45.94000421858365,0
</coordinates>
</LineString>
</Placemark>
</Document>
</kml>
 ------------------------ Confirmed ---------------------------------------------
------------------------ Can I load the layer??? ---------------------------
Opening layer at /private/var/containers/Bundle/Application/2B4FAC7A-2D95-4930-B8B3-EDB74E5EE5C1/PrecisionView.app/kml/SevenHills.kml
Got uriname *file:///private/var/containers/Bundle/Application/2B4FAC7A-2D95-4930-B8B3-EDB74E5EE5C1/PrecisionView.app/kml/SevenHills.kml*
ERROR OPENING KML LAYER: File not found

0 Kudos