Custom maps marker point with picasso not loaded

时间秒杀一切 提交于 2020-01-07 03:57:27

问题


I'm implementing android Mapview with Custom marker. I'm using picasso to load image into marker view. But the maps not shows me the marker that i needed.

Here's my code

i = getIntent();
    if(i.getBooleanExtra("maps", true)) {
        mactionBar.setSubtitle(i.getStringExtra("nama"));
        mMyMarkersArray.add(new MyMarker(i.getStringExtra("nama"), i.getStringExtra("deskripsi"), i.getStringExtra("foto"), i.getStringExtra("marker"), Double.parseDouble(i.getStringExtra("lat")), Double.parseDouble(i.getStringExtra("lng"))));
        plotMarkers(mMyMarkersArray);
    }

Here the code to load image marker. I'm implementing if(i.getBooleanExtra("maps", true)) because there are two activities that lead to this mapsview. If the previous activity put a true value, then it will bring just one marker data. But if the value is false, it will bring all markers data. The mapsview shows me all the markers that i needed if it got from the previous activity that bring false value. But the mapsview not shows me a marker if it got from the activity that bring the true value.

public void plotMarkers(ArrayList<MyMarker> markers) {
    if(markers.size() > 0) {
        for (MyMarker myMarker : markers)
        {
            markerOption = new MarkerOptions().position(new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude()));
            location_marker = mMap.addMarker(markerOption);
            Target target = new PicassoMarker(location_marker);
            targets.add(target);
            Picasso.with(MapsActivity.this).load(myMarker.getmIcon()).resize(84, 125).into(target);
            mMarkersHashMap.put(location_marker, myMarker);

            i = getIntent();
            if(i.getBooleanExtra("maps", true)) {
                buttonNavigasi.setVisibility(View.VISIBLE);

                location_marker.setTitle(i.getStringExtra("nama"));
                dest = new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude());
                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(dest, 16));
            }
            else {
                mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
            }
        }
    }
}

Here's the the code on how i call the MapsActivity

buttonNavigasi = (AppCompatButton) findViewById(R.id.button2);
    buttonNavigasi.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(DetailActivity.this,MapsActivity.class);
            i.putExtra("nama", daftarNama);
            i.putExtra("deskripsi", daftarDeskripsi);
            i.putExtra("foto", daftarFoto);
            i.putExtra("marker", daftarMarker);
            i.putExtra("lng", daftarLng);
            i.putExtra("lat", daftarLat);
            i.putExtra("maps", true);
            startActivity(i);
        }
    });

Here's the method of plotMarkers

public void plotMarkers(ArrayList<MyMarker> markers) {
    if(markers.size() > 0) {
        for (MyMarker myMarker : markers)
        {
            markerOption = new MarkerOptions().position(new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude()));
            location_marker = mMap.addMarker(markerOption);
            Target target = new PicassoMarker(location_marker);
            targets.add(target);
            Picasso.with(MapsActivity.this).load(myMarker.getmIcon()).resize(84, 125).into(target);
            mMarkersHashMap.put(location_marker, myMarker);

            i = getIntent();
            if(i.getBooleanExtra("maps", true)) {
                buttonNavigasi.setVisibility(View.VISIBLE);

                location_marker.setTitle(i.getStringExtra("nama"));
                dest = new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude());
                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(dest, 16));
            }
            else {
                mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
            }
        }
    }
}

PicassoMarker class

public class PicassoMarker implements Target {
Marker mMarker;

    PicassoMarker(Marker marker) {
        mMarker = marker;
    }

    @Override
    public int hashCode() {
        return mMarker.hashCode();
    }

    @Override
    public boolean equals(Object o) {
        if(o instanceof PicassoMarker) {
            Marker marker = ((PicassoMarker) o).mMarker;
            return mMarker.equals(marker);
        } else {
            return false;
        }
    }

    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
       mMarker.setIcon(BitmapDescriptorFactory.fromBitmap(bitmap));
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {
    }
}

What's wrong here?

Thanks.


回答1:


I use Handler to show the markers with some delay. So i change my code like this

i = getIntent();
if(i.getBooleanExtra("maps", true)) {
    ActionBar mactionBar = getSupportActionBar();
    mactionBar.setSubtitle(i.getStringExtra("nama"));
    buttonNavigasi.setVisibility(View.VISIBLE);
    mMyMarkersArray.add(new MyMarker(i.getStringExtra("nama"), i.getStringExtra("deskripsi"), i.getStringExtra("foto"), i.getStringExtra("marker"), Double.parseDouble(i.getStringExtra("lat")), Double.parseDouble(i.getStringExtra("lng"))));

    Handler UI_HANDLER = new Handler();
    UI_HANDLER.postDelayed(UI_UPDATE_RUNNABLE, 500);
}

And this

Runnable UI_UPDATE_RUNNABLE = new Runnable() {
    @Override
    public void run() {
        plotMarkers(mMyMarkersArray);
    }
};


来源:https://stackoverflow.com/questions/36134197/custom-maps-marker-point-with-picasso-not-loaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!