Codename One - 正确使用MapContainer(VIDEO)的addMarker(引脚,坐标)

时间:2018-05-06 18:34:49

标签: codenameone

此问题与CN1Lib for Google Map相关。

我的问题是如果我不使用方法addMarker(com.codename1.ui.Component marker, com.codename1.maps.Coord location),方法setCameraPosition(com.codename1.maps.Coord crd)在我的Android 7真实设备上无法正常工作。

为了更清楚,使用以下代码我扩展了类MapContainer:问题是我无法在不破坏功能的情况下删除行this.setCameraPosition(coordinate);(如果我删除该行,那么我就不能将标记添加到轻敲位置。)

我不确定我的代码或CN1Lib中是否存在错误。

public class MyMapContainer extends MapContainer {

    private Label pin;
    private Location markerLocation = new Location(0.0, 0.0);
    MapObject mapObject = null;

    /**
     * Constructor that creates a default pinVector to be used with
     * addMarker(Coord coordinate)
     */
    public MyMapContainer() {
        super();
        Style s = new Style();
        s.setFgColor(ColorUtil.BLACK);
        pin = new Label(FontImage.createMaterial(FontImage.MATERIAL_PLACE, s, 10));
    }

    /**
     * Constructor
     *
     * @param pinVector to be used with addMarker(Coord coordinate)
     */
    public MyMapContainer(Image pinVector) {
        this();
        if (pinVector != null) {
            pin = new Label(pinVector);
        }
    }

    /**
     * Adds a single marker (removes the previous one, if any)
     *
     * @param coordinate
     */
    public void addMarker(Coord coordinate) {
        if (mapObject != null) {
            pin.remove();
            this.removeMapObject(mapObject);
            Log.p("Marker removed");
        }
        mapObject = this.addMarker(pin, coordinate);
        this.setCameraPosition(coordinate);
        Log.p("Marker added to the coordinate: " + coordinate.toString());

        markerLocation.setLatitude(coordinate.getLatitude());
        markerLocation.setLongitude(coordinate.getLongitude());
    }

    /**
     * Returns the marker location
     *
     * @return markerLocation
     */
    public Location getMarkerLocation() {
        return markerLocation;
    }

}

为了测试扩展类,我在主类中编写了这段代码:

mapContainer.addMarker(initialCoordinate);
mapContainer.zoom(initialCoordinate, 18);
mapContainer.addTapListener(e -> {
    Coord userTappedCoord = mapContainer.getCoordAtPosition(e.getX(), e.getY());
    Log.p("userTappedCoord: " + userTappedCoord);
    mapContainer.addMarker(userTappedCoord);
});

---更新(回复Shai的评论)

如果我用以下行替换this.setCameraPosition ...

this.revalidate();
if (Display.getInstance().getCurrent() != null) {
    Display.getInstance().getCurrent().revalidate();
}
//this.setCameraPosition(coordinate);

......问题是一样的:我无法添加新标记。为了更清楚,请看这个视频: https://www.informatica-libera.net/VID_20180507_113028.mp4

在视频中,白色圆圈对应于我点按屏幕的点。我点击它三次(您可以听到视频中的鼠标点击),但每次点击后标记会进入随机位置,然后返回到第一个位置。此视频已在设备场中注册,但我的真实设备存在相同的问题。

1 个答案:

答案 0 :(得分:1)

我猜这是问题所在:

pin.remove();
removeMapObject(mapObject);

其中一个工作不正常,因此引脚保留其位置。试着这样做:

removeMapObject(mapObject);
pin.remove();

如果不起作用,请通过创建新的组件实例来检查论文,而不是再次添加相同的组件。假设解决了问题,请提出问题here