insertXY()到新草图 - Openlayers

时间:2013-04-17 00:06:49

标签: javascript android openlayers

我在Android手机上运行OpenLayers.js?mobile。

我希望能够绘制线条&来自GPS的多边形以及单击屏幕以添加功能。如果我点击屏幕开始使用新顶点的草图,那么我可以调用insertXY(lat,lon)或单击以添加顶点,它可以按预期工作。

但是,在尝试通过在激活的OpenLayers.Control.DrawFeature上调用insertXY(lat,lon)来开始一个新草图时,在调用抛出此错误的finishSketch()之前没有任何反应 - “TypeError:无法读取属性”几何'of null“。

如何使用insertXY()开始草图?

这应该与此示例一样:http://openlayers.org/dev/examples/editing-methods.html#

以下是相关的javascript:

var map;
var draw;


map = new OpenLayers.Map({
    div: "map",
    layers: [
        var roads = new OpenLayers.Layer.Vector()
    ],
    center: new OpenLayers.LonLat(0, 0),
    zoom: 1
});



draw = new OpenLayers.Control.DrawFeature(roads, OpenLayers.Handler.Path);
map.addControl(draw);

draw.activate()


function add_GPS_Line_vertex(lon,lat) {
    /* this function only works if sketch has been manually started */
    var lat = parseFloat(lat);
    var lon = parseFloat(lon);
    var point = new OpenLayers.Geometry.Point(lon, lat);
    lon = point.x;
    lat = point.y;
    draw.insertXY(lon,lat);
}

1 个答案:

答案 0 :(得分:1)

如果鼠标永远不会悬停在地图上,我也会在链接示例中重现此错误(http://openlayers.org/dev/examples/editing-methods.html#),并且您只通过坐标添加点,然后调用draw.finishSkech () 方法。

我通过仅在鼠标位于地图上时启动绘图来实现此解决方法:

map.events.register('mousemove', map, function (e) {

   //make sure it only runs once (with a flag or something)
   if (mouseFlag == true) {
     mouseFlag = false;

     // do your drawing
     draw.insertXY(lon,lat);

     draw.finishSketch();
  }
});

或者如果您只需要绘制一个形状,您可以随时使用:

draw.drawFeature(geometry);