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.
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?
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!