Select to view content in your preferred language

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

10100
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
dotMorten_esri
Esri Notable Contributor
0 Kudos
YouKnow_It
Emerging Contributor
Is this what you were looking for?
http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#KmlLayerSimple


hi thanks for the reply! But i am looking something that it can be using KML file(s) directly from the working directory instead of web services (i couldnt publish kml file as a webservice; nor could'nt use ArcGIS Server as well) at this line:

<esri:KmlLayer ID="KmlLayer" Url="http://kml-samples.googlecode.com/svn/trunk/KML_Samples.kml"
                           ProxyUrl="http://serverapps.esri.com/SilverlightDemos/ProxyPage/proxy.ashx" />

Looking forward for people to find solution(s) of what i currently has, thanks.
0 Kudos
DanielWalton
Frequent Contributor
You need to start with the source code for the KMLLayer classes mentioned above (get the bits from codeplex). Then modify the constructor and initializer code to accept a stream or byte array as a source input (instead of setting source URI). Find the place in there where the webclient_downloadcomplete routine hands off the stream to the kml parser and plug in your source data there. The rest should work as-is.
0 Kudos
YouKnow_It
Emerging Contributor
You need to start with the source code for the KMLLayer classes mentioned above (get the bits from codeplex). Then modify the constructor and initializer code to accept a stream or byte array as a source input (instead of setting source URI). Find the place in there where the webclient_downloadcomplete routine hands off the stream to the kml parser and plug in your source data there. The rest should work as-is.


hmm danwallie, any codes for me to reference? I'm new for ESRI and I urgently need these code for my final year project.. 😞
0 Kudos
DanielWalton
Frequent Contributor
Nothing I said is really specific to ESRI classes or concepts. This is basic .NET stuff. Download the codeplex source code and put it into a project, then step through the calls to see how it works. I wouldn't want to rob you of the learning process by just pasting some code here for you to copy.
0 Kudos
YouKnow_It
Emerging Contributor
Nothing I said is really specific to ESRI classes or concepts. This is basic .NET stuff. Download the codeplex source code and put it into a project, then step through the calls to see how it works. I wouldn't want to rob you of the learning process by just pasting some code here for you to copy.


okay, i just got it, thanks a lot 🙂
0 Kudos
ElizabethMiller
Regular Contributor
I am trying to add a kml from file also (using vb code behind). Can someone provide more help about how to do this? I have unzipped the codeplex files into my ESRI SDKs folder but I don't see how to add anything helpful to my Silverlight project.

I am not looking to enable an end user to add a kml file to the map. I am looking to serve out the kml in my map, but not from a service source.

Thanks for any help from anyone who has done this!
0 Kudos
dotMorten_esri
Esri Notable 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);
}
0 Kudos
ElizabethMiller
Regular 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.
0 Kudos