Click here to Skip to main content
15,897,032 members
Articles / Mobile Apps / Windows Phone 7

Bike In City with Windows Phone 7

Rate me:
Please Sign up or sign in to vote.
4.97/5 (62 votes)
5 Jan 2011CPOL33 min read 111.3K   3.1K   101  
Learn how to build a small mobile application to visualize data from the city bike sharing system. Get the nearest stations, find the number of free bikes, and compute directions to other stations.
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls.Maps.Platform;

namespace BikeInCity.Utils
{
  public class GeoMath
  {
    /// <summary>
    /// Converts degrees to radians. Used to convert the latitude and longitude coordinates.
    /// </summary>
    /// <param name="angle"></param>
    /// <returns></returns>
    public static double ToRad(double angle)
    {
      var r = angle * Math.PI / 180;
      return r;
    }

    /// <summary>
    /// Returns distsance in meters between two points of earth using the Spherical Law of Cosines
    /// </summary>
    /// <param name="start"></param>
    /// <param name="end"></param>
    /// <returns></returns>
    public static int ComputeDistance(Location start, Location end)
    {
      var R = 6371;
      double lat1 = ToRad(start.Latitude);
      double lat2 = ToRad(end.Latitude);

      double lng1 = ToRad(start.Longitude);
      double lng2 = ToRad(end.Longitude);

      double dlng = lng2 - lng1;
      double dlat = lat2 - lat1;

      var a = Math.Pow(Math.Sin(dlat / 2),2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Pow(Math.Sin(dlng/2),2);
      var c = 2*Math.Asin(Math.Min(1,Math.Sqrt(a)));

      var d = R * c;
      

      /*
      //spherical law of cosines
      
      var d = Math.Acos(Math.Sin(lat1) * Math.Sin(lat2) +
                        Math.Cos(lat1) * Math.Cos(lat2) *
                        Math.Cos(lng1 - lng2)) * R;
      */
      return (int)(d * 1000);
    }
  }
}

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
Software Developer (Junior) OCTO Technology
Czech Republic Czech Republic
Writing software at ITG RFQ-hub.
LinkedIn
Blog
GitHub
Articles at OCTO blog

Comments and Discussions