Search code examples
databasedictionaryannotationstitaniumappcelerator

Annotations from database to Ti.map Appcelerator


I'm trying to create an array which pushes values from a local database into an annotation, in Titanium. The annotation doesn't respond to the value of my database. I think I'm doing a school boy error somewhere but I have been staring myself blind for a while now. Can anyone help me? Much appreciated! Cheers.

function localdb(){

var db = Ti.Database.install('/my_db/annotations.sqlite', 'Annos');
var row = db.execute('select title, latitude, longitude, type from annotations ');
places = [];

while (row.isValidRow()){
       var annotation = Titanium.Map.createAnnotation({
               latitude:row.fieldByName('latitude'),
               longitude:row.fieldByName('longitude'),
               title:row.fieldByName('title'),
               subtitle:row.fieldByName('type'),
               animate:true,
               pincolor: Titanium.Map.ANNOTATION_GREEN
    });

       places.push(annotation);
       mapview.addAnnotation(annotation);
       row.next();

}

mapview.annotations = places;
db.close();

}

   var mapview = Titanium.Map.createView({
   mapType: Titanium.Map.STANDARD_TYPE,
   height: '100%',
   animate:true,
   regionFit:true,
   userLocation:true,

});

localdb();

win.add(mapview);


Solution

  • You'll want to use the addAnnotations method passing the array of annotations you created http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.Map.View-method-addAnnotations

    var places = [];
    
    while (row.isValidRow()){
       var annotation = Titanium.Map.createAnnotation({
               latitude:row.fieldByName('latitude'),
               longitude:row.fieldByName('longitude'),
               title:row.fieldByName('title'),
               subtitle:row.fieldByName('type'),
               animate:true,
               pincolor: Titanium.Map.ANNOTATION_GREEN
    });
       places.push(annotation);
       row.next();
    }
    // optional mapview.removeAllAnnotations();
    mapview.addAnnotations(places);