Skip to content

Instantly share code, notes, and snippets.

@bradygaster-zz
Created October 28, 2012 19:25

Revisions

  1. @bradygaster bradygaster revised this gist Oct 28, 2012. 1 changed file with 53 additions and 0 deletions.
    53 changes: 53 additions & 0 deletions index.cshtml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    <div id="body">
    <section class="featured">
    <div class="content-wrapper">
    <hgroup class="title">
    <h2>Where are all the good chicken wings in the Puget Sound Area?</h2>
    </hgroup>
    </div>
    </section>
    <section class="content-wrapper main-content clear-fix">
    <table>
    <tbody data-bind="foreach: locations">
    <tr>
    <td data-bind="text: name"></td>
    <td data-bind="text: address"></td>
    <td data-bind="text: distance"></td>
    </tr>
    </tbody>
    </table>
    </section>
    </div>

    @section scripts{
    <script src="~/Scripts/knockout-2.1.0.js"></script>
    <script type="text/javascript">
    function pageViewModel() {
    var self = this;
    self.locations = ko.observableArray([]);
    self.handle_geolocation_query = function (position) {
    var url = '/api/location?latitude=' + position.coords.latitude + '&longitude=' + position.coords.longitude;
    $.get(url, function (data) {
    $(data).each(function (i, item) {
    var newLocation = new locationViewModel(item.Name, item.Address, item.Distance);
    self.locations.push(newLocation);
    });
    });
    };
    self.initialize = function () {
    navigator.geolocation.getCurrentPosition(self.handle_geolocation_query);
    }
    };
    function locationViewModel(nm, addr, dist) {
    this.name = ko.observable(nm);
    this.address = ko.observable(addr);
    this.distance = ko.observable(parseFloat(dist).toFixed(2) + ' miles');
    };
    var model = new pageViewModel();
    ko.applyBindings(model);
    model.initialize();
    </script>
    }
  2. @bradygaster bradygaster created this gist Oct 28, 2012.
    21 changes: 21 additions & 0 deletions Configuration.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    internal sealed class Configuration : DbMigrationsConfiguration<SpatialDemo.Models.SpatialDemoContext>
    {
    public Configuration()
    {
    AutomaticMigrationsEnabled = true;
    }

    protected override void Seed(SpatialDemo.Models.SpatialDemoContext context)
    {
    context.Locations.AddOrUpdate((x) => x.Name,
    new LocationEntity { Name = "The Wing Dome", Address = "7818 Greenwood Avenue North, Seattle WA", Coordinates = DbGeography.FromText("POINT(-122.35499 47.68609)") },
    new LocationEntity { Name = "Kona Kitchen", Address = "8501 5th Ave NE, Seattle WA", Coordinates = DbGeography.FromText("POINT(-122.32338 47.69068)") },
    new LocationEntity { Name = "Wing Central on the Ave", Address = "4524 University Way Northeast, Seattle WA", Coordinates = DbGeography.FromText("POINT(-122.31302 47.66182)") },
    new LocationEntity { Name = "J. Michael's Pub and Eatery", Address = "15770 Redmond Way, Redmond WA", Coordinates = DbGeography.FromText("POINT(122.12975 47.67434)") },
    new LocationEntity { Name = "Charlies Flame Broiled Burgers", Address = "1006 Lake Street South, Kirkland WA", Coordinates = DbGeography.FromText("POINT(-122.20642 47.6674)") },
    new LocationEntity { Name = "The Wing Dome", Address = "232 Central Way, Kirkland WA", Coordinates = DbGeography.FromText("POINT(-122.19963 47.67861)") },
    new LocationEntity { Name = "Jillian's Billiards Club", Address = "731 Westlake Avenue North, Seattle WA", Coordinates = DbGeography.FromText("POINT(-122.33939 47.62633)") },
    new LocationEntity { Name = "Malarkey's Sports Grill", Address = "1025 Northwest Gilman Boulevard, Issaquah WA", Coordinates = DbGeography.FromText("POINT(-122.05334 47.54516)") }
    );
    }
    }
    8 changes: 8 additions & 0 deletions Location.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    public class Location
    {
    public string Name { get; set; }
    public string Address { get; set; }
    public double Latitude { get; set; }
    public double Longitude { get; set; }
    public double Distance { get; set; }
    }
    33 changes: 33 additions & 0 deletions LocationController.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    public class LocationController : ApiController
    {
    SpatialDemoContext db;

    public LocationController()
    {
    db = new SpatialDemoContext();
    }

    protected override void Dispose(bool disposing)
    {
    db.Dispose();
    base.Dispose(disposing);
    }

    public IEnumerable<Location> Get(string longitude, string latitude)
    {
    var qry = string.Format("POINT({0} {1})", longitude, latitude);

    var results = (from s in db.Locations
    orderby s.Coordinates.Distance(DbGeography.FromText(qry))
    select new Location
    {
    Name = s.Name,
    Address = s.Address,
    Longitude = s.Coordinates.Longitude.Value,
    Latitude = s.Coordinates.Latitude.Value,
    Distance = (s.Coordinates.Distance(DbGeography.FromText(qry)).Value * 0.000621371)
    }).Take(5).ToList<Location>();

    return results;
    }
    }
    7 changes: 7 additions & 0 deletions LocationEntity.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    public class LocationEntity
    {
    public int Id { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public DbGeography Coordinates { get; set; }
    }
    8 changes: 8 additions & 0 deletions SpatialDemoContext.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    public class SpatialDemoContext : System.Data.Entity.DbContext
    {
    public SpatialDemoContext() : base("name=SpatialDemoConnectionString")
    {
    }

    public System.Data.Entity.DbSet<LocationEntity> Locations { get; set; }
    }