Skip to content

Instantly share code, notes, and snippets.

@adrianstevens
Created April 10, 2015 23:49
Show Gist options
  • Save adrianstevens/5c1c33e20d911cf9121c to your computer and use it in GitHub Desktop.
Save adrianstevens/5c1c33e20d911cf9121c to your computer and use it in GitHub Desktop.
IOS mapping - distance between two points
using System;
using CoreGraphics;
using Foundation;
using UIKit;
using MapKit;
using CoreLocation;
using System.Diagnostics;
namespace MappingApp
{
public partial class MappingAppViewController : UIViewController
{
CLLocationManager locationManager = new CLLocationManager();
public MappingAppViewController(IntPtr handle) : base(handle)
{
}
public override void DidReceiveMemoryWarning()
{
// Releases the view if it doesn't have a superview.
base.DidReceiveMemoryWarning();
// Release any cached data, images, etc that aren't in use.
}
#region View lifecycle
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
// TODO: Step 1a - add the map view
var map = new MKMapView(this.View.Frame);
this.View.Add (map);
map.MapType = MKMapType.Hybrid;
map.RotateEnabled = true;
map.ZoomEnabled = true;
map.PitchEnabled = true;
if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0) )
locationManager.RequestWhenInUseAuthorization ();
map.ShowsUserLocation = true;
// TODO: Step 1b - change the map style
// map.MapType = MKMapType.Standard;
// map.MapType = MKMapType.Satellite;
// map.MapType = MKMapType.Hybrid;
// TODO: Step 1c - enable/disable interactions
// map.ZoomEnabled = false;
// map.ScrollEnabled = false;
// TODO: Step 1d - show user location
// if (UIDevice.CurrentDevice.CheckSystemVersion (8, 0)) {
// locationManager.RequestWhenInUseAuthorization();
// }
// map.ShowsUserLocation = true;
/*
CLLocation fromAddress = new CLLocation(FromAddress.lat,FromAddress.lng);
CLLocation toAddress = new CLLocation (ToAddress.lat, ToAddress.lng); //ios mapkit distance test.
distance = toAddress.DistanceFrom (fromAddress); //google distance test
*/
//pins
var pointAnnotation = new MKPointAnnotation ();
pointAnnotation.SetCoordinate (new CLLocationCoordinate2D (37.797530, -122.402590));
map.AddAnnotation (pointAnnotation);
pointAnnotation = new MKPointAnnotation ();
pointAnnotation.SetCoordinate (new CLLocationCoordinate2D (42.374172, -71.120639));
map.AddAnnotation (pointAnnotation);
MKDirectionsRequest request = new MKDirectionsRequest ();
MKPlacemarkAddress placemark = null;
var orignPlaceMark = new MKPlacemark (new CLLocationCoordinate2D (37.797530, -122.402590), placemark);
var sourceItem = new MKMapItem(orignPlaceMark);
//End at Xamarin Cambridge Office
var destPlaceMark = new MKPlacemark(new CLLocationCoordinate2D(42.374172, -71.120639), placemark);
var destItem = new MKMapItem(destPlaceMark);
request.Source = sourceItem;
request.Destination = destItem;
request.RequestsAlternateRoutes = true;
var directions = new MKDirections (request);
directions.CalculateDirections ((response, error) => {
if(error != null)
{
Console.WriteLine(error.LocalizedDescription);
}
else
{
foreach (var route in response.Routes)
{
map.AddOverlay(route.Polyline);
Console.WriteLine(route.Name + " " + route.Distance + "(m) " + route.ExpectedTravelTime + "(s)");
}
}
});
MKDirections dir = new MKDirections ();
map.Delegate = new MyMapDelegate ();
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
}
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
}
public override void ViewWillDisappear(bool animated)
{
base.ViewWillDisappear(animated);
}
public override void ViewDidDisappear(bool animated)
{
base.ViewDidDisappear(animated);
}
#endregion
}
// Setup a map delgate to handle annotations and relative eventing
class MyMapDelegate : MKMapViewDelegate
{
MKPolylineRenderer polylineRenderer = null;
public override MKOverlayRenderer OverlayRenderer(MKMapView mapView, IMKOverlay overlay)
{
if (overlay is MKPolyline)
{
if (polylineRenderer == null)
{
polylineRenderer = new MKPolylineRenderer(overlay as MKPolyline);
polylineRenderer.StrokeColor = UIColor.Green;
}
return polylineRenderer;
}
else
{
Debug.WriteLine("OverlayRenderer() - Unknown overlay type!");
return null;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment