1

I'm newbie and I'm developing an android app that tracks the employee's location and shows them on the map. It's almost done, except that it does not show the employee's image on the marker.

I was able to download the image on the web and set that image on the marker. But the problem is, it is not automatically shown. I need to plot the coordinates again to be able to show the image. Here is the code I used to download and set the image on the marker...

    private class DownloadEmployeeImage extends AsyncTask<String, Void, Bitmap> {

    ImageView image;

    public DownloadEmployeeImage(ImageView bmImage) {

        this.image = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {

        String url = urls[0];
        Bitmap bmpImg = null;

        try {

            InputStream in = new java.net.URL(url).openStream();
            bmpImg = BitmapFactory.decodeStream(in);

        } catch (Exception e) { 

            Log.e("Failed to download image: ", e.toString());
        }           

        return bmpImg;
    }

    protected void onPostExecute(Bitmap bmpImg) {

        try {

            image.setImageBitmap(RoundedImageView.getCroppedBitmap(bmpImg, 200));                               

        } catch(Exception e) {

            image.setImageBitmap(RoundedImageView.getCroppedBitmap(BitmapFactory
                    .decodeResource(MainActivity.this.getResources(), R.drawable.ic_user_placeholder), 200));               
        }
    }
}   

after downloading and setting the image on the marker, I added the marker on the map

    View viewLocationMarker = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layout_location_marker, null);
    ImageView ivPictureLocationMarker = (ImageView) viewLocationMarker.findViewById(R.id.ivPictureLocationMarker);

    new DownloadEmployeeImage(ivPictureLocationMarker).execute(TheURLOfTheImage);       

    Marker locationMarker = googleMap.addMarker(new MarkerOptions()
    .position(new LatLng(latitude, longitude)).title(name).anchor(0.33f, 1)
    .icon(BitmapDescriptorFactory.fromBitmap(TarkieManagerLib
            .createDrawableFromView(MainActivity.this, viewLocationMarker))));

any brilliant idea on how can I do this?

mgcaguioa
  • 1,443
  • 16
  • 19

2 Answers2

1

ok fine i think problem is here in below code

new DownloadEmployeeImage(ivPictureLocationMarker).execute(TheURLOfTheImage);

Marker locationMarker = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude)).title(name).anchor(0.33f, 1)
.icon(BitmapDescriptorFactory.fromBitmap(TarkieManagerLib
        .createDrawableFromView(MainActivity.this, viewLocationMarker))));

in this code you are download image(call asynktask) and set image on marker sequentially but in actually both process work parallel. your question is how? i will explain in detail. See when you call Asynctask then it work in backend and its below code execute. so here you call for load image and before it load system set marker.so in that case just set marker in OnPostexecute(...){

Marker locationMarker = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude)).title(name).anchor(0.33f, 1)
.icon(BitmapDescriptorFactory.fromBitmap(TarkieManagerLib
        .createDrawableFromView(MainActivity.this, viewLocationMarker))));

Here also set marker on map....

} thats it...

Jayesh Khasatiya
  • 2,140
  • 1
  • 14
  • 15
0

You have to use custom marker for showing image on the marker.Follow this link :

Google Maps Android API v2 - Interactive InfoWindow (like in original android google maps)

Community
  • 1
  • 1
Harsh Parikh
  • 3,845
  • 3
  • 14
  • 14
  • I used custom marker here. Actually, I was able to download the image and set it on the marker. The only problem is that, the image on the marker is not shown right after it was set (on the onPostExecute). I need to refresh or add the marker again to see the image on the marker. – mgcaguioa Oct 07 '14 at 10:52
  • I think so, i got the problem,actually you have written marker.set icon below the execute .You should have to write it in postexecute().Actually, aynchronous task execute both code paralley and you are setting image below, there may be chance it doesn't get executed – Harsh Parikh Oct 07 '14 at 10:58
  • Yes, I got it. I must set the image on the postexecute(). By the way, thanks! – mgcaguioa Oct 07 '14 at 11:16