Skip to content

Instantly share code, notes, and snippets.

@bradygaster-zz
Created October 28, 2012 19:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bradygaster-zz/3969576 to your computer and use it in GitHub Desktop.
Save bradygaster-zz/3969576 to your computer and use it in GitHub Desktop.
Code associated with a location API used to demonstrate EF Spatial and Windows Azure Web Sites
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)") }
);
}
}
<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>
}
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; }
}
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;
}
}
public class LocationEntity
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public DbGeography Coordinates { get; set; }
}
public class SpatialDemoContext : System.Data.Entity.DbContext
{
public SpatialDemoContext() : base("name=SpatialDemoConnectionString")
{
}
public System.Data.Entity.DbSet<LocationEntity> Locations { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment