第一次加载未加载毕加索的地图标记点图像 [英] Maps marker point image with picasso not loaded at first time

查看:92
本文介绍了第一次加载未加载毕加索的地图标记点图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Custom标记实现android Mapview。我正在使用picasso将图像加载到标记视图中。当我第一次启动应用程序时,它会向我显示所有标记,但只有一个从数据库加载毕加索的标记,其他标记不会从数据库加载,它们仅向我显示默认地图标记引脚。但是,当我进入之前的活动并返回到MapsActivity时,它会显示所有使用毕加索从数据库加载的标记。



这是我的PicassoMarker类

  public class PicassoMarker实现Target {
Marker mMarker;

PicassoMarker(标记标记){
mMarker = marker;


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

$ b @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(位图位图,Picasso.LoadedFrom from){
mMarker.setIcon(BitmapDescriptorFactory.fromBitmap(bitmap ));

$ b @Override
public void onBitmapFailed(Drawable errorDrawable){
}
$ b $ @Override
public void onPrepareLoad( Drawable placeHolderDrawable){
//mMarker.setIcon(BitmapDescriptorFactory.fromResource(R.mipmap.here));




$ b

以下是MapsActivity中的方法



pre $ $ $ $ $ $ $ $ $ $ $ $ b $ public $ plot $ (MyMarker myMarker:markers)
{
markerOption = new MarkerOptions()。position(new LatLng(myMarker.getmLatitude(),myMarker.getmLongitude()));
location_marker = mMap.addMarker(markerOption);
target = new PicassoMarker(location_marker);
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());




code
$ b

什么是在这里出错?



谢谢。

解决方案

设法重现您正在经历的事情,并找出造成您的问题的原因。在您提供的代码中,请注意 MapsActivity 中的以下行:

 目标=新的PicassoMarker(location_marker); 

我推测您正在使用全局变量 target 。我添加了一些日志,并设法看到使用 Picasso 加载图像的唯一Marker是 last Marker 在for循环中。



原因是因为每次进入循环时, target code>更改为新的 PicassoMarker ,使 onBitmapLoaded 以前的 PicassoMarker 您有无用,因为它不再有目标。 :(

所以我做的是,我只是添加了一个 List< Target> 变量( make确保你不要忘记初始化它)来存储 target s的实例。在我之前指定的行中,我只是添加了代码将 target 的值存储到列表中,如下所示:

  Target target = new PicassoMarker(location_marker); 
targets.add(target);

在我的模拟器上测试它,并将图像加载到所有标记 s。


编辑


以下是我用来重现错误然后修改它以使其正常工作的活动代码:

  public class MapsActivity扩展FragmentActivity实现OnMapReadyCallback {

私有GoogleMap mMap;

意图i;
MarkerOptions markerOption;
List< Target>目标;
HashMap& lt; Marker,MyMarker> mMarkersHashMap;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);

mMarkersHashMap = new HashMap<>();
targets = new ArrayList<>();

//获取SupportMapFragment并在地图准备好使用时得到通知。
SupportMapFragment mapFragment =(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}


/ **
*操作一次可用的地图。
*当地图准备好使用时,会触发此回调。
*这是我们可以添加标记或线条,添加听众或移动相机的位置。在这种情况下,
*我们只在澳大利亚悉尼附近添加一个标记。
*如果设备上未安装Google Play服务,系统会提示用户在SupportMapFragment中安装
* it。只有当用户安装了
* Google Play服务并返回到应用时,才会触发此方法。
* /
@Override
public void onMapReady(GoogleMap googleMap){
mMap = googleMap;

//在悉尼添加一个标记并移动相机
// LatLng sydney = new LatLng(-34,151);
// mMap.addMarker(新MarkerOptions()。position(悉尼).title(悉尼标记));
// mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
ArrayList< MyMarker> markers = new ArrayList< MyMarker>();
MyMarker m1 =新的MyMarker(新LatLng(-34,151.1),https://developer.chrome.com/extensions/examples/api/idle/idle_simple/sample-128.png);
MyMarker m2 = new MyMarker(new LatLng(-34,151.2),https://developer.chrome.com/extensions/examples/api/idle/idle_simple/sample-128.png);
MyMarker m3 = new MyMarker(new LatLng(-34,151.3),https://developer.chrome.com/extensions/examples/api/idle/idle_simple/sample-128.png);

markers.add(m1);
markers.add(m2);
markers.add(m3);

plotMarkers(标记);

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener(){
@Override
public boolean onMarkerClick(Marker marker){
Log.d(MapsActivity.class.getSimpleName (),MARKER Longitude:+ marker.getPosition()。longitude);
return false;
}
}); (MyMarker myMarker:标记)的
}

public void plotMarkers(ArrayList< MyMarker>标记){
if(markers.size()> 0){
){

markerOption = new MarkerOptions()。position(new LatLng(myMarker.getmLatitude(),myMarker.getmLongitude()));
Marker 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));
LatLng dest = new LatLng(myMarker.getmLatitude(),myMarker.getmLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(dest,8f));
} else {
Log.d(MapsActivity.class.getSimpleName(),In else {});
// mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
}
}
}
}
}


I'm implementing android Mapview with Custom marker. I'm using picasso to load image into marker view. And when i launch the app at the first time, it shows me all the markers, but only one marker that has loaded from database with picasso, the other markers are not loaded from database, they only show me the default maps marker pin. But when i go to the previous activity and go back into MapsActivity, it shows me all the markers that loaded from database with picasso.

Here's my 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) {
        //mMarker.setIcon(BitmapDescriptorFactory.fromResource(R.mipmap.here));
    }
}

Here's the method in MapsActivity

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 = new PicassoMarker(location_marker);
            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());
            }
        }
    }
}

What's going wrong here?

Thanks.

解决方案

Okay, so I managed to reproduce what you are experiencing and found what was causing your problem. In the code you provided, notice this line in MapsActivity:

target = new PicassoMarker(location_marker);

I presumed that you are using a global single variable for target. I added some logs and managed to see that the only Marker that gets the image loaded using Picasso is the last Marker in the for loop.

The reason is because, every time you enter the loop, the value of target changes to the newer PicassoMarker that you have, making the onBitmapLoaded of the previous PicassoMarker you have useless, since it no longer has a target. :(

So what I did is, I just added a List<Target> variable (make sure you don't forget to initialize it) to store the instances of the targets. In the line where that I specified earlier, I just added the code to store the value of the target to the list, like so:

Target target = new PicassoMarker(location_marker);
targets.add(target);

Tested it on my emulator and it loads the images to all the Markers.

EDIT

Here is the Activity code I used to reproduce your error and then modified it to make it work:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    Intent i;
    MarkerOptions markerOption;
    List<Target> targets;
    HashMap<Marker, MyMarker> mMarkersHashMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        mMarkersHashMap = new HashMap<>();
        targets = new ArrayList<>();

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        // LatLng sydney = new LatLng(-34, 151);
        // mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        // mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
        ArrayList<MyMarker> markers = new ArrayList<MyMarker>();
        MyMarker m1 = new MyMarker(new LatLng(-34, 151.1), "https://developer.chrome.com/extensions/examples/api/idle/idle_simple/sample-128.png");
        MyMarker m2 = new MyMarker(new LatLng(-34, 151.2), "https://developer.chrome.com/extensions/examples/api/idle/idle_simple/sample-128.png");
        MyMarker m3 = new MyMarker(new LatLng(-34, 151.3), "https://developer.chrome.com/extensions/examples/api/idle/idle_simple/sample-128.png");

        markers.add(m1);
        markers.add(m2);
        markers.add(m3);

        plotMarkers(markers);

        mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                Log.d(MapsActivity.class.getSimpleName(), "MARKER Longitude: " + marker.getPosition().longitude);
                return false;
            }
        });
    }

    public void plotMarkers(ArrayList<MyMarker> markers) {
        if (markers.size() > 0) {
            for (MyMarker myMarker : markers) {

                markerOption = new MarkerOptions().position(new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude()));
                Marker 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"));
                    LatLng dest = new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude());
                    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(dest, 8f));
                } else {
                    Log.d(MapsActivity.class.getSimpleName(), "In else{}");
                    // mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
                }
            }
        }
    }
}

这篇关于第一次加载未加载毕加索的地图标记点图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆