Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-14761] Android: Ti.Map is unable to access getAnnotations() method

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2013-09-04T21:36:23.000+0000
Affected Version/sn/a
Fix Version/s2013 Sprint 18, 2013 Sprint 18 API, Release 3.2.0
ComponentsAndroid
Labelsandroid, mapv2, parity
ReporterChris Golding
AssigneeHieu Pham
Created2013-08-01T23:53:57.000+0000
Updated2013-11-14T09:46:06.000+0000

Description

Problem

When I attempt to access annotations on a map view created by Ti.Map in Alloy I find that the annotations are undefined, however they are displayed on the map. I do not have this issue when creating the map using the traditional method. I need to be able to get all the current annotations and loop through them, I have tried using the getAnnotations() method but no results are returned. This issue does not exist with the map on iOS. Below is the code to reproduce this issue.

alloy.js

Alloy.Globals.Map = require('ti.map');

index.xml

<Alloy>
    <Window>
        <!-- Use the Alloy.Globals.Map namespace to create a map module view -->
        <View id="mapview" ns="Alloy.Globals.Map" onClick="report" />
    </Window>
</Alloy>

index.js

function report(evt) {
    Ti.API.info("Annotation " + evt.title + " clicked, id: " + evt.annotation.myid);
}

// API calls to the map module need to use the Alloy.Globals.Map reference
var mountainView = Alloy.Globals.Map.createAnnotation({
    latitude:37.390749,
    longitude:-122.081651,
    title:"Appcelerator Headquarters",
    subtitle:'Mountain View, CA',
    pincolor:Alloy.Globals.Map.ANNOTATION_RED,
    myid:1 // Custom property to uniquely identify this annotation.
});

$.mapview.region = {latitude:33.74511, longitude:-84.38993,
                    latitudeDelta:0.01, longitudeDelta:0.01};
$.mapview.addAnnotations([mountainView]);

alert($.mapview.getAnnotations().length);

Comments

  1. Eduardo Gomez 2013-08-05

    Alloy app

    Surprisingly I couldn't use the Alloy global namespace (got a module not found error). Then reproduced the getAnnotations() issue on TiSDK 3.1.1.GA using controller shown below:
       <Alloy>
           <Window>
               <!-- Use the Alloy.Globals.Map namespace to create a map module view -->
               <View id="mapview" ns="Ti.Map" onClick="report" />
           </Window>
       </Alloy>
       
    iOS reports "1" meanwhile Android returns "0".

    Titanium app

    On TiSDK 3.1.1.GA on iOS Simulator v6.1.2 returns "1" annotations, meanwhile Android just returns "0" annotations. If same method is tested by a Titanium simple sample length property is undefined.

    Device stack trace (Razr 2.3.5)

       08-05 15:22:19.579: E/TiExceptionHandler(12312): (main) [122,268] ----- Titanium Javascript Runtime Error -----
       08-05 15:22:19.579: E/TiExceptionHandler(12312): (main) [0,268] - In app.js:20,31
       08-05 15:22:19.579: E/TiExceptionHandler(12312): (main) [1,269] - Message: Uncaught TypeError: Cannot read property 'length' of undefined
       08-05 15:22:19.579: E/TiExceptionHandler(12312): (main) [0,269] - Source: alert(mapview.getAnnotations().length);
       

    Simple sample

       var win1 = Ti.UI.createWindow();
       
       var mapview = Ti.Map.createView();
       
       // API calls to the map module need to use the Alloy.Globals.Map reference
       var mountainView = Ti.Map.createAnnotation({
       	latitude : 37.390749,
       	longitude : -122.081651,
       	title : "Appcelerator Headquarters",
       	subtitle : 'Mountain View, CA',
       	pincolor : Ti.Map.ANNOTATION_RED,
       	myid : 1 // Custom property to uniquely identify this annotation.
       });
       
       mapview.region = {
       	latitude : 33.74511,
       	longitude : -84.38993,
       	latitudeDelta : 0.01,
       	longitudeDelta : 0.01
       };
       
       mapview.addAnnotations([mountainView]);
       
       alert(mapview.getAnnotations().length);
       win1.add(mapview);
       
       win1.open(); 
       
  2. Chris Golding 2013-08-05

    I did find a method to get the annotations on Android. However you have to create the annotations prior to creating the view, and any annotations added after creating the view are not returned by getAnnotations(). This doesn't solve my issue since I am creating and deleting annotations on the fly.
       var MapModule = require('ti.map');
        
       var win = Ti.UI.createWindow({fullscreen: false, layout: "vertical"});
       
        
       var anno = MapModule.createAnnotation({
           latitude : -33.87365,
           longitude : 151.20689,
           title : "Drag Me",
           subtitle : "Sydney is quite chill",
           draggable : true
       });
       var anno2 = MapModule.createAnnotation({
           latitude : -33.86365,
           image : "applogo.png",
           longitude : 151.21689,
           title : "Drag Me 2",
           subtitle : "This is anno2",
           draggable : true
       });
       var anno3 = MapModule.createAnnotation({
           latitude : -33.85365,
           pincolor : MapModule.ANNOTATION_BLUE,
           longitude : 151.20689,
           title : "anno3",
           subtitle : "This is anno3",
           draggable : false
       });
        
       var map = MapModule.createView({
           enableZoomControls: false,
           mapType: MapModule.NORMAL_TYPE,
           animate: true,
           annotations: [anno, anno2, anno3],
           region: {latitude: -33.87365, longitude: 151.20689, latitudeDelta: 0.1, longitudeDelta: 0.1 } //Sydney
       });
        
       var anno4 = MapModule.createAnnotation({
           latitude : -34.85365,
           pincolor : MapModule.ANNOTATION_BLUE,
           longitude : 152.20689,
           title : "anno4",
           subtitle : "This is anno4",
           draggable : false
       });
       
       map.addAnnotation(anno4);
       
       win.add(map);
       win.open();
       
       alert(map.getAnnotations().length);
       
  3. Hieu Pham 2013-08-23

    master PR: https://github.com/appcelerator-modules/ti.map/pull/3
  4. Hieu Pham 2013-08-24

    Testing steps: 1. Run the classic titanium test case mentioned in the above comment. Should see an alert showing '4'. 2. Run app.js from example/app.js inside the map module. Play around with 'Annotation Test' suite and make sure there are no regressions. 3. Run this additional test case
       var MapModule = require('ti.map');
         
       var win = Ti.UI.createWindow({fullscreen: false, layout: "vertical"});
        
         
       var anno = MapModule.createAnnotation({
           latitude : -33.87365,
           longitude : 151.20689,
           title : "anno",
           subtitle : "Sydney is quite chill",
           draggable : true
       });
       var anno2 = MapModule.createAnnotation({
           latitude : -33.86365,
           image : "applogo.png",
           longitude : 151.21689,
           title : "anno2",
           subtitle : "This is anno2",
           draggable : true
       });
       var anno3 = MapModule.createAnnotation({
           latitude : -33.85365,
           pincolor : MapModule.ANNOTATION_BLUE,
           longitude : 151.20689,
           title : "anno3",
           subtitle : "This is anno3",
           draggable : false
       });
         
       var map = MapModule.createView({
           enableZoomControls: false,
           mapType: MapModule.NORMAL_TYPE,
           animate: true,
           //annotations: [anno, anno2, anno3],
           region: {latitude: -33.87365, longitude: 151.20689, latitudeDelta: 0.1, longitudeDelta: 0.1 } //Sydney
       });
         
       var anno4 = MapModule.createAnnotation({
           latitude : -34.85365,
           pincolor : MapModule.ANNOTATION_BLUE,
           longitude : 152.20689,
           title : "anno4",
           subtitle : "This is anno4",
           draggable : false
       });
       
       win.add(map);
       win.open();
       
       
       setTimeout (function() {
       
       map.addAnnotations([anno, anno2, anno4]);
       map.removeAllAnnotations();
       map.removeAnnotations(["anno4"]);
       map.removeAnnotation(anno);
       map.addAnnotations([anno, anno2]);
       
       Ti.API.info(JSON.stringify(map.getAnnotations()));
       alert((map.getAnnotations().length));
       }, 1000);
       
    You should see an alert printing '2', and logcat should have the content of those 2 annotations. 4. Change line 61 in the above code to 100 (instead of 1000), also try to run the block without setTimeout. The result should be the same.
  5. Priya Agarwal 2013-11-14

    Verified with test environment: Appcelerator Studio: 3.2.0.201311122338 SDK:3.2.0.v20131113183932 alloy: 1.3.0 acs: 1.0.7 npm: 1.3.2 titanium: 3.2.0 titanium-code-processor: 1.1.0 Xcode:5.0.2 Device: Google Nexus7(v4.3) OS: Mac OSX 10.9 getAnnotation() working fine.

JSON Source