[mono-android] Googlemap sample and Java -> MDr interop
Yann Schwartz
abolibibelot at gmail.com
Mon Mar 21 09:13:15 EDT 2011
>
> On Mar 19, 2011, at 4:25 PM, Yann Schwartz wrote:
> >* I could write everything in Java *shudder* or have a nicely populated ArrayList<OverlayItem> sent from a .NET class instance (or from a static member of a .NET class, I'm not being difficult here) where the grunt work will be done.*
> It'll be ugly, but you could create such a list in managed code by using Android.Runtime.JavaList<Java.Lang.Object> (which is implemented as a java.util.ArrayList), then using JNIEnv to create OverlayItem instances and set fields/etc:
>
> var list = new JavaList<Java.Lang.Object>();
> IntPtr OverlayItem = JNIEnv.FindClass("com/google/android/maps/OverlayItem");
> IntPtr ctor = JNIEnv.GetMethodId (OverlayItem, "<init>", "(...)V"); // TODO: prototype
> // ...
> IntPtr instance = JNIEnv.NewObject(OverlayItem, ctor /* ctor args... */);
> list.Add(new Java.Lang.Object(instance));
>
> See also: http://mono-android.net/Documentation/API_Design#Jave_Native_Interface_Support
>
> You could then pass `list` via JNI to some Java method..
>
>
After some harrowing time passed with javap and like, I've managed to follow
your advice. I took the GoogleMaps sample as a start and added a way to pass
a List<Object> from the C# Activity to the Java one. I build the overlays
inside the java activity (I did not feel brave enough to do it from the C#
code).
* The C# Activity (with the JNI call to the java communication class)
https://gist.github.com/878737
I was pleasantly surprised on how well the .NET types were marshalled in the
List<Object>. On the other hand, JNI errors don't leave much clues to
investigate. And using javap on the .class files compiled in obj/Debug lets
you get those pesky JNI signatures without making you want to kill yourself.
* The Java Activity subclassing the MapActivity and adding overlays using
coordinates set from the C# side.
https://gist.github.com/878733
Not much to say here, beside that you have to code in Java to appreciate
type inference, value types and [] operator overloading in C#...
* The placeholder Java class used for passing information (fugly but it
works for now)
https://gist.github.com/879415
Adding your own java class works fine, but any compilation error is
advertised as a Signing Error and you have to walk through the extremely
long messagebox that appears then. It's actually too long to display in the
box (you can type Ctrl+C to get all the text though), a textbox could be
used here.
Anayway, thanks to your hints, and thanks to monodroid, I can display a map
a put stuff on it. Yay!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/mailman/private/monodroid/attachments/20110321/b5fb7b5b/attachment.html
More information about the Monodroid
mailing list