Select to view content in your preferred language

How to add KML/KMZ/MXD to layers on a map in C#

10367
13
03-31-2011 05:43 PM
YouKnow_It
Emerging Contributor
Hi forumers, may i know to add a kml/ kmz/ mxd file into visual studio ( silverlight 4 in C# )? I couldnt use ArcGIS server to upload a map as a webservice and use it as a layer in my project. Anyone knows please help 🙂
0 Kudos
13 Replies
TonyBacon
Deactivated User
I am looking into adding kml layers to a silverlight client application. So far I have been able to add the kml files using the kmllayer class and a proxy however there are errors trying to access kmz files. Can anyone tell me if kmz files are supported?
0 Kudos
dotMorten_esri
Esri Notable Contributor
Thank you for your reply! To be clear, though, I am not wanting to load a kml from a local machine/client to the silverlight app/host/server. Rather, I would like the host map data to come from a kml file on the server (with no user interaction). This can be done with Javascript, I believe. Am I still barking up the wrong tree?

Another way to clarify is that I do not have the capability to serve out my own data as a service. Therefore I must find a way to host my data in another form (perhaps similar to a Graphics Layer stored with the Silverlight Assets?). If this is not viable, maybe Silverlight is not the way to go for me. I do see that others on this list have asked about ways to host kmls and shapefiles directly in their maps.


Did you look at the SDK sample? http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#KmlLayerSimple
It pulls KML data straight from a website:
   <esri:KmlLayer ID="KmlLayer" Url="http://kml-samples.googlecode.com/svn/trunk/KML_Samples.kml" />
0 Kudos
ElizabethMiller
Regular Contributor
Did you look at the SDK sample? http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#KmlLayerSimple
It pulls KML data straight from a website:
   <esri:KmlLayer ID="KmlLayer" Url="http://kml-samples.googlecode.com/svn/trunk/KML_Samples.kml" />


My bad. I did look at the sample but I thought it would not work for me. I took a second look and realized it would. Thanks for pointing me back in the right direction!
0 Kudos
MaciejGolebiewski
Emerging Contributor
You cannot read files directly from a local folder on disk if you are using Silverlight (that would be a HUGE security risk). You can use the OpenFileDialog class to let the user browse to a kml file. This will give you a stream to the kml file. Next use the SetSource method and use the stream as the parameter. Last add KML layer that to your map.
Something along the lines of this (untested):
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "KML files (.kml)|*.kml|Compressed KML files 
(.kmz)|*.kmz|All Files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;

bool? userClickedOK = openFileDialog1.ShowDialog();
if (userClickedOK == true)
{
                System.IO.Stream fileStream = openFileDialog1.File.OpenRead();
                var layer = new KmlLayer();
                layer.SetSource(myFiledDialog.File);
                myMap.Layers.Add(layer);
}


Anyone managed to run this concept? I need to allow users to upload KML file on which they will be able to perform a query.
0 Kudos