1

Before asking this question, I reviewed many links

I'm parsing an xml from a web, and from this xml, get the longitude, latitude and name for a marker that I add to my map (v2) on android app

so far everything works perfectly.

mMap.addMarker(new MarkerOptions()
                .position(new LatLng(dLat, dLon)) // lat and lon from the xml of the web
                .title(NAME_OF_MARKER));

This xml also includes a field with the url of the small image to display the marker

<marker>
<lat>40.758895</lat>
<lon>-73.985131</lon>
<name>My Point</name>
<thumb>http://research.cbei.psu.edu/images/uploads/research-digest/general/1.CityIcon.png</thumb>
</marker>

I understand that the image of the xml should be loaded into a BitMap, and this BitMap put it as marker icon:

 mMap.addMarker(new MarkerOptions()
                .position(new LatLng(dLat, dLon))
                .title(NAME_OF_MARKER))
                .setIcon(BitmapDescriptorFactory.fromBitmap(bmp));

I am using an AsyncTask to load the url and assign it to a bitmap:

    Bitmap bmp;
    Canvas canvas1;

    //...
 public class markers extends AsyncTask<String, Void, String> {

     protected void onPreExecute() {

              Bitmap.Config conf = Bitmap.Config.ARGB_8888;
              bmp = Bitmap.createBitmap(80, 80, conf);

          }

          public String doInBackground(String... urls) {

                    try {
                        URL url = new URL(MY_THUMB); //url of the image parser from the xml ("http://research.cbei.psu.edu/images/uploads/research-digest/general/1.CityIcon.png")
                        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                        conn.setDoInput(true);
                        conn.connect();
                        InputStream is = conn.getInputStream();
                        bmp = BitmapFactory.decodeStream(is);
                    } catch (IOException e) {

                    }
        //...

... And before creating the marker on the map:

  canvas1 = new Canvas(bmp);

        Paint color = new Paint();
        color.setTextSize(35);
        color.setColor(Color.BLACK);

//modify canvas
        canvas1.drawBitmap(bmp, 0, 0, color);
        canvas1.drawText("User Name!", 30, 40, color);

        mMap.addMarker(new MarkerOptions()
                .position(new LatLng(dLat, dLon))
                .title(NAME_OF_MARKER))
                .setIcon(BitmapDescriptorFactory.fromBitmap(bmp));
    }

..but Never works. Everything loads fine, but never the icon is displayed.

I have carefully read many threads related like this:

Android load from URL to Bitmap

How to get bitmap from a url in android?

But I get it working, of course I have included the necessary permissions in the manifest, internet, write external, etc, but without success.

I would greatly appreciate any help, I have tested hundreds of ways.

Greetings and thank you in advance

Community
  • 1
  • 1
Sergio76
  • 3,835
  • 16
  • 61
  • 88
  • 1
    Why are you both creating your `Canvas` from the `Bitmap` *and* attempting to draw the bitmap to the `Canvas`? Why are you using black to draw your `Bitmap` to the `Canvas`? – CommonsWare Feb 02 '15 at 14:47
  • is quite possible that after trying many ways to do it, my code is confusing between canvas and bitmaps. Now do not understand the Right Way. Can this be the reason why no display? ... Or is it for another reason? Excluding the canvas icon not shown – Sergio76 Feb 02 '15 at 14:51
  • 1
    Yup, seems that your code is drawing an empty bitmap onto itself. Since your AsyncTask already has the bitmap, why not just use it in .setIcon() ? – xorgate Feb 02 '15 at 15:22
  • I think trying it before. I try again when i get home and I tell you – Sergio76 Feb 02 '15 at 16:20
  • Finally I was committing two errors, was creating an empty canvas (as you've seen) and was calling the bitmap in wrong order, was not yet available the url. Thanks! – Sergio76 Feb 03 '15 at 10:25

0 Answers0