|
|
TO: monotouch@lists.ximian.com
SUBJECT: MapKit, protocols and stuff...
7th September 2009
I recently started playing around with MapKit via MonoTouch. At first I struggled, but in the end got this sample working: http://conceptdev.blogspot.com/2009/09/monotouch-mapkit-101.html
One of the issues I had was needing to [Export("reverseGeocoder:didFindPlacemark:")] because it wasn't already on MKReverseGeocoderDelegate - not a big deal, just a learning experience.
BUT I still haven't figured out MKAnnotation, MKMapViewDelegate.GetViewForAnnotation() and MKMapView.AddAnnotation(). No matter what combination of stuff I try, it manages to crash [SIGSEGV] "warning: Trying to remove a section from the ordered section list that did not exist at 0x2d2000" (misaligned stack bug?). You can look at the most recent 'attempt' in the sample code at http://conceptdevelopment.net/iPhone/MapKit01/Main.cs.htm (the AddAnnotation() call is commented out, but the related MKAnnotation and MKMapViewDelegate code is all there). Based on Console.WriteLine it crashes after GetViewForAnnotation() returns...
Basically I can't figure out what I'm doing wrong... suggestions welcome!
I also have a related question to ask - what is the plan for handling/implementing ObjC @protocol in C#? It's kinda hard to explain, but here goes (and apols in advance if I've missed the point!):
1) @protocols seem "kinda" like C# Interfaces in that a @protocol defines interface ONLY, and classes can implement multiple @protocols
2) @protocols are different to C# Interfaces in that members can be 'optionally' implemented
3) MKAnnotation is a @protocol, with one required property (coordinate) and two optional (title, subtitle)
4) In MonoTouch, MKAnnotation is an abstract class with (title, subtitle) properties and coordinate as an abstract property. That kinda makes sense in terms of 'modelling' the @protocol
5) BUT in ObjC "A placemark is also an annotation and conforms to the MKAnnotation protocol, whose properties and methods
include the placemark coordinate and other information. Because they are
annotations, you can add them directly to the map view." ie. MKMapVew.AddAnnotation()
takes an MKAnnotation @protocol parameter which means it will accept an MKAnnotation class (or subclass) AND ALSO an MKPlacemark instance.
6) HOWEVER in C# the method definition is MKMapView.AddAnnotation (MKAnnotation annotation) ... meaning it will accept any implementation of the abstract MKAnnotation subclass but NOT an MKPlacemark.
So part of my problem was successfully getting an MKPlacemark back in my code but not being able to add it to the MKMapView directly, and also not being able to construct a valid MKAnnotation subclass that would successfully add to the MKMapView. It seems more logical to me that MonoTouch provide both an IMKAnnotation and abstract class MKAnnotation:IMKAnnotation so that methods can accept the interface as a parameter... of course I've no idea how this would translate into the compiler
(but it is already stripping out the
ModelNotImplementedExceptions)...
MonoTouch.MapKit Class Diagram
|
|
Useful links
|