Jump to content

How To Create A Google Maps IEnumerable Object In C#.NET With Linq To XML


Recommended Posts

Posted

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Xml.Linq;

namespace LiquidFrameworks.GoogleMaps

{

public class Route

{

public string To { get; set; }

public string From { get; set; }

 

}

public class GoogleMaps : IEnumerable<string>

{

Route Agenda = new Route();

public GoogleMaps(string to, string from)

{

Agenda.To = to;

Agenda.From = from;

}

public List<string> GetDirections(Route Agenda)

{

string mapsURL = @"http://maps.google.com/?q=From " + Agenda.To + " To " + Agenda.From + "&output=kml";

XDocument mapsDocument = XDocument.Load(mapsURL);

XNamespace myNS = XNamespace.Get(@"http://earth.google.com/kml/2.0");

 

var result = from e in mapsDocument.Element(myNS + "kml").Element(myNS + "Document").Elements(myNS + "Placemark").Elements(myNS + "name")

 

select (string)e;

 

return result.ToList();

 

}

 

#region IEnumerable<string> Members

 

public IEnumerator<string> GetEnumerator()

{

foreach (string i in GetDirections(Agenda))

{

yield return i;

 

}

}

 

#endregion

 

#region IEnumerable Members

 

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()

{

return GetEnumerator();

}

 

#endregion

}

}


Merged post follows:

Consecutive posts merged

I created this object so that other people can reuse google maps without calling special API.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.