Yassine Ben Hamida
Yassine Ben Hamida

Reputation: 630

CodenameOne : cannot display google map

I want to use the latest 'improved' native maps api listed in this link, i have installed the cn1lib from the codename One extensions in netbeans, tried the provided code listed in the link with my google api key, but i can't seem to get it right.

i keep getting an blank map on the phone screen with some exception stack traces in the background, the stack trace reads :

[EDT] 0:0:1,313 - Exception: java.lang.IllegalArgumentException - create image failed for the given image data of length: 253

here's a screenshot of what i am getting enter image description here

below is a code snippet of what i have tried :

private static final String HTML_API_KEY = "mykey";
private Form current;
private Resources theme;
public void init(Object context) {
    // use two network threads instead of one
    updateNetworkThreadCount(2);

    theme = UIManager.initFirstTheme("/theme");

    // ....
}

public void start() {
    if (current != null) {
        current.show();
        return;
    }
    Form hi = new Form("Native Maps Test");
    hi.setLayout(new BorderLayout());
    final MapContainer cnt = new MapContainer(new GoogleMapsProvider(HTML_API_KEY));

    Button btnMoveCamera = new Button("Move Camera");
    btnMoveCamera.addActionListener(e->{
        cnt.setCameraPosition(new Coord(-33.867, 151.206));
    });
    Style s = new Style();
    s.setFgColor(0xff0000);
    s.setBgTransparency(0);
    FontImage markerImg = FontImage.createMaterial(FontImage.MATERIAL_PLACE, s, Display.getInstance().convertToPixels(3));

    Button btnAddMarker = new Button("Add Marker");
    btnAddMarker.addActionListener(e->{

        cnt.setCameraPosition(new Coord(41.889, -87.622));
        cnt.addMarker(
                EncodedImage.createFromImage(markerImg, false),
                cnt.getCameraPosition(),
                "Hi marker",
                "Optional long description",
                 evt -> {
                         ToastBar.showMessage("You clicked the marker", FontImage.MATERIAL_PLACE);
                 }
        );

    });

    Button btnAddPath = new Button("Add Path");
    btnAddPath.addActionListener(e->{

        cnt.addPath(
                cnt.getCameraPosition(),
                new Coord(-33.866, 151.195), // Sydney
                new Coord(-18.142, 178.431),  // Fiji
                new Coord(21.291, -157.821),  // Hawaii
                new Coord(37.423, -122.091)  // Mountain View
        );
    });

    Button btnClearAll = new Button("Clear All");
    btnClearAll.addActionListener(e->{
        cnt.clearMapLayers();
    });

    cnt.addTapListener(e->{
        TextField enterName = new TextField();
        Container wrapper = BoxLayout.encloseY(new Label("Name:"), enterName);
        InteractionDialog dlg = new InteractionDialog("Add Marker");
        dlg.getContentPane().add(wrapper);
        enterName.setDoneListener(e2->{
            String txt = enterName.getText();
            cnt.addMarker(
                    EncodedImage.createFromImage(markerImg, false),
                    cnt.getCoordAtPosition(e.getX(), e.getY()),
                    enterName.getText(),
                    "",
                    e3->{
                            ToastBar.showMessage("You clicked "+txt, FontImage.MATERIAL_PLACE);
                    }
            );
            dlg.dispose();
        });
        dlg.showPopupDialog(new Rectangle(e.getX(), e.getY(), 10, 10));
        enterName.startEditingAsync();
    });

    Container root = LayeredLayout.encloseIn(
            BorderLayout.center(cnt),
            BorderLayout.south(
                    FlowLayout.encloseBottom(btnMoveCamera, btnAddMarker, btnAddPath, btnClearAll)
            )
    );

    hi.add(BorderLayout.CENTER, root);
    hi.show();

}

public void stop() {
    current = Display.getInstance().getCurrent();
}

public void destroy() {
}

i am using googleplay services 9.4 & java 8, i have tried this solution but still nothing

Upvotes: 0

Views: 72

Answers (1)

Shai Almog
Shai Almog

Reputation: 52760

Change this:

final MapContainer cnt = new MapContainer(new GoogleMapsProvider(HTML_API_KEY));

To:

final MapContainer cnt = new MapContainer(HTML_API_KEY);

It uses Google by default.

Also make sure that HTML_API_KEY contains a valid JavaScript maps key and not the string it currently has there.

Upvotes: 2

Related Questions