如何在mapview气球中添加自己的图像

时间:2012-11-28 05:55:00

标签: android

  

可能重复:
  How to add an image in mapview balloon?

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

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

     }

1 个答案:

答案 0 :(得分:0)

简单方法

  1. 更改CustomOverlayItem课程。如果您不想从网址加载图片,可以将String mImageURL更改为int 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.
    
  4. 如果要保留方法加载网址,则还必须修改BalloonOverlayView