Skip to content

Instantly share code, notes, and snippets.

@HeathHopkins
Created February 13, 2013 14:52
Show Gist options
  • Save HeathHopkins/4945101 to your computer and use it in GitHub Desktop.
Save HeathHopkins/4945101 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MonoTouch.CoreLocation;
using MonoTouch.MapKit;
using System.Xml.Linq;
namespace Auburn.Models
{
public class AUBuilding : MKPolygon
{
public static AUBuilding FromData(Building b)
{
// this cast doesn't work. it was worth a shot. :)
var points = AUBuilding.BuildingPoints(b.kml);
var poly = AUBuilding.FromPoints(points) as AUBuilding;
poly.building = b;
return poly;
}
/*
* Either this should work, or I should be able to set some unique identifier on a plain MKPolygon object.
*
public override MKMapPoint[] Points {
get {
return new MKMapPoint[10];
}
}
*/
public Building building { get; set; }
public AUBuilding (Building building)
{
this.building = building;
}
public override string Title { get { return building.name; } }
public static MKMapPoint[] BuildingPoints(string kml)
{
var list = new List<MKMapPoint>();
XElement xe = XElement.Parse(kml);
IEnumerable<XElement> coords = xe.Elements().Elements().Elements();
foreach (var coordXML in coords)
{
var coordString = coordXML.Value;
foreach (string coord in coordString.Split(" ".ToCharArray()))
{
var coordEntities = coord.Split(",".ToCharArray());
list.Add(MKMapPoint.FromCoordinate(new CLLocationCoordinate2D(double.Parse(coordEntities[1]), double.Parse(coordEntities[0]))));
}
}
return list.ToArray();
}
}
}
var kml = @"<Polygon><outerBoundaryIs><LinearRing><coordinates>-85.4867142869,32.6004323223,0 -85.4867434496,32.6004763692,0 -85.4867107582,32.6004907865,0 -85.4867714882,32.6005893807,0 -85.4864231987,32.6007430277,0 -85.4864764096,32.6008294527,0 -85.4864111298,32.6008582695,0 -85.48635866,32.6007730838,0 -85.4862478436,32.600821977,0 -85.4860059567,32.6004291414,0 -85.4865266665,32.6001993966,0 -85.4866795242,32.6004476574,0 -85.4867142869,32.6004323223,0</coordinates></LinearRing></outerBoundaryIs></Polygon>";
var bdata = new Building()
{
name = "Parker Hall test",
kml = kml,
id = 7
};
// this cast doesn't work.
//var building = (AUBuilding)MKPolygon.FromPoints(AUBuilding.BuildingPoints(bdata.kml));
// this doesn't work for the same reason. i can't cast from MKPolygon to AUPolygon
//var building = AUBuilding.FromData(bdata);
// this does work, but I can't figure out how to set a unique ID or add a custom field
var building = MKPolygon.FromPoints(AUBuilding.BuildingPoints(bdata.kml));
mapView.AddOverlay(building);
mapView.GetViewForOverlay = (m, o) => {
if (buildingView == null)
{
// How do I get a unique ID for my current MKPolygon?
buildingView = new MKPolygonView(o as MKPolygon);
buildingView.FillColor = Constants.PrimaryOrange;
buildingView.Alpha = 0.5f;
buildingView.Tag = 7;
}
return buildingView;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment