2016-12-08 155 views
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()); 
} 

}

回答

1

注意,设备上的当前位置将被使用,并强调因为设备运行的本地地图,而模拟器运行模拟。

话虽如此,您可以从LocationManager类获得您的位置。

例如为:

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

谢谢夏嘉曦它的工作,现在我该怎样改善这种相同的代码,让标记显示我的位置移动每当我移动以及如何自动放大我的位置。 –

+0

您可以添加/删除标记,我建议使用位置侦听器而不是此同步方法。您也可以使用API​​来控制相机的位置 –