Titanium Community Questions & Answer Archive

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

Can Map Annotations display remote images?

The pins in my map display images correctly if they are local, but if I try to use a remote image (e.g. to replace the pin) then they don't load. Is it possible to load remote images by specifying a URL?

Thanks

This is the code that worked best for me, thanks Tamas for pointing in the right direction. (I can't get it to format to code view though so if anyone else can repost this below as a comment that would be good):

var image = Ti.UI.createImageView({
image: imageUrl,
width: 30,
height: 30,
backgroundColor: '#000',
preventDefaultImage: true
});

var annotation = Ti.Map.createAnnotation({
latitude: latitude,
longitude: longitude,
title: 'Title',
subtitle: 'Subtitle',
leftView: image
});

— asked September 18th 2010 by Charles Davison
  • annotation
  • map
0 Comments

4 Answers

  • No, an annotation cannot load a remote image. I have some code that I have written (very ugly) but it does allow for remote loading of an image into an annotation. I can post it here if you still need something like this.

    — answered September 25th 2010 by John McKnight
    permalink
    1 Comment
    • If anyone has some code that works to pull an imageView into the rightButton or leftButton of an annotation, I'd very grateful.

      What I'm trying to do is put a text variable (a location name) onto an image and then use that resultant image as the only thing in the annotation. Users would click on that to navigate to a location specific page.

      The reason I don't want to use the title or subtitle (along side a clickable button) is that on Android, the click event doesn't work when you click on the background of the annotation: The user doesn't navigate to another page.

      — commented April 1st 2011 by Michael Chaplin
  • This is how I load remote images into annotations. The short version is the image is loaded to the local filesystem and there is a temp cache that holds the image. I used this to show Twitter followers on a map so it has extra code to resize the image. The first block of code is responsible for fetching an image and then sending the image to the caller. The second block is the part that makes the request. Make a folder named 'iconcache' in the resources folder or the code won't work as expected.

    I'm relatively new to be Javascript and Titanium so there may be easier/better ways to do this.

    // The first three parameters are mandatory
    function fetch_remote_image(url, tmpPath, callback, height, width, defaultImage, timeout) {
      var imagePath = tmpPath + Ti.Utils.md5HexDigest(url) + '.jpg';
      var imagefile = Ti.Filesystem.getFile(imagePath);      
    
      if (imagefile.exists()) {
        callback(imagePath);
      }
    
      defaultImage = defaultImage || 'images/dummy_profile50x50.png';
      timeout = timeout || 30000;
      height = height || 64;
      width = width || 64;
    
      var xhr = Ti.Network.createHTTPClient();
      xhr.setTimeout(timeout);
    
      xhr.onerror = function(e) { 
        callback(defaultImage);
      };
    
      xhr.onload = function(){
        var remoteimage = this.responseData;
    
        if (!remoteimage) {
          imagePath = defaultImage;
        } else {
          var imageView = Ti.UI.createImageView({
            image: remoteimage,
            width: width,
            height: height
          });
    
          var tmpImage = imageView.toImage();
          var imagefile = Ti.Filesystem.getFile(imagePath);      
          imagefile.write(tmpImage);
        }
    
        callback(imagePath);
      };
    
      xhr.open('GET', url);
      xhr.send();
    }
    
    function addFriendPushPin(lat, lng, title, subtitle, image, id) {
      var friendAnnotation;
    
      fetch_remote_image(image, 'iconcache/', function(remoteImage) {
        var imagefile = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, remoteImage);    
        if (imagefile.exists()) {          
          friendAnnotation = Ti.Map.createAnnotation({
            latitude: lat,
            longitude: lng,
            title: title,
            subtitle: subtitle,
            image: remoteImage,
            animate:true,
            leftButton: 'appicon.png',
            myid: id
          });  
    
          mapview.addAnnotation(friendAnnotation);  
        }    
      }, 50, 50);
    }
    
    — answered September 25th 2010 by John McKnight
    permalink
    0 Comments
  • I had ended up loading a WebView into the annotation, and loading the image URL into that. It worked well enough but I think this approach may be better for the ability to resize etc. Though not sure of the network effects - if using a webview I assume that the image doesn't load unless the view is displayed (could be wrong though).

    Does anyone know what the lifetime of a file made with Ti.Filesystem.createTempFile() is?

    — answered September 27th 2010 by Charles Davison
    permalink
    0 Comments
  • A webview for a simple image? Webviews should be banned :)

    Try something like this:

    
    var big_img = Titanium.UI.createImage{
        image:'url_to_remote_image',
        left:-1000,
        top:-1000,
        width:160,
        height:160,
        backgroundColor:'#000'
    });
    
    var dest_img = Titanium.UI.createImage({
    
        image:big_img.toImage(),
        width:30,
        height:30
        });
    
    var dest = Titanium.Map.createAnnotation({
        latitude:the_venue.latitude,
            longitude:the_venue.longitude,
        pincolor:Titanium.Map.ANNOTATION_RED,
        title:the_venue.name,
        subtitle:the_venue.name+' '+the_venue.city+','+the_venue.state,
        leftView: dest_img
        // rightButton: Titanium.UI.iPhone.SystemButton.DISCLOSURE
    });
    
    — answered September 27th 2010 by Dan Tamas
    permalink
    2 Comments
    • You're right, a webview was overkill for this - but the above can be simplified; putting an Ti.UI.ImageView directly into the annotation worked for me:

      var image = Ti.UI.createImageView({
      image: remoteUrl,
      width: 30,
      height: 30,
      backgroundColor: '#000',
      preventDefaultImage: true
      });

      var annotation = Ti.Map.createAnnotation({
      latitude: lat,
      longitude: long,
      title: 'Place name',
      subtitle: 'Subtitle',
      leftView: image
      });

      — commented September 27th 2010 by Charles Davison
    • This seems to work only if the image is "cached" by the device before… :(

      — commented August 29th 2012 by Raul Riera
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.