Sep 29 2008

C# GPS Tracking en Windows Mobile (3 parte)

Category: ProgramacionIndigo @ 4:49 PM

Hola a todos, aquí esta la tercera entrega de mis artículos sobre GPS Tracking. En las entregas anteriores vimos con preparar el entorno para poder desarrollar aplicaciones para Compact Mobile e instalar el ejemplo de Microsoft sobre el que nos vamos a apoyar para el desarrollo de nuestra aplicación.

Si estudiamos un poco el GPS Sample, veremos que hay un evento LocationChanged en la clase gps. En el manejados de este evento es donde pondremos nuestro código para grabar nuestra posición.

[sourcecode language='csharp']private void gps_LocationChanged(object sender, LocationChangedEventArgs args)
{
position = args.Position;

Invoke(displayLocationHandler);
Invoke(saveLocationHandler);
}[/sourcecode]

La declaración de saveLocationHandler la haremos en el Form_Load de la siguiente menera

[sourcecode language='csharp']private void Form1_Load(object sender, EventArgs e)
{
// …
saveLocationHandler = new EventHandler(SavePosition);
// …
}[/sourcecode]

y nuestro método para grabar la posición es el siguiente

[sourcecode language='csharp']private void SavePosition(object sender, System.EventArgs args)
{
if (gps.Opened)
{
if (position != null)
{
if ((position.LatitudeValid) ‘and’ (position.LongitudeValid) ‘and’ (position.SeaLevelAltitudeValid))
{
SavePositionXml(position.LatitudeInDegreesMinutesSeconds,
}
}
}
}

private void SavePositionXml(DegreesMinutesSeconds latitude, DegreesMinutesSeconds longitude, float altitude)
{
string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
string filename = Path.Combine(path, DateTime.Today.ToString(”yyyyMMdd”) + “.xml”);

XmlDocument xmlDocument = new XmlDocument();
XmlNode rootNode = null;
if (File.Exists(filename))
{
// Cargamos el fichero xml
xmlDocument.Load(filename);
rootNode = xmlDocument.DocumentElement;
}
else
{
// Creamos el fichero
XmlNode header = xmlDocument.CreateXmlDeclaration(”1.0″, “UTF-8″, null);
xmlDocument.AppendChild(header);

rootNode = xmlDocument.CreateElement(”Positions”);
xmlDocument.AppendChild(rootNode);
}

XmlElement nodePosition = xmlDocument.CreateElement(”Position”);

nodePosition.AppendChild(CreateXmlElement(xmlDocument, “DateTime”, DateTime.Now.ToString(”dd/MM/yyyy HH:mm:ss”)));

nodePosition.AppendChild(CreateXmlElement(xmlDocument, “LatitudeDegrees”, (latitude.Degrees * (latitude.IsPositive ? 1 : -1)).ToString()));
nodePosition.AppendChild(CreateXmlElement(xmlDocument, “LatitudeMinutes”, latitude.Minutes.ToString()));
nodePosition.AppendChild(CreateXmlElement(xmlDocument, “LatitudeSeconds”, latitude.Seconds.ToString()));

nodePosition.AppendChild(CreateXmlElement(xmlDocument, “LongitudeDegrees”, (longitude.Degrees * (longitude.IsPositive ? 1 : -1)).ToString()));
nodePosition.AppendChild(CreateXmlElement(xmlDocument, “LongitudeMinutes”, longitude.Minutes.ToString()));
nodePosition.AppendChild(CreateXmlElement(xmlDocument, “LongitudeSeconds”, longitude.Seconds.ToString()));

nodePosition.AppendChild(CreateXmlElement(xmlDocument, “Altitude”, altitude.ToString()));

rootNode.AppendChild(nodePosition);

xmlDocument.Save(filename);
}[/sourcecode]

Y por último, el método auxiliar CreateXmlElement es este

[sourcecode language='csharp']private XmlElement CreateXmlElement(XmlDocument xmlDocument, string name, string value)
{
XmlElement node = xmlDocument.CreateElement(name);
XmlText text = xmlDocument.CreateTextNode(value);
node.AppendChild(text);

return node;
}[/sourcecode]

Con este método obtendremos un archivo xml como éste

[sourcecode language='xml'] 22/09/2008 16:04:22
xx
xx
xx,yy
x
xx
xx,yy
xxx,y
22/09/2008 16:04:23
xx
xx
xx,yy
xx
xx
xx,yy
xxx,y
[/sourcecode]

Con esto ya tenemos nuestro primer fichero xml con las posiciones guardas. En el próximo artículo veremos como generar otro tipo de fichero xml. En este caso, un fichero xml que cumpla el estandar gpx.

¡Compartelo!
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Add to favorites
  • BarraPunto
  • Bitacoras.com
  • LinkedIn
  • Meneame
  • RSS
  • Technorati
  • Twitter

Tags: , , ,

3 Responses to “C# GPS Tracking en Windows Mobile (3 parte)”

  1. www.programame.net says:

    C# GPS Tracking en Windows Mobile (3 parte)…

    Tercera entre sobre gps tracking en windows mobile. En este entrega veremos como generar el xml con las posiciones que obtenemos el gps….

  2. www.webeame.net says:

    C# GPS Tracking en Windows Mobile (3 parte)…

    Tercera entre sobre gps tracking en windows mobile. En este entrega veremos como generar el xml con las posiciones que obtenemos el gps….