如何添加在图形页面气球的形象呢? [英] How to add an image in mapview balloon?

查看:99
本文介绍了如何添加在图形页面气球的形象呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个教程的http:// crazyandroidian。 blogspot.com/2011/10/custom-mapview-popup-in-android.html 请告诉我如何添加图像在气球标签。我想补充的位置图像中的标记点泡沫。这code只是显示一个文本消息,但没有图像。

 包com.readystatesoftware.mapviewballoons;
进口mapviewballoons.example.R;
进口java.lang.reflect.Method中;
进口的java.util.List;
进口android.graphics.drawable.Drawable;
进口android.util.Log;
进口android.view.MotionEvent;
进口android.view.View;
进口android.view.View.OnTouchListener;
进口android.view.ViewGroup.LayoutParams;
进口com.google.android.maps.GeoPoint;
进口com.google.android.maps.ItemizedOverlay;
进口com.google.android.maps.MapController;
进口com.google.android.maps.MapView;
进口com.google.android.maps.Overlay;
进口com.google.android.maps.OverlayItem;

公共抽象类BalloonItemizedOverlay<项目>扩展ItemizedOverlay< OverlayItem> {
  私人图形页面图形页面;
  私人BalloonOverlayView balloonView;
  私人查看clickRegion;
  私人诠释viewOffset;
  最后MapController MC;

  公共BalloonItemizedOverlay(可绘制defaultMarker,图形页面图形页面){
    超(defaultMarker);
    this.mapView =图形页面;
    viewOffset = 0;
    MC = mapView.getController();
  }
  公共无效setBalloonBottomOffset(INT像素){
    viewOffset =像素;
  }
  保护布尔onBalloonTap(INT指数){
    返回false;
  }
  @覆盖
  受保护的最后布尔中的onTap(INT指数){
    布尔isRecycled;
    最终诠释thisIndex;
    的GeoPoint点;
    thisIndex =指数;
    点= createItem中(指数).getPoint();
    如果(balloonView == NULL){
      balloonView =新BalloonOverlayView(mapView.getContext(),viewOffset);
      clickRegion =(查看)balloonView.findViewById(R.id.balloon_inner_layout);
      isRecycled = FALSE;
    } 其他 {
      isRecycled = TRUE;
    }
    balloonView.setVisibility(View.GONE);
    名单<覆盖> mapOverlays =调用MapView.getOverlays();
    如果(mapOverlays.size()→1){
      hideOtherBalloons(mapOverlays);
    }
    balloonView.setData(createItem中(指数));
    MapView.LayoutParams PARAMS =新MapView.LayoutParams(
      LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,点,
      MapView.LayoutParams.BOTTOM_CENTER
    );
    params.mode = MapView.LayoutParams.MODE_MAP;
    setBalloonTouchListener(thisIndex);
    balloonView.setVisibility(View.VISIBLE);
    如果(isRecycled){
      balloonView.setLayoutParams(PARAMS);
    } 其他 {
      mapView.addView(balloonView,则params);
    }
    mc.animateTo(点);
    返回true;
  }
  / **
    *设置此覆盖的气球,以GONE的知名度。
  * /
  私人无效hideBalloon(){
    如果(balloonView!= NULL){
      balloonView.setVisibility(View.GONE);
    }
  }
  私人无效hideOtherBalloons(名单<覆盖>覆盖){
    对于(叠加覆盖:覆盖){
      如果(叠加的instanceof BalloonItemizedOverlay<>&安培;&放大器;叠加=这个!){
        ((BalloonItemizedOverlay<>)叠加).hideBalloon();
      }
    }
  }
  私人无效setBalloonTouchListener(最终诠释thisIndex){
    尝试 {
      @燮pressWarnings(未使用)
      方法M = this.getClass()getDeclaredMethod(onBalloonTap,int.class)。
      clickRegion.setOnTouchListener(新OnTouchListener(){
        公共布尔onTouch(视图V,MotionEvent事件){
          查看L =((查看)v.getParent())findViewById(R.id.balloon_main_layout)。
          绘制对象D = l.getBackground();
          如果(event.getAction()== MotionEvent.ACTION_DOWN){
            INT []状态= {android.R.attr.state_ pressed};
            如果(d.setState(州)){
              d.invalidateSelf();
            }
            返回true;
          }否则,如果(event.getAction()== MotionEvent.ACTION_UP){
            INT newStates [] = {};
            如果(d.setState(newStates)){
              d.invalidateSelf();
            }
            //调用覆盖的方法
            onBalloonTap(thisIndex);
            返回true;
          } 其他 {
            返回false;
          }
        }
      });
    }赶上(SecurityException异常E){
      Log.e(BalloonItemizedOverlay,setBalloonTouchListener反射SecurityException异常);
      返回;
    }赶上(NoSuchMethodException E){
      //方法不重写 - 什么都不做
      返回;
    }
  }
}
 

解决方案

我相信,你需要创建你的气球的布局。在<一个href="http://mobileappproject.word$p$pss.com/2011/11/26/add-a-balloon-pop-up-on-map-in-itemizedoverlay/"相对=nofollow>本教程笔者创建了一个气球景观,并设有balloon.xml要显示的图像,你会把正确的图像视图中的XML文件研究。有点像试图在您需要的行的布局,以显示在列表视图中的文字和图片。除非我错过了它,你需要在这个定义你的 R.id.balloon_main_layout

您甚至可能需要创建一个类扩展OverlayItem因为地图上的每一个项目将可能有一个与之关联的不同的画面。如果您创建一个扩展OverlayItem那么一个项目,你可以通过在图片或创建覆盖时具备的功能来获取图片。

编辑:你的榜样工程完全它所描述的方式。您在您的评论链接到本教程解决您的问题。它旁边的教程显示文本与的ImageView 。这些图像是在本教程的底部提供给你,你必须将它们保存到你的资源目录中的文件夹命名为绘制。

编辑:其实我刚收到你使用的工作的code。它显示了一个空白的地图,因为我没有把我的API密钥的,但弹出包含文本和取消它旁边的是balloon_map_overlay.xml膨胀的图像视图按钮

您可以延长OverlayItem这样一幅图片:

 进口android.graphics.Rect;
进口android.graphics.drawable.Drawable;

进口com.google.android.maps.GeoPoint;
进口com.google.android.maps.OverlayItem;

公共类MyItem扩展OverlayItem {

    可绘制balloonPic = NULL;
    矩形mBounds = NULL;
    公共MyItem(GeoPoint对象点,弦乐标题,弦乐片段,可绘制抽奖,矩形边界){
        超(点,标题段);

        mBounds =界限;
        // 0,0位置在地图上的GeoPoint。正打算向右和向下。消极的去留下了起来。的setBounds接受(leftBound,topbound,右束缚,底界)
        draw.setBounds(-draw.getIntrinsicWidth()/ 2,-draw.getIntrinsicHeight(),draw.getIntrinsicWidth()/ 2,0);
        this.setBalloonPic(抽奖);

    }

    @覆盖
    公共无效setBalloonPic(可绘制平局){

        balloonPic =借鉴;

    }
    @覆盖
    公众可绘制getBalloonPic(){

        返回balloonPic;

    }

}
 

在你的气球overlayview你需要设置你的数据提供的照片获得创建的overlayitems时。生病离开这个给你,但这个例子说明了如何访问OverlayItem的get方法和上面的code暗示具有getter和setter方法​​,你要显示的图像。否则它将显示在每个球囊相同的图像。

 公共无效使用setData(OverlayItem项目){
    layout.setVisibility(可见);
    如果(item.getTitle()!= NULL){
    title.setVisibility(可见);
    title.setText(item.getTitle());
    } 其他 {
    title.setVisibility(GONE);
    }
    如果(item.getSnippet()!= NULL){
    snippet.setVisibility(可见);
    snippet.setText(item.getSnippet());
    } 其他 {
    snippet.setVisibility(GONE);
    }
 

P.S。你的公共类MyItemizedOverlay扩展BalloonItemizedOverlay&LT; OverlayItem&GT; 必须是 公共类MyItemizedOverlay扩展BalloonItemizedOverlay&LT; MyItem&GT;

i used this tutorial http://crazyandroidian.blogspot.com/2011/10/custom-mapview-popup-in-android.html Please tell me how to add an image in a balloon tab. I want to add a location image in a marker point bubble. This code just shows a text message but no image.

package com.readystatesoftware.mapviewballoons;
import mapviewballoons.example.R;
import java.lang.reflect.Method;
import java.util.List;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public abstract class BalloonItemizedOverlay<Item> extends ItemizedOverlay<OverlayItem>  {
  private MapView mapView;
  private BalloonOverlayView balloonView;
  private View clickRegion;
  private int viewOffset;
  final MapController mc;

  public BalloonItemizedOverlay(Drawable defaultMarker, MapView mapView) {
    super(defaultMarker);
    this.mapView = mapView;
    viewOffset = 0;
    mc = mapView.getController();
  }
  public void setBalloonBottomOffset(int pixels) {
    viewOffset = pixels;
  }
  protected boolean onBalloonTap(int index) {
    return false;
  }
  @Override
  protected final boolean onTap(int index) {
    boolean isRecycled;
    final int thisIndex;
    GeoPoint point;
    thisIndex = index;
    point = createItem(index).getPoint();
    if (balloonView == null) {
      balloonView = new BalloonOverlayView(mapView.getContext(), viewOffset);
      clickRegion = (View) balloonView.findViewById(R.id.balloon_inner_layout);
      isRecycled = false;
    } else {
      isRecycled = true;
    }
    balloonView.setVisibility(View.GONE);
    List<Overlay> mapOverlays = mapView.getOverlays();
    if (mapOverlays.size() > 1) {
      hideOtherBalloons(mapOverlays);
    }
    balloonView.setData(createItem(index));
    MapView.LayoutParams params = new MapView.LayoutParams(
      LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, point,
      MapView.LayoutParams.BOTTOM_CENTER
    );
    params.mode = MapView.LayoutParams.MODE_MAP;
    setBalloonTouchListener(thisIndex);
    balloonView.setVisibility(View.VISIBLE);
    if (isRecycled) {
      balloonView.setLayoutParams(params);
    } else {
      mapView.addView(balloonView, params);
    }
    mc.animateTo(point);
    return true;
  }
  /**
    * Sets the visibility of this overlay's balloon view to GONE.
  */
  private void hideBalloon() {
    if (balloonView != null) {
      balloonView.setVisibility(View.GONE);
    }
  }
  private void hideOtherBalloons(List<Overlay> overlays) {
    for (Overlay overlay : overlays) {
      if (overlay instanceof BalloonItemizedOverlay<?> && overlay != this) {
        ((BalloonItemizedOverlay<?>) overlay).hideBalloon();
      }
    }
  }
  private void setBalloonTouchListener(final int thisIndex) {
    try {
      @SuppressWarnings("unused")
      Method m = this.getClass().getDeclaredMethod("onBalloonTap", int.class);
      clickRegion.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
          View l =  ((View) v.getParent()).findViewById(R.id.balloon_main_layout);
          Drawable d = l.getBackground();
          if (event.getAction() == MotionEvent.ACTION_DOWN) {
            int[] states = {android.R.attr.state_pressed};
            if (d.setState(states)) {
              d.invalidateSelf();
            }
            return true;
          } else if (event.getAction() == MotionEvent.ACTION_UP) {
            int newStates[] = {};
            if (d.setState(newStates)) {
              d.invalidateSelf();
            }
            // call overridden method
            onBalloonTap(thisIndex);
            return true;
          } else {
            return false;
          }
        }
      });
    } catch (SecurityException e) {
      Log.e("BalloonItemizedOverlay", "setBalloonTouchListener reflection SecurityException");
      return;
    } catch (NoSuchMethodException e) {
      // method not overridden - do nothing
      return;
    }
  }
}

解决方案

I believe that you need to create a layout for your balloon. In this tutorial the author creates a balloon view and has a balloon.xml To display an image you would put the correct image view in the XML file. Kind of like trying to display text and images in a list view where you need a layout for the rows. Unless i missed it you need to define this in your R.id.balloon_main_layout

You might even need to create a class extending OverlayItem because each item on the map will probably have a different picture associated with it. If you create an item that extends OverlayItem then you can pass in a picture or have functions to retrieve the picture when the overlay is created.

EDIT: Your example works exactly the way it describes. The tutorial that you linked to in your comments solves your problem. The tutorial displays text with an ImageView next to it. The images are given to you at the bottom of the tutorial you have to save them to a folder named drawable in your res directory.

Edit: I actually just got the code that you used working. It displays a blank map because i didnt put my api key in but the popup has text and a cancel button next to it that is the image view inflated from balloon_map_overlay.xml

You can extend the OverlayItem like this Include a picture:

import android.graphics.Rect;
import android.graphics.drawable.Drawable;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.OverlayItem;

public class MyItem extends OverlayItem{

    Drawable balloonPic = null;
    Rect mBounds = null;
    public MyItem(GeoPoint point, String title, String snippet, Drawable draw, Rect bounds) {
        super(point, title, snippet);

        mBounds = bounds;
        //0,0 position is the geopoint on the map. Positive is going right and down. negative goes left and up. setBounds accepts (leftBound, topbound, right bound, bottom bound)
        draw.setBounds(-draw.getIntrinsicWidth()/2, -draw.getIntrinsicHeight(), draw.getIntrinsicWidth()/2, 0);
        this.setBalloonPic(draw);

    }

    @Override
    public void setBalloonPic(Drawable draw) {

        balloonPic = draw;

    }
    @Override
    public Drawable getBalloonPic() {

        return balloonPic;

    }

}

In Your balloon overlayview you need to set your data for the pictures when the overlayitems get created. Ill leave that up to you but the example shows how to access the OverlayItem's get methods and the above code hints at having getters and setters for the Image that you want to display. Otherwise it will display the same image on every balloon.

public void setData(OverlayItem item) {
    layout.setVisibility(VISIBLE);
    if (item.getTitle() != null) {
    title.setVisibility(VISIBLE);
    title.setText(item.getTitle());
    } else {
    title.setVisibility(GONE);
    }
    if (item.getSnippet() != null) {
    snippet.setVisibility(VISIBLE);
    snippet.setText(item.getSnippet());
    } else {
    snippet.setVisibility(GONE);
    }

P.S. your public class MyItemizedOverlay extends BalloonItemizedOverlay<OverlayItem> needs to be public class MyItemizedOverlay extends BalloonItemizedOverlay<MyItem>

这篇关于如何添加在图形页面气球的形象呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆