Translate Driving directions from Google Maps in XML format


1.gif
Figure 1

Introduction

In this example, It has been demonstrated on how you can integrate the Keyhole Markup Language  KML  document in Asp.net using C Sharp. The Google maps return this document in result of Http request sent by the user which contains the data for direction and distance. You will see the implementation and translation when you walk in to the code. I have use Linq allows XML data to be queried by using the standard query operators as well as tree-specific operators that provide XPath-like navigation through descendants, ancestors, and siblings. It simplifies working with XML data without having to resort to using additional language syntax like XPath or XQuery. You can use LINQ to XML to perform LINQ queries over XML that you retrieve from the file system, from a remote web service, or from in-memory XML content. 

Using the code

The first thing we will do is add the namespaces we will be using. Our code-behind will look something like this:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Xml;

2.gif
  
Figure 2

Line 1:   XNamespace obj_XNameSpace = XNamespace.Get("http://earth.google.com/kml/2.0");
Line 2:   string Des_Source_URL = "http://maps.google.com/?q=From " + txtDestination.Text + " to " + txtSource.Text + "&output=kml&view=text";

Before going into detail there are some points to keep in mind.

1: XNamespace ("http://earth.google.com/kml/2.0") this will works as a namespace for which we will use to parse the KML for the specified Uniform Resource Identifier 
2: Browser Qurey www.maps.google.com/?q=from "from_address" to "to_address" "&output=kml&view=text"; (where "from" and "to" are the keywords).
3: The "output" parameter retrieves the result in the given format which will be KML in this case.

3.gif
 
Figure 3

Hint: If you type the Query above in step 2 in the brower url window after a filedownload  dialog will apprear and asked you to save the file.
 
The code is really simple and straight forward. If you are familiar with the Linq then i bet you can do more inquisitive things. Right now it's just a basic example where i have created a user control which mainly consists of two Text boxes, Button and a Grid view. I am using the ScriptManager for AJAX functionality in order to improve the performance and less postback

In the following  Linq query i am loading all the decendants elements of "Placemark".in IEnumerable<Xelement> object as shown in Figure 2.And I need to get just Placemark(name,description) WITHOUT StyleMap, Point, LookAt (PlaceMark) I'm using the following code, but due to the fact that all nodes are "Node" I get all levels of Descendants. I would like to limit it to only one level.
IEnumerable<XElement> nodes = from n in Des_Source_document.Descendants(obj_XNameSpace + "Placemark")    
select n;
   
The Xdocument.Descendants() Returns a collection of the descendant elements for this document or element, in document order.

var Select_Name_Doc = from n in nodes

    where ((from x in n.Descendants(obj_XNameSpace + "name") select x).Count() > 0)

                                                  && ((from y in n.Descendants(obj_XNameSpace + "description")

                                                       select y).Count() > 0)

                                                  select n;


Inside this Linq query I am getting the value of required nodes value. Which are "name" and "description". Please make sure that XML is case sensitive so the Xname should be precisely in the same format as shown in Figure 2.

foreach (var node in Select_Name_Doc)
{
    DataRow dr = obj_datatble.NewRow();
    dr["Value"] = node.Descendants(obj_XNameSpace + "name").First().Value;
    dr["Distance"] = node.Descendants(obj_XNameSpace + "description").First().Value;
    obj_datatble.Rows.Add(dr);
}
GridView1.DataSource = obj_datatble;
GridView1.DataBind();

After getting all the vlaues in Select_Name_Doc I declare a cusom DataTable with two rows "value" and "distance" and then storing vlaue of each node in these rows and then add in to a DataTable inside foreach loop. After it's done adding the rows, I assigned the DataTable to GridView as  DataSource and then bind the datagrid.

Up Next
    Ebook Download
    View all
    Learn
    View all