简体   繁体   English

更新openlayers 3中的功能坐标

[英]Update coordinate of feature in openlayers 3

I have a model in javascript which has latitude and longitude value. 我在javascript中有一个具有纬度和经度值的模型。 I have to find a feature on the map by the ID of the element and update it's location and several other properties. 我必须通过元素的ID在地图上找到一个特征,并更新它的位置和其他几个属性。 My code looks like this: 我的代码看起来像这样:

function updateCoordinate(item) {
    var features = source.getFeatures();
    var featureToUpdate;

    // find feature by custom property
    for(var i=0; i< features.length; i++) {
       if (features[i].get('ID') == item.ID) {
           featureToUpdate = features[i];
           break;
       }
    } 

    // get lon, lat from input item
    var lon  = item.Coordinate.Longitude;
    var lat  = item.Coordinate.Latitude;

    // update geometry (not working)
    featureToUpdate.set('Geometry', new ol.geom.Point(getPointFromLongLat(lon, lat)));
    // update custom properties (working)
    featureToUpdate.set('MapMarkerTitle', item.Title);
    // ...
}

function getPointFromLongLat (long, lat) {
    return ol.proj.transform([long, lat], 'EPSG:4326', 'EPSG:3857')
}

Am I doing something wrong? 难道我做错了什么? Is there a better way for this? 有更好的方法吗? Is there a better way to find feature by custom property? 是否有更好的方法来查找自定义属性的功能?

By custom poperty I mean that the feature is getting initiated like this: 通过自定义poperty我的意思是该功能正在启动如下:

var fea = new ol.Feature({
            geometry: new ol.geom.Point(getPointFromLongLat(lon, lat)),
            MapMarkerTitle : 'AAA',
            // ...
         }) 
source.addFeatures([fea]);

The custom properties are getting updated but the coordinate doesn't seem to update. 自定义属性正在更新,但坐标似乎没有更新。 Will the feature be redrawn after updating position? 更新位置后是否会重新绘制该功能? The label is however redrawn so I think yes. 然而,标签重新绘制,所以我认为是的。

UPDATE UPDATE

After some debugging I found out that, I mispelled the 'geometry' property with uppercase. 经过一些调试后我发现,我用大写错误拼写了'geometry'属性。 Actually: 其实:

featureToUpdate.set('geometry', new ol.geom.Point(getPointFromLongLat(lon, lat)));

does set the new position and update the location right away. 设置新位置并立即更新位置。 I would still like to know if what I am doing is the good way or there is better. 我仍然想知道我所做的是好方法还是更好。 Thanks! 谢谢!

You can simplify it to: 您可以将其简化为:

function updateCoordinate(item) {
    var featureToUpdate = source.getFeatureById(item.ID);

    var coord = getPointFromLongLat(item.Coordinate.Longitude, item.Coordinate.Latitude);

    featureToUpdate.getGeometry().setCoordinates(coord);
    featureToUpdate.set('MapMarkerTitle', item.Title);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM