我使用了本教程http://crazyandroidian.blogspot.com/2011/10/custom-mapview-popup-in-android.html请告诉我如何在气球标签中添加图片。我想在标记点气泡中添加位置图像。此代码只显示文本消息,但没有图像。
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;
}
}
}
答案 0 :(得分:0)
我相信你需要为你的气球创建一个布局。在this tutorial中,作者创建了一个气球视图并具有balloon.xml。要显示图像,您需要将正确的图像视图放在XML文件中。有点像尝试在列表视图中显示文本和图像,您需要行的布局。除非我错过了,否则你需要在R.id.balloon_main_layout
您甚至可能需要创建一个扩展OverlayItem的类,因为地图上的每个项目可能都有与之关联的不同图片。如果您创建了一个扩展OverlayItem的项目,那么您可以传入图片或具有在创建叠加层时检索图片的功能。
编辑:您的示例与其描述的方式完全一致。您在评论中链接到的教程可以解决您的问题。本教程显示旁边有ImageView
的文本。这些图像是在教程底部提供给您的,您必须将它们保存到res目录中名为drawable的文件夹中。
编辑:我实际上只是得到了你用过的代码。它显示一个空白地图,因为我没有放入我的api键,但弹出窗口有文本和旁边的取消按钮,这是从balloon_map_overlay.xml充气的图像视图
您可以像这样扩展OverlayItem包括图片:
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;
}
}
在气球叠加层视图中,您需要在创建叠印层时为图片设置数据。我将这个留给你,但示例显示了如何访问OverlayItem的get方法以及上面的代码提示,为要显示的Image提供getter和setter。否则它将在每个气球上显示相同的图像。
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。你的public class MyItemizedOverlay extends BalloonItemizedOverlay<OverlayItem>
需要
public class MyItemizedOverlay extends BalloonItemizedOverlay<MyItem>