2016-03-21 54 views
2

我正在使用Custom標記實現android Mapview。我正在使用picasso將圖像加載到標記視圖中。當我第一次啓動應用程序時,它會向我顯示所有標記,但只有一個從數據庫加載畢加索的標記,其他標記不會從數據庫加載,它們僅向我顯示默認地圖標記引腳。但是,當我進入之前的活動並返回到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()); 
      } 
     } 
    } 
} 

什麼錯嗎?

謝謝。

+0

我不能完全肯定這一點,因爲我無法測試它在我的結束,但你有沒有嘗試調用這些行'target = new PicassoMarker(location_marker); (TargetActivity.this).load(myMarker.getmIcon())。resize(84,125).into(target);'location_marker = mMap.addMarker(markerOption)之前''' –

+0

你可以試試看,並告訴我,如果它以某種方式工作..? –

+0

是的,我試過了,但是我得到了同樣的結果。 –

回答

1

好的,所以我設法重現你正在經歷的事情,並發現是什麼導致你的問題。在您所提供的代碼,發現這一行MapsActivity

target = new PicassoMarker(location_marker); 

我假設你使用的是全球單變量target。我添加了一些日誌,並設法看到使用Picasso獲取圖像的唯一標記是for循環中的最後Marker

的原因是因爲,每次進入循環的target的值變化的PicassoMarker你有,做的以前PicassoMarkeronBitmapLoaded你有沒用,因爲它不再有一個目標。 :(

所以我所做的是,我只是增加了一個List<Target>變量(確保你不要忘了初始化)存儲target S的實例。在我前面指定的路線,我剛添加的代碼到target的值保存到列表中,像這樣:

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

測試它在我的模擬器,它加載圖像的所有Marker小號

編輯

這裏是我用來重現你的錯誤,然後修改它,使其工作活動代碼:

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

是的,只有使用畢加索加載圖像的標記是for循環中的最後一個標記。我試圖添加一個像這樣的變量'私人列表目標;' –

+0

好吧..它有效嗎? –

+0

你還剛剛實例化循環內的目標? 'Target target = ....'並且不使用單個全局變量? –

相關問題