Youssef Abdeen
Youssef Abdeen

Reputation: 49

google map in codename one

i am using code-name one to develop an app which has google map in it, i want when i open the app it gets my current location how could i do that, here is the code i have.

    static Location lastKnownLocation;
@Override
protected void beforeMap(Form f) {
    MapContainer mapContainer = new MapContainer(new GoogleMapsProvider("AIzaSyCyy_vOWn3DvR3Y8pzAWUmKTzBaDa81Tfc"));
    lastKnownLocation = LocationManager.getLocationManager().getLastKnownLocation();
    Style s = new Style();
    s.setBgTransparency(0);
    s.setFgColor(0);
    mapContainer.addMarker(FontImage.createMaterial(FontImage.MATERIAL_MY_LOCATION, s).toEncodedImage(), new Coord(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude()), "", "", evt -> {
        ToastBar.showErrorMessage(lastKnownLocation.toString());
    });
    mapContainer.addTapListener(evt -> {
        Coord coord = mapContainer.getCoordAtPosition(evt.getX(), evt.getY());
        mapContainer.addMarker(FontImage.createMaterial(FontImage.MATERIAL_LOCATION_ON, s).toEncodedImage(), coord, "", "", null);
    });

    f.add(BorderLayout.CENTER, mapContainer);
     FloatingActionButton fab = FloatingActionButton.createFAB(FontImage.MATERIAL_ADD);
    fab.addActionListener(e -> {
        ParseObject object = ParseObject.create("Geo");
        object.put("current", new ParseGeoPoint(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude()));
        if (ParseUser.getCurrent() != null)
            object.put("user", ParseUser.getCurrent());
        try {
            object.save();
            ToastBar.showErrorMessage("Geo Sent");
        } catch (ParseException ex) {
            ex.printStackTrace();
            ToastBar.showErrorMessage(ex.getMessage());
        }
    });
    fab.bindFabToContainer(f.getContentPane());
}

}

Upvotes: 1

Views: 516

Answers (1)

Shai Almog
Shai Almog

Reputation: 52760

Notice that on the device your current location will be used and highlighted since the device runs the native maps whereas the simulator runs a simulation.

Having said that you can get your location from the LocationManager class.

E.g.:

Location position = LocationManager.getLocationManager().getCurrentLocationSync();

Upvotes: 1

Related Questions