0

可能重复:
如何在地图视图气球中添加图像?

这是一个显示来自 url 的图像的代码我如何从我的 res/drawable 文件夹中提供图像?这是我使用的https://github.com/jgilfelt/android-mapviewballoons 代码,请告诉我如何提供自己的图像而不是 url

 public class CustomMap extends MapActivity {

MapView mapView;
List<Overlay> mapOverlays;
Drawable drawable;
Drawable drawable2;
CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay;
CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay2;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);

    mapOverlays = mapView.getOverlays();

    // first overlay
    drawable = getResources().getDrawable(R.drawable.marker);
    itemizedOverlay = new CustomItemizedOverlay<CustomOverlayItem>(drawable,  
   mapView);

    GeoPoint point = new GeoPoint((int)(51.5174723*1E6),(int)(-0.0899537*1E6));
    CustomOverlayItem overlayItem = new CustomOverlayItem(point, "Tomorrow  
   Never Dies (1997)", 
            "(M gives Bond his mission in Daimler car)", 
            "http://ia.media-imdb.com/images 
 5BMTM1MTk2ODQxNV5BMl5BanBnXkFtZTcwOTY5MDg0NA@@._V1._SX40_CR0,0,40,54_.jpg");
    itemizedOverlay.addOverlay(overlayItem);

    GeoPoint point2 = new GeoPoint((int)(51.515259*1E6),(int)(-0.086623*1E6));
    CustomOverlayItem overlayItem2 = new CustomOverlayItem(point2,  
    "GoldenEye    (1995)", 
            "(Interiors Russian defence ministry council chambers in  
St Petersburg)", 
            "http://ia.media-imdb.com 


  images5BMzk2OTg4MTk1NF5BMl5BanBnXkFtZTcwNj
ExNTgzNA@@._V1._SX40_CR0,0,40,54_.jpg");        
    itemizedOverlay.addOverlay(overlayItem2);

    mapOverlays.add(itemizedOverlay);

    // second overlay
    drawable2 = getResources().getDrawable(R.drawable.marker2);
    itemizedOverlay2 = new CustomItemizedOverlay<CustomOverlayItem>
   (drawable2,    mapView);

    GeoPoint point3 = new GeoPoint((int)(51.513329*1E6),(int)(-0.08896*1E6));
    CustomOverlayItem overlayItem3 = new CustomOverlayItem(point3, "Sliding 
   Doors (1998)", 
            "(interiors)", null);
    itemizedOverlay2.addOverlay(overlayItem3);

    GeoPoint point4 = new GeoPoint((int)(51.51738*1E6),(int)(-0.08186*1E6));
    CustomOverlayItem overlayItem4 = new CustomOverlayItem(point4, "Mission:  
  Impossible (1996)", 
            "(Ethan & Jim cafe meeting)", 
            "http://ia.media- 
      imdb.coimagesV5BMjAyNjk5Njk0MV5BMl5BanBnXkFtZTcwOT
 A4MjIyMQ@@._V1._SX40_CR0,0,40,54_.jpg");       
    itemizedOverlay2.addOverlay(overlayItem4);

    mapOverlays.add(itemizedOverlay2);

    final MapController mc = mapView.getController();
    mc.animateTo(point2);
    mc.setZoom(16);

  }

@Override
protected boolean isRouteDisplayed() {
    return false;
}

         }







                     public class CustomOverlayItem extends OverlayItem {

protected String mImageURL;

public CustomOverlayItem(GeoPoint point, String title, String snippet, String  
         imageURL) {
    super(point, title, snippet);
    mImageURL = imageURL;
}

public String getImageURL() {
    return mImageURL;
}

public void setImageURL(String imageURL) {
    this.mImageURL = imageURL;
}

             }




                    public class CustomBalloonOverlayView<Item extends OverlayItem> 
       extends BalloonOverlayView<CustomOverlayItem> {

private TextView title;
private TextView snippet;
private ImageView image;

public CustomBalloonOverlayView(Context context, int balloonBottomOffset) {
    super(context, balloonBottomOffset);
}

@Override
protected void setupView(Context context, final ViewGroup parent) {

    // inflate our custom layout into parent
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.balloon_overlay_example2, parent);

    // setup our fields
    title = (TextView) v.findViewById(R.id.balloon_item_title);
    snippet = (TextView) v.findViewById(R.id.balloon_item_snippet);
    image = (ImageView) v.findViewById(R.id.balloon_item_image);

}

@Override
protected void setBalloonData(CustomOverlayItem item, ViewGroup parent) {

    // map our custom item data to fields
    title.setText(item.getTitle());
    snippet.setText(item.getSnippet());

    // get remote image from network.
    // bitmap results would normally be cached, but this is good enough for  
         demo purpose.
    image.setImageResource(R.drawable.icon);
    new FetchImageTask() { 
        protected void onPostExecute(Bitmap result) {
            if (result != null) {
                image.setImageBitmap(result);
            }
        }
    }.execute(item.getImageURL());

}

private class FetchImageTask extends AsyncTask<String, Integer, Bitmap> {
    @Override
    protected Bitmap doInBackground(String... arg0) {
        Bitmap b = null;
        try {
             b = BitmapFactory.decodeStream((InputStream) new  
       URL(arg0[0]).getContent());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } 
        return b;
    }   
}

     }
4

1 回答 1

0

简单的方法

  1. 换班CustomOverlayItem。如果您不想从 URL 加载图像,您可以更改 String mImageURLint drawableId. 并为此变量和新构造函数创建 getter 和 setter。

    public class CustomOverlayItem extends OverlayItem {
    
    protected String mImageURL;
    private int drawableId;
    
    
    
    public CustomOverlayItem(GeoPoint point, String title, String snippet, int drawableId) {
        super(point, title, snippet);
        this.drawableId = drawableId;
    }
    
    public CustomOverlayItem(GeoPoint point, String title, String snippet, String imageURL) {
        super(point, title, snippet);
        mImageURL = imageURL;
    }
    
    
    public int getDrawableId() {
        return drawableId;
    }
    
    public void setDrawableId(int drawableId) {
        this.drawableId = drawableId;
    }
    
    public String getImageURL() {
        return mImageURL;
    }
    
    public void setImageURL(String imageURL) {
        this.mImageURL = imageURL;
    }
    

    }

  2. 在类setBalloonData的方法中CustomBalloonOverlayView,将其更改为

    @Override
    protected void setBalloonData(CustomOverlayItem item, ViewGroup parent) {
    
    // map our custom item data to fields
    title.setText(item.getTitle());
    snippet.setText(item.getSnippet());
    
    // get remote image from network.
    // bitmap results would normally be cached, but this is good enough for demo purpose.
    image.setImageResource(item.getDrawableId()); // you set your image from drawable here.
    
    Here is where you load image from URL. So comment or remove it.
    /*new FetchImageTask() { 
        protected void onPostExecute(Bitmap result) {
            if (result != null) {
                image.setImageBitmap(result);
            }
         }
    }.execute(item.getImageURL());*/ }
    
  3. CustomMap中,更改创建叠加层的方式

     CustomOverlayItem overlayItem = new CustomOverlayItem(point, "Tomorrow Never Dies (1997)", 
                "(M gives Bond his mission in Daimler car)", 
                R.drawable.marker); // pass your drawable here  instead of URL.
    

如果你想保留方法加载 URL,你也必须修改BalloonOverlayView

于 2012-11-28T07:48:44.830 回答