package com.mycompany.myapp;
import com.codename1.googlemaps.MapContainer;
import com.codename1.io.Log;
import com.codename1.location.Location;
import com.codename1.location.LocationManager;
import com.codename1.maps.Coord;
import com.codename1.maps.MapListener;
import com.codename1.ui.Button;
import com.codename1.ui.Command;
import com.codename1.ui.Component;
import com.codename1.ui.Container;
import com.codename1.ui.Dialog;
import com.codename1.ui.Display;
import com.codename1.ui.EncodedImage;
import com.codename1.ui.Form;
import com.codename1.ui.Label;
import com.codename1.ui.SideMenuBar;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.layouts.LayeredLayout;
import com.codename1.ui.plaf.Style;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.table.TableLayout;
import com.codename1.ui.util.Resources;
import java.io.IOException;
public class MyApplication {
private Form current;
public void init(Object context) {
// Pro users - uncomment this code to get crash reports sent to you automatically
Display.getInstance().addEdtErrorHandler(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
evt.consume();
Log.p("Exception in AppName version " + Display.getInstance().getProperty("AppVersion", "Unknown"));
Log.p("OS " + Display.getInstance().getPlatformName());
Log.p("Error " + evt.getSource());
Log.p("Current Form " + Display.getInstance().getCurrent().getName());
Log.e((Throwable)evt.getSource());
Log.sendLog();
}
});
try {
Resources theme = Resources.openLayered("/theme");
UIManager.getInstance().setThemeProps(theme.getTheme(theme.getThemeResourceNames()[0]));
Display.getInstance().setCommandBehavior(Display.COMMAND_BEHAVIOR_SIDE_NAVIGATION);
UIManager.getInstance().getLookAndFeel().setMenuBarClass(SideMenuBar.class);
} catch(IOException e){
e.printStackTrace();
}
}
public void start() {
if(current != null){
current.show();
return;
}
Form hi = new Form("Native Maps Test");
hi.setLayout(new LayeredLayout());
hi.setScrollable(false);
Container map = new Container(new BorderLayout());
final MapContainer cnt = new MapContainer();
final Label lbl = new Label("Location: ...");
cnt.addMapListener(new MapListener() {
public void mapPositionUpdated(Component source, int zoom, Coord center) {
//lbl.setText("Location: " + center.getLatitude() + ", " + center.getLongitude());
lbl.setText("0 lon: " + cnt.getCoordAtPosition(0, 0).getLongitude()+ " w lon " + cnt.getCoordAtPosition(Display.getInstance().getDisplayWidth(), 0).getLongitude());
}
});
map.addComponent(BorderLayout.SOUTH, lbl);
map.addComponent(BorderLayout.CENTER, cnt);
hi.addCommand(new Command("Move Camera") {
public void actionPerformed(ActionEvent ev) {
cnt.setCameraPosition(new Coord(-33.867, 151.206));
}
});
hi.addCommand(new Command("Add Marker") {
public void actionPerformed(ActionEvent ev) {
try {
cnt.setCameraPosition(new Coord(41.889, -87.622));
cnt.addMarker(EncodedImage.create("/maps-pin.png"), new Coord(41.889, -87.622), "Hi marker", "Optional long description", new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Dialog.show("Marker Clicked!", "You clicked the marker", "OK", null);
}
});
} catch(IOException err) {
// since the image is iin the jar this is unlikely
err.printStackTrace();
}
}
});
hi.addCommand(new Command("Add Marker Here") {
public void actionPerformed(ActionEvent ev) {
try {
cnt.addMarker(EncodedImage.create("/maps-pin.png"), cnt.getCameraPosition(), "Marker At", "Lat: " + cnt.getCameraPosition().getLatitude() + ", " + cnt.getCameraPosition().getLongitude(), new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Dialog.show("Marker Clicked!", "You clicked the marker", "OK", null);
}
});
} catch(IOException err) {
// since the image is iin the jar this is unlikely
err.printStackTrace();
}
}
});
hi.addCommand(new Command("Add Path") {
public void actionPerformed(ActionEvent ev) {
cnt.setCameraPosition(new Coord(-18.142, 178.431));
cnt.addPath(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
);
}
});
hi.addCommand(new Command("Clear All") {
public void actionPerformed(ActionEvent ev) {
cnt.clearMapLayers();
}
});
hi.show();
hi.addComponent(map);
TableLayout layout = new TableLayout(3, 3);
Container c = new Container();
c.setLayout(layout);
Button fetchme = new Button("Fetch me now!");
fetchme.setPreferredW(200);
Style s = new Style();
s.setAlignment(Label.CENTER);
fetchme.setUnselectedStyle(s);
TableLayout.Constraint constraint = layout.createConstraint(2, 2);
constraint.setWidthPercentage(100);
constraint.setHeightPercentage(90);
constraint.setHorizontalAlign(Component.CENTER);
constraint.setVerticalAlign(Component.BOTTOM);
c.addComponent(constraint, fetchme);
hi.addComponent(c);
LocationManager lm = LocationManager.getLocationManager();
fetchme.setEnabled(false);
try {
Location location = lm.getCurrentLocationSync(30000);
fetchme.setEnabled(true);
cnt.setCameraPosition(new Coord(location.getLatitude(), location.getLongitude()));
cnt.zoom(new Coord(location.getLatitude(), location.getLongitude()), 16);
cnt.addMarker(EncodedImage.create("/maps-pin.png"), new Coord(location.getLatitude(), location.getLongitude()), "Hi marker", "Optional long description", new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// nothing
}
});
} catch (Exception ex) {
if(Display.isInitialized()) {
Dialog.show(null, "Can't get location - is your GPS on?", Dialog.TYPE_ERROR, null, null, null, 2000);
}
}
}
public void stop() {
current = Display.getInstance().getCurrent();
}
public void destroy() {
}
}
Enter code here...