Adding markers from url with Picasso

I’m adding markers to my map from a url using the Picasso library

As a marker isn’t an ImageView I tried to use a Target instead

  • What is meaning of negative dbm in signal strength?
  • android audio distortion on square waveform
  • Custom font for webview
  • Send Command to Bluetooth from Android device
  • how to use getListView() in Activity?
  • Detect android application crash force close
  • for(int x =0; x < mapIcon_url.length; x++){
    
        Picasso.with(getActivity()).load(mapIcon_url[x]).resize(marker_size, marker_size+15).into(new Target() {
    
            @Override
            public void onSuccess(Bitmap b) {
                bitmapMarker = BitmapDescriptorFactory.fromBitmap(b);
    
    
                //create marker option
                if(b != null)
                    markerOptions = new MarkerOptions().position(marker_position).icon(bitmapMarker));
                else
                    markerOptions = new MarkerOptions().position(marker_position).icon(BitmapDescriptorFactory.fromResource(R.drawable.placeholder_pin)).snippet(String.valueOf(x));
    
                marker = map.addMarker(markerOptions);                              
            }
    
            @Override
            public void onError() {
    
                //create marker option                                  
                markerOptions = new MarkerOptions().position(marker_position).icon(BitmapDescriptorFactory.fromResource(R.drawable.placeholder_pin)).snippet(String.valueOf(x));
                marker = map.addMarker(markerOptions);
    
            }
        }); 
    }   
    

    I’m doing this in a loop to add about 20 markers but I find that on first run of the code only 5 or 7 markers are added so I’ve switched to using the lib and an AsyncTask like this.

    for(int x =0; x < mapIcon_url.length; x++){
    
        new AddMarker().execute(mapIcon_url[x]);
    }
    
    
    public class AddMarker extends AsyncTask<String, Integer, BitmapDescriptor> {
    
        BitmapDescriptor bitmapMarker1;
        VenueDetails myVenue;
    
        @Override
        protected BitmapDescriptor doInBackground(String... url) {  
            myUrl = url[0];
            try {
                bitmapMarker1 = BitmapDescriptorFactory.fromBitmap(Picasso.with(getActivity()).load(myUrl).resize(marker_size, marker_size+15).get());
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return bitmapMarker1;
        }
    
        protected void onPostExecute(BitmapDescriptor icon) {
    
            try {
    
                map.addMarker(new MarkerOptions().position(marker_position).icon(icon)));
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }   
    

    However I’m a bit worried this method could give me some issues when I have alot of markers say about 100. My question would be is this the best way to do this and if not what other options can I try.

    Related posts:

    Retrieve a Fragment from a ViewPager
    How can I check my Android app&#039;s network consumption?
    Cost of raising an Intent in android
    open link of google play store in mobile version android
    How to center layout inside of Android Dialog?
    android BluetoothDevice.getName() return null
  • Soft keyboard does not show when Activity starts
  • Android: Behaviour of launchMode=“singleTask” not as described?
  • R.java file not generated automatically Eclipse
  • What's a good iScroll alternative that supports iOS, Android, WP8 and pull-to-refresh functionality?
  • Android SDK tools install in Docker fails
  • Retrofit 2 file down/upload
  • 2 Solutions collect form web for “Adding markers from url with Picasso”

    You have to keep a reference for each Target, otherwise the system
    automatically releases them when the garbage collector is invoked.

    So, the better solution is add each Target to a HashSet and then in
    onBitmapLoaded() and onBitmapFailed methods from Target, remove the
    Target itself from the set.

    Thank you for the suggestion, now my code work perfectly. Below the pieces of code that implement your suggestion.

    [...]//Global var
      private Set<PoiTarget> poiTargets = new HashSet<PoiTarget>();
    [...]    
     private void somewhere(){
        PoiTarget pt;
        for(Item item: data) {
            m = map.addMarker(new MarkerOptions()
                   .position(new LatLng(item.latitude, item.longitude))
                   .title(item.title));
            pt = new PoiTarget(m);
            poiTargets.add(pt);
            Picasso.with(context)
               .load(mapImageURLString)
               .into(pt);
        }
    }
    [...]
    //--------------------------------------------------------
    // Inner class
    //--------------------------------------------------------
        class PoiTarget implements Target{
            private Marker m;
    
            public PoiTarget(Marker m) { this.m = m; }
    
            @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                m.setIcon(BitmapDescriptorFactory.fromBitmap(bitmap));
                poiTargets.remove(this);
                Tools.msg(" @+ Set bitmap for "+m.getTitle()+" PT size: #"+poiTargets.size());
            }
    
            @Override public void onBitmapFailed(Drawable errorDrawable) {
                Tools.msg(" @+ [ERROR] Don't set bitmap for "+m.getTitle());
                poiTargets.remove(this);
            }
    
            @Override public void onPrepareLoad(Drawable placeHolderDrawable) {
    
            }
        }
    

    You have to keep a reference for each Target, otherwise the system automatically releases them when the garbage collector is invoked.

    So, the better solution is add each Target to a HashSet and then in onBitmapLoaded() and onBitmapFailed methods from Target, remove the Target itself from the set.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.