Titanium Community Questions & Answer Archive

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

How can I create a link on a map annotation? (mobile)

I have a map view, with several annotations, and I want my annotations to be able to redirect to something, any URL or a view.

I've tried to add an event listener on my annotation, to add a html link in the annotation subtitle, but nothing works.

Any ideas?

— asked March 16th 2010 by Gregoire Marchal
  • annotation
  • link
  • map
0 Comments

8 Answers

  • Add your custom attribute to the annotation

    
    var atlanta = Titanium.Map.createAnnotation({
        latitude:33.74511,
        longitude:-84.38993,
        title:"Atlanta, GA",
        subtitle:'Atlanta Braves Stadium',
        pincolor:Titanium.Map.ANNOTATION_PURPLE,
        animate:true,
        leftButton:'../images/atlanta.jpg',
        rightButton: Titanium.UI.iPhone.SystemButton.DISCLOSURE,
        url:'http://www.google.com' // CUSTOM ATTRIBUTE THAT IS PASSED INTO EVENT OBJECTS    
    });
    

    then the eventListener is added to the mapview, not directly to the annotation.

    
    mapview.addEventListener('click',function(evt)
    {
        // map event properties 
        var annotation = evt.annotation;
        var title = evt.title;
        var clickSource = evt.clicksource;
    
    
    
        Titanium.API.info('MAPVIEW EVENT - you clicked on '+title+' with click source = '+clickSource);
    
    if (clickSource == 'rightButton'){
    
    // get custom annotation attribute
        var url = evt.annotation.url;
    // code to open your url
    }
    
    });
    
    — answered March 16th 2010 by Nick Lloyd
    permalink
    2 Comments
    • Thank you! This helps a lot.

      — commented June 16th 2010 by Michael Gajda
    • Works perfectly! Thanks for posting this.

      — commented March 14th 2014 by Nick Duffield
  • I found Nick Lloyd's code quite helpful. Immediately after his comment

    // code to open your url
    

    I added this –

    var button = Titanium.UI.createButton({
            title:'Back',
            style:Titanium.UI.iPhone.SystemButtonStyle.DONE
        });
    
    button.addEventListener('click', function() {
            window.close();
        });
    
    var webview = Titanium.UI.createWebView({url:evt.annotation.url});
    var window = Titanium.UI.createWindow();
    window.add(webview);
    window.setLeftNavButton(button);
    window.open({modal:true});
    

    Now, when the rightButton is clicked, a webview pops up showing the URL evt.annotation.url (defined in the annotion as url:'http://somewebsite.com' as Nick demonstrated above). The webview includes a "Back" button that can be pressed/clicked to close the window containing the webview, bringing the user back to the mapview. Instead of all this, if you're not trying to keep the user focused on your app's content, a simple

    Titanium.Platform.openURL(evt.annotation.url);
    

    will do, opening up Safari and closing the app. Hope this helps!

    — answered July 19th 2010 by Steve Phillips
    permalink
    0 Comments
  • I also want the same thing. When I googled I found this link and I start trying on these solutions. Later I found an excellent solution in appcelerator documentation. The link is

    https://developer.appcelerator.com/apidoc/mobile/1.8.2/Titanium.Map-module
    
    The code is
    
    var mountainView = Titanium.Map.createAnnotation({
        latitude:37.390749,
        longitude:-122.081651,
        title:"Appcelerator Headquarters",
        subtitle:'Mountain View, CA',
        pincolor:Titanium.Map.ANNOTATION_RED,
        animate:true,
        leftButton: '../images/appcelerator_small.png',
        myid:1 // Custom property to uniquely identify this annotation.
    });
    
    var mapview = Titanium.Map.createView({
        mapType: Titanium.Map.STANDARD_TYPE,
        region: {latitude:33.74511, longitude:-84.38993, 
                latitudeDelta:0.01, longitudeDelta:0.01},
        animate:true,
        regionFit:true,
        userLocation:true,
        annotations:[mountainView]
    });
    
    win.add(mapview);
    // Handle click events on any annotations on this map.
    mapview.addEventListener('click', function(evt) {
    
        Ti.API.info("Annotation " + evt.title + " clicked, id: " + evt.annotation.myid);
    
        // Check for all of the possible names that clicksouce
        // can report for the left button/view.
        if (evt.clicksource == 'leftButton' || evt.clicksource == 'leftPane' ||
            evt.clicksource == 'leftView') {
            Ti.API.info("Annotation " + evt.title + ", left button clicked.");
        }
    });
    
    — answered January 10th 2013 by zeeshan pervez
    permalink
    0 Comments
  • Thank you for your idea. When I try it, I get this result:

    1. I click on the annotation marker: the event is not fired, but the annotation tooltip is displayed
    2. I click on the annotation tooltip: the event is fired, but annotation is undefined

    Here is my code

    mapview.addEventListener('click',function(evt) {
      var annotation = evt.annotation;
      Titanium.API.debug('annotation clicked');
      Titanium.API.debug(evt.annotation.latitude + ' ' + evt.annotation.longitude);
    });
    

    And the result:

    [DEBUG] [2063,155311] annotation clicked
    [TRACE] E/KrollCallback( 228): (kroll$2) [23,155334] ERROR: TypeError: Cannot read property "latitude" from undefined
    [TRACE] E/KrollCallback( 228): org.mozilla.javascript.EcmaError: TypeError: Cannot read property "latitude" from undefined
    
    — answered March 16th 2010 by Gregoire Marchal
    permalink
    1 Comment
    • Try this

      
      mapview.addEventListener('click',function(evt) {
          if (evt.annotation){
                var annotation = evt.annotation;
                Titanium.API.debug('annotation clicked');
                Titanium.API.debug(evt.annotation.latitude + '' + evt.annotation.longitude);
          }
      });
      

      — commented June 30th 2011 by Mel Maxwell
  • To be honest I'm not sure you can tie the eventListener to the 'pin' itself.

    What happens if you try adding the latitude & longitude as addition custom attributes? (with diff names)

    — answered March 16th 2010 by Nick Lloyd
    permalink
    0 Comments
  • Same result… I can't retrieve the annotation object. It is undefined.

    — answered March 16th 2010 by Gregoire Marchal
    permalink
    0 Comments
  • Adding a URL on a subtitle will not work, i've also expirimented with that. What does work is creating a left/right button and redirect from a URL from there.

    — answered March 25th 2010 by Glenn Tillemans
    permalink
    0 Comments
  • Glenn Tillemans > how do you add a button in an annotation? I've tried this, that doesn't work…

    var my_button = Titanium.UI.createButton({
      title: 'Click me!'
    });
    
    annotation = Titanium.Map.createAnnotation({
      latitude: 12.34,
      longitude: 12.34,
      // ...
      rightButton: my_button
    });
    
    — answered March 25th 2010 by Gregoire Marchal
    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.