Map Annotations have a problem!

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

Hi! I am working on an app that uses map annotations and I found a problem. I added a variable that increases each time the annotation.addEventListener fires. Normally one would think that each time you click on one annotation the variable increases one time. Instead it seems to 'remember' the number of times you have already clicked on one annotation: when you click on the second annotation it enters on the method annotation.addEventListener twice, when you click a third time it enters 3 time and so on.. Is there a way to deceive this problem?

— asked 9 months ago by Cristina Nardin
1 Comment
  • You may need to post your code with this particular problem. By default, annotations do not keep track of click counts and the event should only be fired once per click. So in order to get the help you need you'll need to post a very minimal code example that reproduces the issue that you're encountering.

    — commented 9 months ago by Anthony Decena

2 Answers

Here is the code. For each annotation I have to add 5 pictures taken from different directories called PhotoPotholes 1, PhotoPotholes 2 and so on. So I read the number of annotation that I am tapping and then I take the pictures from the relative directory. I use a scrollable view to show them. This part of the code is inserted in a getCurrentPosition method that fires every 2 seconds. But in the annotation I am not using the latitude and longitude found every 2 seconds, they are the position of a pothole calculated previously with a formula.

var annotation_array = []; 
var annotation = Titanium.Map.createAnnotation
({
    latitude: latp,
    longitude: longp,
    title: 'Pothole ' + imgPothole,
    animate: true,
    pincolor: Titanium.Map.ANNOTATION_RED,
    myid: imgPothole
});
annotation_array.push(annotation);
annotation.addEventListener('click', function(e)
{
    nrclick++;
    alert(nrclick);
    win2.add(scrollingView);
    var nr = annotation.myid;
    photoName = 'PhotoPothole' + nr;
    photoDir = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,photoName);
    data_new = photoDir.getDirectoryListing();
 
    for(j = 0, k = data_new.length; j < k; j++)
    {
        var img = Titanium.Filesystem.getFile(photoDir.resolve(), data_new[j]);
        var imgView = Titanium.UI.createImageView
        ({
                top: 0,
                left: 0,
                width: 286,
                height: 337
        });
        imgView.image = img;
        scrollingView.addView(imgView);
    }
});
mapview.addAnnotations(annotation_array);

Hi,

nrclick seems to be a global variable. Add var to it within the call back function and you will be fine.

Cheers, Christoph

Your Answer

Think you can help? Login to answer this question!