Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Discover the top 5 tips for understanding .NET Interop
Search :       Advanced Search »
Home » ASP.NET & Web Forms » Translate Driving directions from Google Maps in XML format

Translate Driving directions from Google Maps in XML format

In this article, I demonstrate on how you can integrate the Keyhole Markup Language document in Asp.net using C#.

Page Views : 5548
Downloads : 390
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
GoogleMapDirection.zip
 
 
Team Foundation Server Hosting
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Syed Ahmed
I am an Application developer for more than 5 years now. I am currently working for the Title Company. My experties are mainly in Web application but i am also getting myself in Desktop applications.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
No attachment to this post.. by Westley On April 26, 2010
The attachment to this post doesn't work.

Any chance you could forward me the zip file or make it available on this page? the file name is GoogleMapDirection.zip.

wes@mouse.co.uk

Cheers,

Wes
Reply | Email | Modify 
ur link is broken can u correct i plzzz by Ashok On September 25, 2010
ur link is broken
can u plz update it?
plz update the link  so that i can download it
Reply | Email | Modify 
Nevron Chart
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.