1
我正在使用代碼名稱來開發一個應用程序,它具有谷歌地圖,我想要當我打開應用程序它獲得我當前的位置我怎麼能做到這一點,這裏是我的代碼。google map in codename one
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());
}
}
謝謝夏嘉曦它的工作,現在我該怎樣改善這種相同的代碼,讓標記顯示我的位置移動每當我移動以及如何自動放大我的位置。 –
您可以添加/刪除標記,我建議使用位置偵聽器而不是此同步方法。您也可以使用API來控制相機的位置 –