Titanium Community Questions & Answer Archive

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

Help with Map / Geo

I'm trying to add my home to a map view but an annotation is never added. Can someone please help tell me how I'm messing up?

Thanks.

var addr = "123 SW 3th CT Miami Florida 33100";

var lat = "";
var lon = "";

Titanium.Geolocation.forwardGeocoder(addr,function(evt)
{
    lat = evt.latitude;
    Ti.API.info('latitude: '+evt.latitude);
    lon = evt.longitude;
    Ti.API.info('longitude: '+evt.longitude);
});

var mountainView = Titanium.Map.createAnnotation({
    latitude:lat,
    longitude:lon,
    title:"My House",
    subtitle:'Miami, Florida',
    pincolor:Titanium.Map.ANNOTATION_RED,
    animate:true,
    leftButton: '../images/appcelerator_small.png',
    myid:1 // CUSTOM ATTRIBUTE THAT IS PASSED INTO EVENT OBJECTS
});

var mapview = Titanium.Map.createView({
    mapType: Titanium.Map.STANDARD_TYPE,
    region: {latitude:lat, longitude:lon, 
            latitudeDelta:0.01, longitudeDelta:0.01},
    animate:true,
    regionFit:true,
    userLocation:true,
    annotations:[mountainView]
});


Titanium.UI.currentWindow.add(mapview);
— asked November 11th 2010 by Irving Carrion
  • annotations
  • creatview
  • map
  • mapview
1 Comment
  • When you create a view and annotation the lat/lon vars are still undefined. Remember that forwardGeocoder is asynchronous, so next line of code executes before assigning the values of lat/lon. Here's the working code:

    var win = Titanium.UI.currentWindow;

    var isAndroid = false;
    if (Titanium.Platform.name == 'android') {
    isAndroid = true;
    }

    var addr = "123 SW 3th CT Miami Florida 33100";

    var lat = "";
    var lon = "";

    Titanium.Geolocation.forwardGeocoder(addr,function(evt)
    {
    lat = evt.latitude;
    Ti.API.info('latitude: '+evt.latitude);
    lon = evt.longitude;
    Ti.API.info('longitude: '+evt.longitude);

    var mountainView = Titanium.Map.createAnnotation({
        latitude:lat,
        longitude:lon,
        title:"My House",
        subtitle:'Miami, Florida',
        pincolor:Titanium.Map.ANNOTATION_RED,
        animate:true,
        leftButton: '../images/appcelerator_small.png',
        myid:1 // CUSTOM ATTRIBUTE THAT IS PASSED INTO EVENT OBJECTS
    });
    
    var mapview = Titanium.Map.createView({
        mapType: Titanium.Map.STANDARD_TYPE,
        region: {latitude:lat, longitude:lon, 
                latitudeDelta:0.01, longitudeDelta:0.01},
        animate:true,
        regionFit:true,
        userLocation:true,
        annotations:[mountainView]
    });
    
    
    win.add(mapview);
    

    });

    — commented January 5th 2011 by Oleg Polyakov

1 Answer

  • When you create a view and annotation the lat/lon vars are still undefined. Remember that forwardGeocoder is asynchronous, so next line of code executes before assigning the values of lat/lon. Here's the working code:

    var win = Titanium.UI.currentWindow;
    var isAndroid = false; if (Titanium.Platform.name == 'android') { isAndroid = true; }
    var addr = "123 SW 3th CT Miami Florida 33100";
    var lat = ""; var lon = "";
    Titanium.Geolocation.forwardGeocoder(addr,function(evt) { lat = evt.latitude; Ti.API.info('latitude: '+evt.latitude); lon = evt.longitude; Ti.API.info('longitude: '+evt.longitude);
    var mountainView = Titanium.Map.createAnnotation({
        latitude:lat,
        longitude:lon,
        title:"My House",
        subtitle:'Miami, Florida',
        pincolor:Titanium.Map.ANNOTATION_RED,
        animate:true,
        leftButton: '../images/appcelerator_small.png',
        myid:1 // CUSTOM ATTRIBUTE THAT IS PASSED INTO EVENT OBJECTS
    });
    
    var mapview = Titanium.Map.createView({
        mapType: Titanium.Map.STANDARD_TYPE,
        region: {latitude:lat, longitude:lon, 
                latitudeDelta:0.01, longitudeDelta:0.01},
        animate:true,
        regionFit:true,
        userLocation:true,
        annotations:[mountainView]
    });
    
    
    win.add(mapview);
    });
    
    — answered January 5th 2011 by Oleg Polyakov
    permalink
    0 Comments
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.