Click here to Skip to main content
15,892,965 members
Articles / Hosted Services / Azure

GPS Runner Maps: My First Windows Azure Application

Rate me:
Please Sign up or sign in to vote.
4.90/5 (12 votes)
20 Dec 2009CPOL3 min read 55.3K   3.1K   63  
It is "cloud" Web application to display GPS tracks on Google or Bing maps
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Live.ServerControls.VE;
using System.Configuration;
using System.Drawing;

namespace PS.GpsRunnerMaps
{
  public class GpsRunnerDisplay : IMapDisplay
  {
    private GoogleMapDisplay _googleMapDisplay;
    private VEMapDisplay _veMapDisplay;
    private string _centerPointIcon;

    public GpsRunnerDisplay(GoogleMapForASPNet go, Map ve)
    {
      _googleMapDisplay = new GoogleMapDisplay(this);
      _veMapDisplay = new VEMapDisplay(this);
      _googleMapDisplay.GoogleMapUC = go;
      _veMapDisplay.VeMap = ve;
    }

    public string CenterPointIcon
    {
      get { return _centerPointIcon; }
      set { _centerPointIcon = value; }
    }

    IMapDisplay CurrMapDisplay
    {
      get
      {
        if (this.MapType == 0)
        { return _googleMapDisplay; }
        else
        { return _veMapDisplay; }
      }
    }

    public GoogleMapDisplay Google
    {
      get { return _googleMapDisplay; }
    }

    public VEMapDisplay Ve
    {
      get { return _veMapDisplay; }
    }

    public string TrackId
    {
      get { return HttpContext.Current.Session["trackId"] as string; }
      set { HttpContext.Current.Session["trackId"] = value; }
    }

    public int MapType
    {
      get { return (int)HttpContext.Current.Session["mapType"]; }
      set { HttpContext.Current.Session["mapType"] = value; }
    }

    public void Clear()
    {
      CurrMapDisplay.Clear();
      HttpContext.Current.Session["Times"] = null;
      HttpContext.Current.Session["Coords"] = null;
      HttpContext.Current.Session["MeanLat"] = null;
      HttpContext.Current.Session["MeanLon"] = null;
      this.Point1 = 0; this.Point2 = 0;
    }

    public void FillMap(bool showPoints, bool recenter)
    {
      CurrMapDisplay.FillMap(showPoints, recenter);
    }

    public int TracksZoom
    { get { return int.Parse(ConfigurationManager.AppSettings["TracksZoom"]); } }

    public int TrackDetailsZoom
    { get { return int.Parse(ConfigurationManager.AppSettings["TrackDetailsZoom"]); } }

    public string IconTrack
    { get { return this.MapType == 0 ? "icons/p01.png" : "icons/p01Lrg.png"; } }

    public bool IsPointOfTrack(GooglePoint p) { return p.IconImage == IconTrack; }

    public List<CoordinateRow> GetTrackCoordinatesByTime()
    {
      if (HttpContext.Current.Session["Coords"] == null)
      {
        List<CoordinateRow> coordList = GpsRunnerDataStore.GetTrackCoordinates(this.TrackId, true).ToList();
        coordList = coordList.OrderBy(c => c.Time).ToList();
        HttpContext.Current.Session["Coords"] = coordList;
        this.Point1 = 0;
        this.Point2 = coordList.Count - 1;
        this.CurrPointNo = 0;
        return coordList;
      }
      else
        return HttpContext.Current.Session["Coords"] as List<CoordinateRow>;
    }

    public IList<CoordinateRow> TrackCoordinates
    { get { return HttpContext.Current.Session["Coords"] as IList<CoordinateRow>; } }

    public List<DateTime> GetTimes()
    {
      List<CoordinateRow> coordList = GetTrackCoordinatesByTime();
      if (HttpContext.Current.Session["Times"] == null)
      {
        List<DateTime> res = new List<DateTime>(coordList.Count);
        DateTime dtStart = coordList[0].Time;
        for (int i = 0; i < coordList.Count; i++)
        {
          CoordinateRow c = coordList[i];
          TimeSpan sp = c.Time.Subtract(dtStart);
          res.Add(new DateTime(1000, 1, 1, sp.Hours, sp.Minutes, sp.Seconds));
        }
        HttpContext.Current.Session["Times"] = res;
        return res;
      }
      else
      {
        return HttpContext.Current.Session["Times"] as List<DateTime>;
      }
    }

    public double MeanLat
    {
      get
      {
        if (HttpContext.Current.Session["MeanLat"] == null)
        {
          TrackRow trRow = GpsRunnerDataStore.GetTrack(this.TrackId);
          HttpContext.Current.Session["MeanLat"] = (trRow.Minlat + trRow.Maxlat) / 2;
          HttpContext.Current.Session["MeanLon"] = (trRow.Minlon + trRow.Maxlon) / 2;
        }
        return (double)HttpContext.Current.Session["MeanLat"];
      }
    }

    public double MeanLon
    {
      get
      {
        return (double)HttpContext.Current.Session["MeanLon"];
      }
    }

    public string GetInfo(CoordinateRow c1)
    {
      string res = string.Format(Resources.Res.infoHtml_FS.Replace("\\n", "\r\n"),
        c1.Time.ToString("HH:mm:ss"), c1.Velocity, c1.Altitude);
      return res;
    }

    public int CurrPointNo
    {
      get { return (int)HttpContext.Current.Session["CurrPointNo"]; }
      set { HttpContext.Current.Session["CurrPointNo"] = value; }
    }

    public int Point1
    {
      get { return (int)HttpContext.Current.Session["Point1"]; }
      set { HttpContext.Current.Session["Point1"] = value; }
    }

    public int Point2
    {
      get { return (int)HttpContext.Current.Session["Point2"]; }
      set { HttpContext.Current.Session["Point2"] = value; }
    }

    public bool Recenter
    {
      get { return this.Google.GoogleMap.RecenterMap; }
      set { this.Google.GoogleMap.RecenterMap = value; }
    }

    public bool Rezoom
    {
      get { return this.Google.GoogleMap.RezoomMap; }
      set { this.Google.GoogleMap.RezoomMap = value; }
    }

    public LatLongWithAltitude CenterPoint
    {
      get
      {
        return this.CurrMapDisplay.CenterPoint;
      }
      set
      {
        this.CurrMapDisplay.CenterPoint = value;
      }
    }

    public int ZoomLevel
    {
      get { return this.CurrMapDisplay.ZoomLevel; }
      set { this.CurrMapDisplay.ZoomLevel = value; }
    }

    public class GoogleMapDisplay : IMapDisplay
    {
      public GoogleMapDisplay(GpsRunnerDisplay parent)
      { this._parent = parent; }

      private GoogleMapForASPNet _googleMapUC;
      private GpsRunnerDisplay _parent;

      public GoogleMapForASPNet GoogleMapUC
      {
        get { return _googleMapUC; }
        set { _googleMapUC = value; }
      }

      public GoogleObject GoogleMap
      {
        get { return _googleMapUC.GoogleMapObject; }
      }

      public LatLongWithAltitude CenterPoint
      {
        get
        {
          return new LatLongWithAltitude(this.GoogleMapUC.GoogleMapObject.CenterPoint.Latitude,
            this.GoogleMapUC.GoogleMapObject.CenterPoint.Longitude, 0);
        }
        set
        {
          this.GoogleMapUC.GoogleMapObject.CenterPoint = new GooglePoint(string.Format("{0}-{1}", value.Latitude, value.Longitude),
            value.Latitude, value.Longitude);
          if (_parent.CenterPointIcon != null)
          { this.GoogleMapUC.GoogleMapObject.CenterPoint.IconImage = _parent.CenterPointIcon; }
        }
      }

      public int ZoomLevel
      {
        get { return this.GoogleMapUC.GoogleMapObject.ZoomLevel; }
        set { this.GoogleMapUC.GoogleMapObject.ZoomLevel = value; }
      }

      public void FillMap(bool showPoints, bool recenter)
      {
        GooglePolyline gp = new GooglePolyline();
        gp.Width = 2;
        gp.ColorCode = "Red";
        List<CoordinateRow> coordList = _parent.GetTrackCoordinatesByTime();
        List<DateTime> times = _parent.GetTimes();
        GooglePoint p1, p2, p;
        CoordinateRow c1 = coordList[0];
        p1 = new GooglePoint(c1.RowKey, c1.Latitude, c1.Longitude, "icons/1.png", _parent.GetInfo(c1), times[0].ToString("hh:mm:ss"), false);
        this.GoogleMap.Points.Add(p1);
        CoordinateRow c2 = coordList[coordList.Count - 1];
        p2 = new GooglePoint(c2.RowKey, c2.Latitude, c2.Longitude, "icons/2.png", _parent.GetInfo(c2), times[times.Count - 1].ToString("hh:mm:ss"), false);
        this.GoogleMap.Points.Add(p2);
        // create track polygon
        int iP = 0;
        foreach (CoordinateRow cRow in coordList)
        {
          GooglePoint trP = new GooglePoint(cRow.RowKey, cRow.Latitude, cRow.Longitude);
          gp.Points.Add(trP);
          if (showPoints)
          {
            p = new GooglePoint(cRow.RowKey, cRow.Latitude, cRow.Longitude, this._parent.IconTrack, string.Empty,
             _parent.GetInfo(cRow), false);
            this.GoogleMap.Points.Add(p);
          }
          iP++;
        }
        this.GoogleMap.Polylines.Add(gp);
        // get way points (points with comments)
        List<CoordinateRow> pointsList = GpsRunnerDataStore.GetTrackCoordinates(_parent.TrackId, false).ToList();
        foreach (CoordinateRow cRow in pointsList)
        {
          GooglePoint wayP = new GooglePoint(cRow.RowKey, cRow.Latitude, cRow.Longitude);
          wayP.ToolTip = cRow.Comment;
          wayP.IconImage = "icons/pushpin-yellow.png";
          wayP.InfoHTML = _parent.GetInfo(cRow) + "\n" + cRow.Comment;
          this.GoogleMap.Points.Add(new GooglePoint(cRow.RowKey, cRow.Latitude, cRow.Longitude));
        }
        // center map
        this.GoogleMap.ZoomLevel = showPoints ? _parent.TrackDetailsZoom : _parent.TracksZoom; ;
        this.GoogleMap.CenterPoint = new GooglePoint("CenterPoint", _parent.MeanLat, _parent.MeanLon);
        this.GoogleMap.RecenterMap = recenter;
      }

      public void Clear()
      {
        this.GoogleMap.Polylines.Clear();
        this.GoogleMap.Points.Clear();
      }
    } // end inner class

    public class VEMapDisplay : IMapDisplay
    {
      public VEMapDisplay(GpsRunnerDisplay parent)
      { this._parent = parent; }

      private Map _veMap;
      private GpsRunnerDisplay _parent;
      private Shape _centerPin;

      public Map VeMap
      {
        get { return _veMap; }
        set { _veMap = value; }
      }

      public LatLongWithAltitude CenterPoint
      {
        get
        {
          return new LatLongWithAltitude(_veMap.Center.Latitude, _veMap.Center.Longitude, 0);
        }
        set
        {
          _veMap.Center = new LatLong(value.Latitude, value.Longitude);
        }
      }

      public int ZoomLevel
      {
        get { return this._veMap.ZoomLevel; }
        set { this._veMap.ZoomLevel = value; }
      }

      public void FillMap(bool showPoints, bool recenter)
      {
        List<CoordinateRow> coordList = _parent.GetTrackCoordinatesByTime();
        List<DateTime> times = _parent.GetTimes();
        List<LatLongWithAltitude> polygonList = coordList.Select(c =>
          new LatLongWithAltitude(c.Latitude, c.Longitude)).ToList();
        Shape shpPolygon = new Shape(ShapeType.Polyline, polygonList);
        shpPolygon.LineWidth = 2;
        shpPolygon.LineColor = new Microsoft.Live.ServerControls.VE.Color(255, 0, 0, 1.0);
        ShapeLayer layer = _veMap.GetShapeLayerByIndex(1);
        layer.AddShape(shpPolygon);
        Shape p1, p2, p;
        CoordinateRow c1 = coordList[0];
        p1 = new Shape(ShapeType.Pushpin, new LatLongWithAltitude(c1.Latitude, c1.Longitude));
        p1.CustomIcon = "icons/1.png"; p1.Description = _parent.GetInfo(c1);
        layer.AddShape(p1);
        CoordinateRow c2 = coordList[coordList.Count - 1];
        p2 = new Shape(ShapeType.Pushpin, new LatLongWithAltitude(c2.Latitude, c2.Longitude));
        p2.CustomIcon = "icons/2.png"; p2.Description = _parent.GetInfo(c2);
        layer.AddShape(p2);
        // create track polygon
        if (showPoints)
        {
          foreach (CoordinateRow cRow in coordList)
          {
            LatLongWithAltitude xy = new LatLongWithAltitude(cRow.Latitude, cRow.Longitude);
            p = new Shape(ShapeType.Pushpin, xy);
            p.CustomIcon = this._parent.IconTrack; p.Description = _parent.GetInfo(cRow);
            p.IconAnchor = xy;
            layer.AddShape(p);
          }
        }
        // get way points (points with comments)
        List<CoordinateRow> pointsList = GpsRunnerDataStore.GetTrackCoordinates(_parent.TrackId, false).ToList();
        foreach (CoordinateRow cRow in pointsList)
        {
          p = new Shape(ShapeType.Pushpin, new LatLongWithAltitude(cRow.Latitude, cRow.Longitude));
          p.CustomIcon = "icons/pushpin-yellow.png"; p.Description = _parent.GetInfo(cRow);
          layer.AddShape(p);
        }
        // center map
        this._veMap.ZoomLevel = showPoints ? _parent.TrackDetailsZoom : _parent.TracksZoom;
        this._veMap.Center = new LatLong(c1.Latitude, c2.Longitude);
      }

      public void Clear()
      {
        _veMap.Clear();
        ShapeLayer layer;
        layer = new ShapeLayer();
        _veMap.AddShapeLayer(layer); // layer 0: points
        layer = new ShapeLayer();
        _veMap.AddShapeLayer(layer); // layer 1: center point
        int numLayers = _veMap.GetShapeLayerCount();
      }
    } // end inner class
  } // end class

  interface IMapDisplay
  {
    void FillMap(bool showPoints, bool recenter);
    LatLongWithAltitude CenterPoint { get; set; }
    int ZoomLevel { get; set; }
    void Clear();
  }


}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer Forthnet
Greece Greece
Software developer and Microsoft Trainer, Athens, Greece (MCT, MCSD.net, MCSE 2003, MCDBA 2000,MCTS, MCITP, MCIPD).

Comments and Discussions