Fork us on GitHub

Mapping Natively

This has been a frequent RFE in the groups but it never made ...

Mapping Natively

This has been a frequent RFE in the groups but it never made its way up because of the complexity involved. A corporate account recently requested support for native Maps so we had to promote the task upwards. We decided to build the native maps as an external cn1lib rather than build them into Codename One itself, the reasoning is two fold:
1. Show how to map a native component - next time someone wants to add a native widget into Codename One they can use the Maps as a reference and go around us.

2. Since Maps are VERY complex we wanted to give developers the ability to customize the native code easily.

Going forward we might introduce more and more extensions as cn1libs rather than as builtin functionality. There is one drawback we didn't predict though, Googles Map API for iOS is 50mb in size which makes sending the builds pretty slow. This is solveable by changing the compression option in the project properties to compress the jar, it is still pretty large though.

To browse/checkout the code/demo-test for maps just go to the project page here . For simplicities sake you can just download the binary here and place it in your lib directory for the project then right click the project and select refresh client libs.

However, configuration does require a bit of work and there are a few limitations.

1. The native maps are only supported on Android devices that have the Google Play store (e.g. not on Amazon Kindle) and on iOS devices. All other devices will show the MapComponent by default.
Map component will be used on the simulator as well.

2. Since a native component is used placing overlays is problematic. You will need to use Dialogs and the API's of the MapContainer class to implement this.

The configuration portion is the hardest part, Google made it especially painful in the Google typical way. You can follow the instructions from Google to get started for Android and for iOS .
You will need to follow their instructions to generate your map keys. Then define the following build arguments within your project:

Make sure to replace the values YOUR_ANDROID_API_KEY & YOUR_IOS_API_KEY with the values you obtained from the Google Cloud console by following the instructions for Android and for iOS .

Now that all of that is done you should be able to create a Map, add markers and paths:
At the time of this writing there are still issues with Maps some of which are pretty hard to resolve. Currently when poping a dialog over the Map it turns white on Android and we aren't really sure why.  There are some artifacts on iOS as well but those might not be fixable.

Notice: This post was automatically converted using a script from an older blogging system. Some elements might not have come out as intended.... If that is the case please let us know via the comments section below.

Share this Post:

Posted by Shai Almog

Shai is the co-founder of Codename One. He's been a professional programmer for over 25 years. During that time he has worked with dozens of companies including Sun Microsystems.
For more follow Shai on Twitter & github.