【问题标题】:Maps marker point image with picasso not loaded at first time首次未加载毕加索的地图标记点图像
【发布时间】:2016-07-07 10:50:17
【问题描述】:

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

这是我的 PicassoMarker 课程

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));
    }
}

这是 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());
            }
        }
    }
}

这里出了什么问题?

谢谢。

【问题讨论】:

  • 我对此并不完全确定,因为我无法最终测试它,但是您是否尝试在 location_marker = mMap.addMarker(markerOption); 之前调用这些行 target = new PicassoMarker(location_marker); Picasso.with(MapsActivity.this).load(myMarker.getmIcon()).resize(84, 125).into(target);
  • 你能试一试并告诉我它是否有效..?
  • 是的,我试过了,但我得到了相同的结果。

标签: java android google-maps google-maps-markers


【解决方案1】:

好的,所以我设法重现了您所遇到的情况并找到了导致您的问题的原因。在您提供的代码中,请注意MapsActivity 中的这一行:

target = new PicassoMarker(location_marker);

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

原因是因为,每次进入循环时,target 的值都会更改为您拥有的 PicassoMarker,从而使之前的onBitmapLoaded PicassoMarker 你有无用,因为它不再有目标。 :(

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

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

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

编辑

这是我用来重现您的错误并对其进行修改以使其正常工作的活动代码:

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());
                }
            }
        }
    }
}

【讨论】:

  • 是的,只有使用 Picasso 加载图像的 Marker 是 for 循环中的最后一个 Marker。我试图添加一个像这样的变量private List&lt;Target&gt; targets;
  • 您是否也只是在循环内实例化了目标? Target target = .... 不使用单个全局变量?
  • 我已经像 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); 这样更改了我的代码,但应用程序崩溃了。你能告诉我正确的完整代码吗?提前致谢
  • 添加了我的活动代码。我没有更改PicassoMarker中的任何内容
  • 我应该使用这个变量private PicassoMarker target; 吗?
猜你喜欢
  • 1970-01-01
  • 2016-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-29
  • 1970-01-01
相关资源
最近更新 更多