Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

addAnnotations help

I asked this question yesterday (http://developer.appcelerator.com/question/76201/multiple-annotations-via-json) and marked it as the correct answer, since it was. But I am still unable to resolve my issue.

I was using setAnnotations to display one set of annotations pulled from my JSON results, and it was reccomended that I use addAnnotations instead. When using addAnnotations, it wont plot any points.

I'm thinking this is just me lacking a full understanding of the API, and the docs really didn't help me too much.

So to expand a bit on my issue, Im pulling in a JSON response which has 2 sets of results. Each set of results has a different set of coordinates that I plan to plot. As mentioned earlier, simply using setAnnotations will work for one set of coordinates..but anything beyond that will not work.

Does anyone have any pointers or examples of using multiple annotations in the mapview? I've been pulling the little hair that I have left out over here!

— asked November 4th 2010 by Chris Nelson
  • annotations
  • mapview
0 Comments

3 Answers

  • Accepted Answer

    Here's a stripped down version of what I've used. It pulls from a DB rather than the network. But, the principles still apply.

    var mapview = Titanium.Map.createView({
      mapType: Titanium.Map.STANDARD_TYPE,
      animate:true,
      regionFit:true,
      userLocation:true
    });
    
    // my custom func for retreiving data from the DB
    var sales = db.getundelivered(userid);
    
    var myAnnotations = new Array();
    for(s in sales) {
      // should filter out prototype properties with for/in
      var dbMarker = Titanium.Map.createAnnotation({
        animate:true,
        pincolor:Titanium.Map.ANNOTATION_RED,
        latitude:parseFloat(sales[s]['lat']),
        longitude:parseFloat(sales[s]['longitude']),
        myid:sales[s]['id']
      });
      myAnnotations.push(dbMarker);
    }
    mapview.annotations = myAnnotations;
    
    — answered November 4th 2010 by Tim Poulsen
    permalink
    0 Comments
  • instead of points[c] try points.push( the_annotation)

    — answered November 4th 2010 by Dan Tamas
    permalink
    1 Comment
    • So something like this?

      var points = [];
      for (var c=0; c<data.results.length; c++) {
      
        points.push(annotation1) = Titanium.Map.createAnnotation({
          latitude:data.results[c].latitude,
          longitude:data.results[c].longitude,
          title:data.results[c].username, 
          subtitle:data.results[c].client,
          pincolor:Titanium.Map.ANNOTATION_RED,
          rightButton: 'phone.png',
          leftButton: 'mail.png',
          url:data.results[c].phone,
          animate:true,
          myid:c+1
        });
      
      }
      mapview.addAnnotations(annotation1);
      

      Because I just tried that and got annotation1 is not defined.

      — commented November 4th 2010 by Chris Nelson
  • var annotation1 = Titanium.Map.createAnnotation({
    .....
    //blah blah
    });
    
    points.push(annotation1);
    
    — answered November 4th 2010 by Dan Tamas
    permalink
    2 Comments
    • which is basically what my code is doing, though I called my annotation "dbMarker" and my array of annotations "myAnnotations" and it's all within a for/in loop

      — commented November 4th 2010 by Tim Poulsen
    • yes, of course :) but he made a mistake in the code :)

      — commented November 4th 2010 by Dan Tamas
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.