adding functionalities to native maps

107 views
Skip to first unread message

gt_bz

unread,
Feb 23, 2015, 9:09:51 AM2/23/15
to codenameone...@googlegroups.com
IDE: NetBeans 7.4
Desktop OS: Windows 8.1

Hello,
I want to add wrappers for some Android Google Maps API functions to native maps (e. g. Google Maps InfoWindow, fitBounds() ) and build my own version of CN1Lib.  I have downloaded codenameone-google-maps sources by cloning svn repository, but in the project I can see only InternalNativeMaps.java and MapContainer.java files. I suppose that I must modify native implementations for Android and iOS. How can I accomplish my goal?

Shai Almog

unread,
Feb 23, 2015, 12:36:41 PM2/23/15
to codenameone...@googlegroups.com
Hi,
the native code is under the native directory in the project root. See a guide here http://www.codenameone.com/how-do-i---access-native-device-functionality-invoke-native-interfaces.html
We also discuss native interfaces here https://www.udemy.com/learn-mobile-programming-by-example-with-codename-one/

gt_bz

unread,
Feb 24, 2015, 9:46:23 AM2/24/15
to codenameone...@googlegroups.com
Thank you for your tips. I managed to add "zoom to path" functionality, ie moving camera such that the entire specified path is visible and centered on screen with maximum possible zoom. But there are some glitches. I'm using Samsung Galaxy Trend with Android 4.0.4.
Every second time form with native MapContainer is not showing properly (look at the attached photo. Why not screenshot? Because when I take screenshot, it contains form from which map form was invoked). On the photo you can see that previous form is visible through places where normally zoom controls and Google logo are seen (marked with red rectangles). Small part is visible also on the top of the photo. When I rotate phone, screen gets refreshed and I can see form from which I was invoking map form. So it looks like map form is drawn, but focus is set to previous form (map is not static, I can see that it is loading tiles). Code:

IntenalNativeMapsImpl.java (for Android):
public void createBoundsBuilder() {
        //AndroidNativeUtil.getActivity().runOnUiThread(new Runnable() { //tried that with no luck
            //public void run() {
                boundsBuilder = LatLngBounds.builder();
          //  }
        //});
    }
   
    public void extendBounds(final double lat, final double lon) {
       // AndroidNativeUtil.getActivity().runOnUiThread(new Runnable() { //tried that with no luck
         //   public void run() {
                boundsBuilder.include(new LatLng(lat, lon));
          //  }
        //});
       
    }
   
    public void fitBounds() {
        final LatLngBounds bounds = boundsBuilder.build();
        AndroidNativeUtil.getActivity().runOnUiThread(new Runnable() {
            public void run() {
                mapInstance.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 20));
            }
        });

MapContainer.java:
public void fitBounds(Coord... path) {
        if (isNativeMaps()) {
            internalNative.createBoundsBuilder();
            for(Coord c : path) {
                internalNative.extendBounds(c.getLatitude(), c.getLongitude());
            }
            internalNative.fitBounds();
        } else {
            this.zoom(path[0], this.getMaxZoom());
        }
    }

I tried invoking fitBounds() before and after map show but effect is always pretty much the same. Map Form and MapContainer are created once and reused. When I am using mapContainer.zoom(Coord, zoom) instead then everything is working fine. Do you have any ideas?
DSC_0684.png

Shai Almog

unread,
Feb 24, 2015, 10:23:47 AM2/24/15
to codenameone...@googlegroups.com
If I understand correctly you are seeing the map painting on top of the form instead of remaining within Component bounds?
Did you read this article: http://www.codenameone.com/blog/understanding-peer-native-components-why-codename-one-is-so-portable
Is it possible the code you are using triggers a change in the size of the View object?

gt_bz

unread,
Feb 25, 2015, 3:02:30 AM2/25/15
to codenameone...@googlegroups.com
Not exactly. I will try to be more precise. Look at attached picture. Yes I did, but I don't know why invoking native functions (LatLngBounds.builder() ) is ruining drawing. It shouldn't, but I don't have knowledge about internal implementation details of those Android API functions.
If I use zoom function orginally implemented in googlemaps cn1lib everything is working fine. If only createBoundsBuilder() is called in fitBounds(), then I'm getting that strange behaviour.
explanation.png

gt_bz

unread,
Feb 25, 2015, 9:57:01 AM2/25/15
to codenameone...@googlegroups.com
Update: I'm experiencing that behaviour even if in native implementation I don't call any Google Maps API functions. Simple int i = 5; is enough. It seems that calling any function that was not included in original cn1lib causes the problem. But I don't know why.

Shai Almog

unread,
Feb 25, 2015, 12:00:28 PM2/25/15
to codenameone...@googlegroups.com
Is it possible your code is blocking one of the paint threads either the EDT or the native Android thread?
Or are you accessing UI in the wrong thread triggering something?

gt_bz

unread,
Feb 26, 2015, 6:50:17 AM2/26/15
to codenameone...@googlegroups.com
I don't think so. Have you looked over source code I pasted? Here I attached diff file which contains every change that I made to original googlemaps cn1lib. Please, look into it, I added only a few lines. Crucial parts of my CN1 App:
final MapContainer mapCnt = new MapContainer();
final Form nativeMapForm = new Form();
/*...*/
InfiniteProgress ip = new InfiniteProgress();
final Dialog dlg = ip.showInifiniteBlocking();
/*load path data from web service & prepare Coord array */
dlg.dispose();
nativeMapForm.addComponent(BorderLayout.CENTER, mapCnt);
mapCnt.clearMapLayers();
mapCnt.addPath(path);
mapCnt.fitBounds(path); //If I replace this with: mapCnt.zoom(path[0], mapCnt.getMaxZoom()-3); then everything is working fine. Always.
nativeMapForm.show();

I hope that now everything is clear. If necessary I could create a simple test case for you
diff.txt

Shai Almog

unread,
Feb 26, 2015, 11:53:11 AM2/26/15
to codenameone...@googlegroups.com
Its hard to tell with peer components, did you run this with DDMS? Do you see anything in the console?

gt_bz

unread,
Feb 27, 2015, 3:02:23 AM2/27/15
to codenameone...@googlegroups.com
Nothing suspicious except "native focus gain" message (look at the attachment). It appears only when I'm experiencing discussed issue. Is it telling you sth?
focus_gain.png

Shai Almog

unread,
Feb 27, 2015, 10:22:38 AM2/27/15
to codenameone...@googlegroups.com
Try setFocusable(false) on the Map component in the Codename One side. That might be related in some way.

gt_bz

unread,
Mar 2, 2015, 8:39:26 AM3/2/15
to codenameone...@googlegroups.com
It certainly is. Your advice was helpful, however issue doesn't appear to be completely resolved. I can still see previous form through areas where zoom control and Google logo should be, but when I tap on the screen, map becomes normal and it has focus. I can change camera positon, click on markers, etc (look at the attachement). When I rotate device before tapping, map disappears from the screen. Is there anything else that could be done to fix that?
explanation2.png

Shai Almog

unread,
Mar 2, 2015, 10:37:06 AM3/2/15
to codenameone...@googlegroups.com
These are the sort of hard to detect edge cases that might be a bug on our side but might be a result of some issues in the widget.
Is this something you can isolate to a test case that happens uniformly on devices? If so you can just file an issue in the issue tracker.

It might take some time to address though since we have a pretty deep pipeline and this doesn't seem like something we can just quickly figure out.

gt_bz

unread,
Mar 6, 2015, 5:36:27 AM3/6/15
to codenameone...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages