4

Can anyone possibly give me an example of how the Openlayers 3 'setGeometry' method can be used on an ol.Point feature please? I have a feature which has been created from data retrieved from a database and this effectively shows waypoints along a linestring route. When the route is modified I'm using the 'modifyend' event to fire a function which will update the underlying data. The last thing I need to do is find the corresponding marker within an ol.Collection and update it's coordinate property. Looking at the API, it would seem that I should be able to do this with:

// results is an array with the old coordinate and the new one
var originalCoordinate = results[0];
var modifiedCoordinate = results[1];

var sourceFeatures = session.markerLayer.getSource().getFeatures();

    sourceFeatures.forEach(displayFeature);

function displayFeature(item, index) {
    var featureGuid = item.get('Guid');
    console.info(featureGuid, index);

    //markerFC.push(marker);
    if (featureGuid == pointGuid) {
        var coords = item.getGeometry().getCoordinates();
        item.Longitude = nc4326[0];
        item.Latitude = nc4326[1];
        item.setGeometry(modifiedCoordinate);
        console.info(item, coords, index);
    }
}

item.setGeometry(modifiedCoordinate);

But it hasn't changing anything and I get an error in the console saying 'Uncaught Error: addEventListener and attachEvent are unavailable.'

I really don't understand why this is so complicated to do and the only other thing I can think of to do is destroy the feature and create a new one in it's place but that seems a little extreme when I'm only trying to update it's lat/lon properties.

Can anyone please advise how I can handle this?

So, to update this question, the item's geometry does seem to be updating when I set it here but the layer won't refresh and the item appears in it's original position. I've also tried the solution in this question but it's not working.

  • Could do with some more of your code to know why its not working for. There are examples of the use of setGeometry in the openlayers docs openlayers.org/en/v3.12.0/examples/geolocation.html – user5219763 Apr 14 '16 at 13:47
  • Sorry @user5219763 I realised that as I'd posted the question. I've incorporated the example functionality into my geolocation tracking but it doesn't work here. – dvmac01 Apr 14 '16 at 13:52
5

So apparently setGeometry does not work in the way which I expected. Rather than using:

item.setGeometry(modifiedCoordinate);

It is necessary to use:

item.getGeometry().setCoordinates(modifiedCoordinate);

This seems slightly counter-intuitive to me but it soles the problem at least. One issue that this has also raised is finding a reliable method for refreshing a layer's content but I guess that will be another question!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.