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

Android 如何在mapview气球中添加自己的图像,android,Android,可能重复: 这是一个显示url中图像的代码。如何从我的res/drawable文件夹中给出图像?这是我使用的一个代码,请告诉我如何给我自己的图片添加url public class CustomMap extends MapActivity { MapView mapView; List<Overlay> mapOverlays; Drawable drawable; Drawable drawable2; CustomItemizedOverlay<CustomOver

可能重复:

这是一个显示url中图像的代码。如何从我的res/drawable文件夹中给出图像?这是我使用的一个代码,请告诉我如何给我自己的图片添加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;
    }   
}

     }
公共类CustomMap扩展了MapActivity{
地图视图;
列出地图覆盖图;
可拉伸;
可拉伸2;
定制项目化Overlay项目化Overlay;
CustomItemizedOverlay ItemizedOverlay 2;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView=(mapView)findViewById(R.id.mapView);
mapView.SetBuilTinZoomControl(真);
mapOverlays=mapView.getOverlays();
//第一层
drawable=getResources().getDrawable(R.drawable.marker);
itemizedOverlay=新的自定义itemizedOverlay(可绘制,
地图视图);
地质点=新的地质点((int)(51.5174723*1E6),(int)(-0.0899537*1E6));
CustomOverlayItem overlayItem=新CustomOverlayItem(点,“明天
永不磨灭(1997年)“,
“(M赋予邦德在戴姆勒汽车公司的使命)”,
"http://ia.media-imdb.com/images 
5BMTM1MTK2ODQXNV5BML5BANXKFTZTCWOTY5MDG0NA@.\u V1.\u SX40_CR0,0,40,54_.jpg”);
itemizedOverlay.addOverlay(overlayItem);
地质点2=新的地质点((int)(51.515259*1E6),(int)(-0.086623*1E6));
CustomOverlayItem overlayItem2=新CustomOverlayItem(第2点,
“GoldenEye(1995)”,
“(俄罗斯国防部内务委员会会议厅
圣彼得堡)“,
"http://ia.media-imdb.com 
图像5BMZK2OTG4MTK1NF5BML5BANxKFTZTCWNJ
ExNTgzNA@._V1._SX40_CR0,0,40,54_.jpg”);
itemizedOverlay.addOverlay(overlayItem2);
添加(itemizedOverlay);
//二次叠加
drawable2=getResources().getDrawable(R.drawable.marker2);
itemizedOverlay2=新的自定义ItemizedOverlay
(可绘图2,地图视图);
地质点3=新的地质点((int)(51.513329*1E6),(int)(-0.08896*1E6));
CustomOverlayItem overlayItem3=新CustomOverlayItem(第3点,“滑动
Doors(1998年)“,
“(内部)”,空);
itemizedOverlay2.addOverlay(overlayItem3);
地质点4=新的地质点((int)(51.51738*1E6),(int)(-0.08186*1E6));
CustomOverlayItem overlayItem4=新CustomOverlayItem(第4点,“任务:
不可能(1996年)“,
“(伊桑和吉姆咖啡屋会议)”,
"http://ia.media- 
imdb.COIMAGESV5BMJAYNJK5NJK0MV5BML5BANxKFTZTCWOT
A4MjIyMQ@._V1._SX40_CR0,0,40,54_.jpg”);
项目化覆盖2.添加覆盖(覆盖项4);
添加(itemizedOverlay2);
最终的MapController mc=mapView.getController();
司令官(第2点),;
mc.setZoom(16);
}
@凌驾
受保护的布尔值isRouteDisplayed(){
返回false;
}
}
公共类CustomOverlayItem扩展了OverlayItem{
受保护的字符串mImageURL;
公共CustomOverlayItem(地质点、字符串标题、字符串片段、字符串
图像URL){
超级(点、标题、片段);
mImageURL=imageURL;
}
公共字符串getImageURL(){
返回mimagerurl;
}
public void setImageURL(字符串imageURL){
this.mImageURL=imageURL;
}
}
公共类自定义视图
扩展气球覆盖视图{
私有文本视图标题;
私有文本视图片段;
私有图像查看图像;
公共自定义BallookOverlayView(上下文上下文,int-BallookBottomOffset){
super(上下文、上下文和偏移量);
}
@凌驾
受保护的void setupView(上下文、最终视图组父视图){
//将自定义布局充气到父级
LayoutFlater充气器=(LayoutFlater)上下文
.getSystemService(上下文布局\充气机\服务);
视图v=充气机。充气(R.layout.balloon\u overlay\u示例2,父级);
//设置我们的字段
title=(TextView)v.findViewById(R.id.balloon\u item\u title);
snippet=(TextView)v.findViewById(R.id.balloon\u item\u snippet);
image=(ImageView)v.findViewById(R.id.balloon\u item\u image);
}
@凌驾
受保护的无效setBalloonData(CustomOverlayItem项,视图组父项){
//将自定义项数据映射到字段
title.setText(item.getTitle());
snippet.setText(item.getSnippet());
//从网络获取远程映像。
//位图结果通常会被缓存,但这对于
演示目的。
setImageResource(R.drawable.icon);
新建FetchImageTask(){
受保护的void onPostExecute(位图结果){
如果(结果!=null){
image.setImageBitmap(结果);
}
}
}.execute(item.getImageURL());
}
私有类FetchImageTask扩展了AsyncTask{
@凌驾
受保护位图doInBackground(字符串…arg0){
位图b=null;
试一试{
b=BitmapFactory.decodeStream((InputStream)新建
URL(arg0[0]).getContent();
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
} 
返回b;
}   
}
}
简单的方法

  • 更改
    CustomOverlayItem
    class。如果不想从URL加载图像,可以将
    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;
    }
    
    }

  • CustomBalloodOverlayView
    类的
    SetballonData
    方法中,将其更改为

    @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());*/ }
    
  • 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,还必须修改
    BallookOverlayView

    不要因为没有答案而多次创建相同的问题。我查看了你的个人资料,有8个问题,但都是一样的。如果你看到一些答案是有用的,记得勾选答案左边的标记来设置它