Xamarin / make a little debugging tool for MKMapView / maps when you don’t want to spend actual time moving around

When you are making an app with GPS routes and you want to create test data (for unit tests or just testing without moving around), you can use the following code;

In ViewDidLoad or whatever used to initialise the view, put:

  1. var tapRecogniser = new UITapGestureRecognizer(this, new MonoTouch.ObjCRuntime.Selector(“MapTapSelector:));
  3.             map.AddGestureRecognizer (tapRecogniser);

And somewhere in the same class;

  1. [Export(“MapTapSelector:)]
  2.         protected void OnMapTapped(UIGestureRecognizer sender)
  3.         {
  4.             CLLocationCoordinate2D tappedLocationCoord =  map.ConvertPoint(sender.LocationInView(map), map);
  6.             locations.Add (tappedLocationCoord);
  8.             HandleDidUpdateUserLocation (this,
  9.                 new CLLocationsUpdatedEventArgs(
  10.                     new CLLocation []{
  11.                         new CLLocation(tappedLocationCoord.Latitude, tappedLocationCoord.Longitude)
  12.                     }
  13.                 )
  14.             );
  16.         }

In my case

  1. List<CLLocationCoordinate2D> locations

which contains all the already clicked locations so we can draw a nice path from all taps which is the same as  say a run or a walk or a drive. Add some small debugging fields for speed and altitude and you can test most real life situations.

Be the first to leave a comment. Don’t be shy.

Join the Discussion

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>