我正在创建谷歌地图当前位置气球叠加层。
我使用android-mapviewballoons这个代码来显示气球。
此处当前位置显示工作正常。当我单击标记时,会出现错误(气球覆盖视图 57,38 行和气球逐项覆盖 87,188,76 行)。
我知道哪里有错误,但我不明白那部分,任何人请帮助我
这是日志猫错误
02-01 05:09:31.606: W/Trace(1250): Unexpected value from nativeGetEnabledTags: 0
02-01 05:09:31.636: I/System.out(1250): 6666666666666
02-01 05:09:31.636: I/System.out(1250): 77777777
02-01 05:09:31.646: I/System.out(1250): 7777888888
02-01 05:09:31.656: I/System.out(1250): 33333333
02-01 05:09:31.756: I/System.out(1250): 1111111
02-01 05:09:31.756: W/Trace(1250): Unexpected value from nativeGetEnabledTags: 0
02-01 05:09:31.756: E/InputEventReceiver(1250): Exception dispatching input event.
02-01 05:09:31.766: E/MessageQueue-JNI(1250): Exception in MessageQueue callback: handleReceiveCallback
02-01 05:09:31.836: D/dalvikvm(1250): GC_CONCURRENT freed 275K, 11% free 3508K/3904K, paused 35ms+27ms, total 163ms
02-01 05:09:31.926: E/MessageQueue-JNI(1250): java.lang.NullPointerException
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at com.sygnet.locationingooglemap.BalloonOverlayView.setupView(BalloonOverlayView.java:57)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at com.sygnet.locationingooglemap.BalloonOverlayView.<init>(BalloonOverlayView.java:38)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at com.sygnet.locationingooglemap.BalloonItemizedOverlay.createBalloonOverlayView(BalloonItemizedOverlay.java:87)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at com.sygnet.locationingooglemap.BalloonItemizedOverlay.createAndDisplayBalloonOverlay(BalloonItemizedOverlay.java:188)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at com.sygnet.locationingooglemap.BalloonItemizedOverlay.onTap(BalloonItemizedOverlay.java:76)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at com.google.android.maps.ItemizedOverlay.onTap(ItemizedOverlay.java:453)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at com.google.android.maps.OverlayBundle.onTap(OverlayBundle.java:83)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at com.google.android.maps.MapView$1.onSingleTapUp(MapView.java:358)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at com.google.android.maps.GestureDetector.onTouchEvent(GestureDetector.java:562)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at com.google.android.maps.MapView.onTouchEvent(MapView.java:685)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at android.view.View.dispatchTouchEvent(View.java:7239)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at android.view.View.dispatchPointerEvent(View.java:7419)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at android.os.MessageQueue.nativePollOnce(Native Method)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at android.os.MessageQueue.next(MessageQueue.java:125)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at android.os.Looper.loop(Looper.java:124)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at java.lang.reflect.Method.invokeNative(Native Method)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at java.lang.reflect.Method.invoke(Method.java:511)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-01 05:09:31.926: E/MessageQueue-JNI(1250): at dalvik.system.NativeStart.main(Native Method)
02-01 05:09:32.006: D/AndroidRuntime(1250): Shutting down VM
02-01 05:09:32.006: W/dalvikvm(1250): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
02-01 05:09:32.096: E/AndroidRuntime(1250): FATAL EXCEPTION: main
02-01 05:09:32.096: E/AndroidRuntime(1250): java.lang.NullPointerException
02-01 05:09:32.096: E/AndroidRuntime(1250): at com.sygnet.locationingooglemap.BalloonOverlayView.setupView(BalloonOverlayView.java:57)
02-01 05:09:32.096: E/AndroidRuntime(1250): at com.sygnet.locationingooglemap.BalloonOverlayView.<init>(BalloonOverlayView.java:38)
02-01 05:09:32.096: E/AndroidRuntime(1250): at com.sygnet.locationingooglemap.BalloonItemizedOverlay.createBalloonOverlayView(BalloonItemizedOverlay.java:87)
02-01 05:09:32.096: E/AndroidRuntime(1250): at com.sygnet.locationingooglemap.BalloonItemizedOverlay.createAndDisplayBalloonOverlay(BalloonItemizedOverlay.java:188)
02-01 05:09:32.096: E/AndroidRuntime(1250): at com.sygnet.locationingooglemap.BalloonItemizedOverlay.onTap(BalloonItemizedOverlay.java:76)
02-01 05:09:32.096: E/AndroidRuntime(1250): at com.google.android.maps.ItemizedOverlay.onTap(ItemizedOverlay.java:453)
02-01 05:09:32.096: E/AndroidRuntime(1250): at com.google.android.maps.OverlayBundle.onTap(OverlayBundle.java:83)
02-01 05:09:32.096: E/AndroidRuntime(1250): at com.google.android.maps.MapView$1.onSingleTapUp(MapView.java:358)
02-01 05:09:32.096: E/AndroidRuntime(1250): at com.google.android.maps.GestureDetector.onTouchEvent(GestureDetector.java:562)
02-01 05:09:32.096: E/AndroidRuntime(1250): at com.google.android.maps.MapView.onTouchEvent(MapView.java:685)
02-01 05:09:32.096: E/AndroidRuntime(1250): at android.view.View.dispatchTouchEvent(View.java:7239)
02-01 05:09:32.096: E/AndroidRuntime(1250): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168)
02-01 05:09:32.096: E/AndroidRuntime(1250): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903)
02-01 05:09:32.096: E/AndroidRuntime(1250): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
02-01 05:09:32.096: E/AndroidRuntime(1250): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
02-01 05:09:32.096: E/AndroidRuntime(1250): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
02-01 05:09:32.096: E/AndroidRuntime(1250): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
02-01 05:09:32.096: E/AndroidRuntime(1250): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
02-01 05:09:32.096: E/AndroidRuntime(1250): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
02-01 05:09:32.096: E/AndroidRuntime(1250): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
02-01 05:09:32.096: E/AndroidRuntime(1250): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
02-01 05:09:32.096: E/AndroidRuntime(1250): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
02-01 05:09:32.096: E/AndroidRuntime(1250): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
02-01 05:09:32.096: E/AndroidRuntime(1250): at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
02-01 05:09:32.096: E/AndroidRuntime(1250): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
02-01 05:09:32.096: E/AndroidRuntime(1250): at android.view.View.dispatchPointerEvent(View.java:7419)
02-01 05:09:32.096: E/AndroidRuntime(1250): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
02-01 05:09:32.096: E/AndroidRuntime(1250): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
02-01 05:09:32.096: E/AndroidRuntime(1250): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
02-01 05:09:32.096: E/AndroidRuntime(1250): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
02-01 05:09:32.096: E/AndroidRuntime(1250): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
02-01 05:09:32.096: E/AndroidRuntime(1250): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
02-01 05:09:32.096: E/AndroidRuntime(1250): at android.os.MessageQueue.nativePollOnce(Native Method)
02-01 05:09:32.096: E/AndroidRuntime(1250): at android.os.MessageQueue.next(MessageQueue.java:125)
02-01 05:09:32.096: E/AndroidRuntime(1250): at android.os.Looper.loop(Looper.java:124)
02-01 05:09:32.096: E/AndroidRuntime(1250): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-01 05:09:32.096: E/AndroidRuntime(1250): at java.lang.reflect.Method.invokeNative(Native Method)
02-01 05:09:32.096: E/AndroidRuntime(1250): at java.lang.reflect.Method.invoke(Method.java:511)
02-01 05:09:32.096: E/AndroidRuntime(1250): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-01 05:09:32.096: E/AndroidRuntime(1250): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-01 05:09:32.096: E/AndroidRuntime(1250): at dalvik.system.NativeStart.main(Native Method)
这是气球逐项叠加.java
public abstract class BalloonItemizedOverlay<Item extends OverlayItem> extends ItemizedOverlay<Item> {
private static final long BALLOON_INFLATION_TIME = 300;
private static Handler handler = new Handler();
private MapView mapView;
private BalloonOverlayView<Item> balloonView;
private View clickRegion;
private View closeRegion;
private int viewOffset;
final MapController mc;
private Item currentFocusedItem;
private int currentFocusedIndex;
private boolean showClose = true;
private boolean showDisclosure = false;
private boolean snapToCenter = true;
private static boolean isInflating = false;
public BalloonItemizedOverlay(Drawable defaultMarker, MapView mapView) {
super(defaultMarker);
this.mapView = mapView;
viewOffset = 0;
mc = mapView.getController();
}
public void setBalloonBottomOffset(int pixels) {
viewOffset = pixels;
}
public int getBalloonBottomOffset() {
return viewOffset;
}
protected boolean onBalloonTap(int index, Item item) {
return false;
}
protected void onBalloonOpen(int index) {}
@Override
public final boolean onTap(int index) {
handler.removeCallbacks(finishBalloonInflation);
isInflating = true;
handler.postDelayed(finishBalloonInflation, BALLOON_INFLATION_TIME);
System.out.println("6666666666666");
currentFocusedIndex = index;
currentFocusedItem = createItem(index);
setLastFocusedIndex(index);
onBalloonOpen(index);
createAndDisplayBalloonOverlay();
if (snapToCenter) {
mc.animateTo(currentFocusedItem.getPoint());
}
return true;
}
protected BalloonOverlayView<Item> createBalloonOverlayView() {
System.out.println("77777777");
return new BalloonOverlayView<Item>(getMapView().getContext(), getBalloonBottomOffset());
}
protected MapView getMapView() {
System.out.println("7777888888");
return mapView;
}
public void hideBalloon() {
if (balloonView != null) {
balloonView.setVisibility(View.GONE);
System.out.println("888888");
}
currentFocusedItem = null;
}
private void hideOtherBalloons(List<Overlay> overlays) {
for (Overlay overlay : overlays) {
if (overlay instanceof BalloonItemizedOverlay<?> && overlay != this) {
((BalloonItemizedOverlay<?>) overlay).hideBalloon();
System.out.println("88886666666666688");
}
}
}
public void hideAllBalloons() {
if (!isInflating) {
List<Overlay> mapOverlays = mapView.getOverlays();
if (mapOverlays.size() > 1) {
hideOtherBalloons(mapOverlays);
}
hideBalloon();
}
}
private OnTouchListener createBalloonTouchListener() {
return new OnTouchListener() {
double startX;
double startY;
public boolean onTouch(View v, MotionEvent event) {
// View l = ((View) v.getParent()).findViewById(R.id.balloon_main_layout);
Drawable d = v.getBackground();
//Event Action Down
if (event.getAction() == MotionEvent.ACTION_DOWN) {
int[] states = {android.R.attr.state_pressed};
if (d.setState(states)) {
d.invalidateSelf();
System.out.println("12122121212");
}
startX = event.getX();
startY = event.getY();
System.out.println("1111222211112222");
return true;
}
//Event Action Up
else if (event.getAction() == MotionEvent.ACTION_UP) {
int newStates[] = {};
if (d.setState(newStates)) {
d.invalidateSelf();
System.out.println("0000011111");
}
if (Math.abs(startX - event.getX()) < 40 && Math.abs(startY - event.getY()) < 40 ) {
// call overridden method
onBalloonTap(currentFocusedIndex, currentFocusedItem);
System.out.println("12345678");
}
return true;
} else {
return false;
}
}
};
}
@Override
public Item getFocus() {
return currentFocusedItem;
}
@Override
public void setFocus(Item item) {
super.setFocus(item);
currentFocusedIndex = getLastFocusedIndex();
currentFocusedItem = item;
if (currentFocusedItem == null) {
hideBalloon();
} else {
createAndDisplayBalloonOverlay();
}
}
private boolean createAndDisplayBalloonOverlay(){
boolean isRecycled;
if (balloonView == null) {
balloonView = createBalloonOverlayView();
clickRegion = (View) balloonView.findViewById(R.id.balloon_inner_layout);
clickRegion.setOnTouchListener(createBalloonTouchListener());
closeRegion = (View) balloonView.findViewById(R.id.balloon_close);
if (closeRegion != null) {
if (!showClose) {
closeRegion.setVisibility(View.GONE);
System.out.println("aaaaaaaa");
} else {
closeRegion.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
hideBalloon();
System.out.println("bbbbbbb");
}
});
}
}
if (showDisclosure && !showClose) {
View v = balloonView.findViewById(R.id.balloon_disclosure);
if (v != null) {
v.setVisibility(View.VISIBLE);
System.out.println("cccccccc");
}
}
isRecycled = false;
}
else {
isRecycled = true;
}
balloonView.setVisibility(View.GONE);
List<Overlay> mapOverlays = mapView.getOverlays();
if (mapOverlays.size() > 1) {
hideOtherBalloons(mapOverlays);
}
if (currentFocusedItem != null)
balloonView.setData(currentFocusedItem);
GeoPoint point = currentFocusedItem.getPoint();
MapView.LayoutParams params = new MapView.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, point,
MapView.LayoutParams.BOTTOM_CENTER);
params.mode = MapView.LayoutParams.MODE_MAP;
balloonView.setVisibility(View.VISIBLE);
System.out.println("dddddddd");
if (isRecycled) {
balloonView.setLayoutParams(params);
}
else {
mapView.addView(balloonView, params);
}
return isRecycled;
}
public void setShowClose(boolean showClose) {
this.showClose = showClose;
}
public void setShowDisclosure(boolean showDisclosure) {
this.showDisclosure = showDisclosure;
}
public void setSnapToCenter(boolean snapToCenter) {
this.snapToCenter = snapToCenter;
}
public static boolean isInflating() {
return isInflating;
}
private static Runnable finishBalloonInflation = new Runnable() {
public void run() {
isInflating = false;
}
};
}
这是气球叠加视图.java
public class BalloonOverlayView<Item extends OverlayItem> extends FrameLayout {
LinearLayout layout;
private TextView title;
MainActivity main;
private CharSequence tvLocation;
public BalloonOverlayView(Context context, int balloonBottomOffset) {
super(context);
setPadding(10, 0, 10, balloonBottomOffset);
layout = new LimitLinearLayout(context);
layout.setVisibility(VISIBLE);
setupView(context, layout);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.NO_GRAVITY;
addView(layout, params);
}
protected void setupView(Context context, ViewGroup parent) {
try {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.balloon_overlay, parent);
title = (TextView) v.findViewById(R.id.balloon_item_title);
System.out.println("00000000000");
}
catch(Exception e)
{
System.out.println("1111111");
title.setText(tvLocation);
}
// title.setText(tvLocation);
}
public void setData(Item item) {
layout.setVisibility(VISIBLE);
setBalloonData(item, layout);
System.out.println("1113333");
}
protected void setBalloonData(Item item, ViewGroup parent) {
if (item.getTitle() != null) {
title.setVisibility(VISIBLE);
title.setText(item.getTitle());
title.setText(tvLocation);
System.out.println("222222222");
} else {
title.setText("");
title.setVisibility(GONE);
System.out.println("111222222");
}
}
private class LimitLinearLayout extends LinearLayout {
private static final int MAX_WIDTH_DP = 280;
final float SCALE = getContext().getResources().getDisplayMetrics().density;
public LimitLinearLayout(Context context) {
super(context);
System.out.println("33333333");
}
public LimitLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
System.out.println("444444");
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int mode = MeasureSpec.getMode(widthMeasureSpec);
int measuredWidth = MeasureSpec.getSize(widthMeasureSpec);
int adjustedMaxWidth = (int)(MAX_WIDTH_DP * SCALE + 0.5f);
int adjustedWidth = Math.min(measuredWidth, adjustedMaxWidth);
int adjustedWidthMeasureSpec = MeasureSpec.makeMeasureSpec(adjustedWidth, mode);
System.out.println("555555555");
super.onMeasure(adjustedWidthMeasureSpec, heightMeasureSpec);
}
}
}
这是balloon_overlay.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/balloon_main_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/balloon_overlay_bg_selector"
android:minWidth="200dip"
android:orientation="horizontal"
android:paddingBottom="25dip"
android:paddingLeft="10dip"
android:paddingRight="0dip"
android:paddingTop="0dip" >
<LinearLayout
android:id="@+id/balloon_inner_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dip"
android:layout_weight="1" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="40dip"
android:orientation="vertical" >
<TextView
android:id="@+id/balloon_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/primary_text_light"
android:textSize="@string/txtsize"
android:text="" />
</LinearLayout>
<ImageView
android:id="@+id/balloon_disclosure"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:src="@drawable/balloon_disclosure"
android:visibility="gone" />
</LinearLayout>
<ImageView
android:id="@+id/balloon_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dip"
android:paddingLeft="10dip"
android:paddingRight="5dip"
android:paddingTop="5dip"
android:src="@drawable/balloon_overlay_close" />
</LinearLayout>
使用此代码,它对我来说工作正常:
public class CurrentLocationOverlay extends ItemizedOverlay<OverlayItem>{
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private MapView mapView;
private Context context;
//pin point drawable
private Drawable marker;
//balloon background
private Drawable viewDrawable;
private LinearLayout viewBubble;
public CurrentLocationOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
/**
*
* @param defaultMarker
* @param activity
* @param context
* @param mapView
*/
public CurrentLocationOverlay(Drawable defaultMarker , Context context, MapView mapView) {
this(boundCenterBottom(defaultMarker));
this.mapView = mapView;
this.context = context;
marker = defaultMarker;
growPopUpAnim = AnimationUtils.loadAnimation(context, R.anim.grow_from_bottom);
viewDrawable = context.getResources().getDrawable(R.drawable.currlocpopup);
}
@Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
@Override
public int size() {
return mapOverlays.size();
}
/**
*
* @param gp
* @param title
* @param msg
*/
public void addOverlay(GeoPoint gp , String title , String msg) {
OverlayItem overlay = new OverlayItem(gp, title, msg);
mapOverlays.add(overlay);
this.populate();
}
@Override
protected boolean onTap(int index) {
final int position = index;
Log.v("Overlay index", "" + position);
GeoPoint p = mapOverlays.get(index).getPoint();
addBubble(p , position);
return true;
}
/**
*
* @param point
* @param mapView
*/
private void addBubble(GeoPoint point , int index) {
dimissbubble(mapView);
// if there isn't any bubble on the screen enter
if (viewBubble == null) {
viewBubble = new LinearLayout(context);
viewBubble.bringToFront();
viewBubble.setBackgroundDrawable(viewDrawable);
TextView tv = new TextView(context);
tv.setText(mapOverlays.get(index).getTitle());
tv.setTextSize(16);
tv.setTextColor(Color.WHITE);
tv.setTypeface(null, Typeface.BOLD);
viewBubble.setGravity(Gravity.CENTER_HORIZONTAL);
viewBubble.setPadding(10, 10, 10, 10);
viewBubble.addView(tv);
MapView.LayoutParams mvlp = new MapView.LayoutParams(
MapView.LayoutParams.WRAP_CONTENT,
MapView.LayoutParams.WRAP_CONTENT, point,
0, -marker.getIntrinsicHeight(),
MapView.LayoutParams.BOTTOM_CENTER);
mvlp.mode = MapView.LayoutParams.MODE_MAP;
mapView.addView(viewBubble, mvlp);
viewBubble.startAnimation(growPopUpAnim);
// And the event.
viewBubble.setOnClickListener(new OnClickListener() {
// When we touch the bubble it is removed. And make null
// viewBubble to reuse it.
@Override
public void onClick(View v) {
mapView.removeView(viewBubble);
viewBubble = null;
}
});
}
}
/**
*
* @param mapView
*/
private void dimissbubble(MapView mapView) {
if (viewBubble != null) {
mapView.removeView(viewBubble);
viewBubble = null;
}
}
}
添加这个:
GeoPoint p = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
CurrentLocationOverlay currentLocationOverlay = new CurrentLocationOverlay(mDrawable,context, mapView);
currentLocationOverlay.addOverlay(p ,"Current Location" , "");
mapView.getOverlays().add(currentLocationOverlay);