Monday, January 5, 2009

Read Google Maps KML using LINQ in ASP .NET to Show Driving Directions

We can use LINQ to query the Google Maps KML File. The magic piece is the Google Maps URL to retrieve KML.

http://maps.google.ca/?q=From 25 King St. West, Toronto, ON to Niagara Falls, Ontario&output=kml

Dim fromAddress = "25 King St. West, Toronto, ON"
Dim toAddress = "Niagara Falls, ON"
Dim urlString As String = "http://maps.google.ca/?q=From " & fromAddress & " to " & toAddress & "&output=kml&view=text"
Dim xmlDataSource2 As New XmlDataSource()
xmlDataSource2.DataFile
= urlString

Dim xdoc As XDocument = XDocument.Parse(xmlDataSource2.GetXmlDocument().InnerXml)
Dim ns As XNamespace = XNamespace.Get("http://earth.google.com/kml/2.0")
Dim ele = xdoc.Element(ns + "kml").Element(ns + "Document").Elements(ns + "Placemark").Elements(ns + "name")


GridView1.DataSource = ele.Take(ele.Count - 1)
GridView1.DataBind()

0 comments:

Video Bar

Loading...