Annotation.setLatitude()

You must Login before you can answer or comment on any questions.

Hi, in a map I try to animate annotations:

var updateVessels = function() {
        clearTimeout(cron);
        getVessels(function(_vessels) {
            for (var v = 0; v < _vessels.length; v++) {
                var vessel = _vessels[v];
                for ( a = 0; a < annotations.length; a++) {
                    if (vessel.pk == annotations[a].pk) {
                        annotations[a].setLatitude(parseFloat(vessel.lat,10));
                        annotations[a].setLongitude(parseFloat(vessel.lon,10));
                        Ti.API.log('Moving of ' + vessel.name)
                        break;
                    }
                }
            }
            cron = setTimeout(updateVessels, 5000);
        });
    }
Every ca. 5 sec. the function will called, but the annotations are fixed. Whats going wrong. Everytime comes other coordinates from server …

Rainer

3 Answers

Try building a new list of annotations and calling removeAllAnnotations() and then addAnnotations() with your new list. It might be fast enough where the user doesn't see them disappear.

— answered 9 months ago by Jason Priebe
answer permalink
5 Comments
  • If you don't mind me asking, what are you building? It sounds interesting...

    — commented 9 months ago by Jason Priebe

  • I think your solution is a rubber solution. It works – why not? But the annotations are flashing; is ugly …

    — commented 9 months ago by Rainer Schleevoigt

  • Why exists this method, if setLatitude() doesnt work really??

    — commented 9 months ago by Rainer Schleevoigt

  • Show 2 more comments

Looking over your code gave me an inspiration. In your original code, you called setLatitude() and setLongitude(). In your revised code, you're accessing the latitude and longitude properties directly. I have seen cases in Titanium where the setter function doesn't work, but the direct property access does work.

What if you try your new code without the removeAnnotation() and addAnnotation() calls?

if (vessel.pk == annotations[a].pk) {
                    if (!vessel.is_moored && vessel.lat != annotation.latitude && vessel.lon != annotation.longitude) {
                        annotation.latitude = vessel.lat;
                        annotation.longitude = vessel.lon;
                        if (vessel.name == 'HARMONIE')
                            Ti.API.log('Moving of ' + vessel.name + ' ' + vessel.lat +',' + vessel.lon)
                        break;
                    }
                }
Maybe this will work without flashing?

— answered 9 months ago by Jason Priebe
answer permalink
1 Comment
  • The vessels leaves fixed. Here the new code which moves the vessels every 5 sec. in relation to bearing and speed:

    var moveVessels = function() {
            var R = 6371000; // km
            for (var a = 0; a < annotations.length; a++) {
                var annotation = annotations[a];
                if (annotation.is_moored)
                    continue;
                var lat1 = annotation.latitude;
                var lon1 = annotation.longitude;
                var d = parseFloat(annotation.speedoverground) * 0.44 / R *5;
                var brng = parseFloat(annotation.orientation);
                var lat2 = Math.asin(Math.sin(lat1) * Math.cos(d) + Math.cos(lat1) * Math.sin(d) * Math.cos(brng));
                var lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(d) * Math.cos(lat1), Math.cos(d) - Math.sin(lat1) * Math.sin(lat2));
                Ti.API.log(lat2 + ',' + lon2);
                //  annotation.setLatitude(lat2);
                map.removeAnnotation(annotation);
                annotation.setLongitude(lon2);
                annotation.longitude = lon2;
                map.addAnnotation(annotation);
            }
        };
    In main thread I call setInterval(moveVessels,5000);. The annotations has properties orientation(bearing) and speedoverground.

    — commented 9 months ago by Rainer Schleevoigt

Here my current solution:

var updateVessels = function() {
    clearTimeout(cron);
    getVessels(function(_vessels) {
        for (var v = 0; v < _vessels.length; v++) {
            var vessel = _vessels[v];
            for ( a = 0; a < annotations.length; a++) {
                var annotation = annotations[a];
                if (vessel.pk == annotations[a].pk) {
                    if (!vessel.is_moored && vessel.lat != annotation.latitude && vessel.lon != annotation.longitude) {
                        map.removeAnnotation(annotation);
                        annotation.latitude = vessel.lat;
                        annotation.longitude = vessel.lon;
                        map.addAnnotation(annotation);
                        if (vessel.name == 'HARMONIE')
                            Ti.API.log('Moving of ' + vessel.name + ' ' + vessel.lat +',' + vessel.lon)
                        break;
                    }
                }
            }
        }
        cron = setTimeout(updateVessels, 10000);
    });
}
The annotation is flashing, but not moving. ;-((

Above the log – you can see the position of HARMONIE is changing: ~~~ ~~~

Your Answer

Think you can help? Login to answer this question!