本文介绍了点击气球在图形页面应该开始另一项活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在应用程序中创建图形页面,也从我的JSON解析取值的MapView内部标记,并在弹出气球显示吧..再次气球图片中点击我应该开始一个新的,当我需要开始一个活动活动..请帮我找到一个解决方案。这是我的BallonItemizedOverlayclass
包com.smartmedia.salonaudi.map;进口的java.lang.reflect.Method;
进口的java.util.List;进口android.app.Activity;
进口android.content.Context;
进口android.graphics.drawable.Drawable;
进口android.util.Log;
进口android.view.MotionEvent;
进口android.view.View;
进口android.view.View.OnTouchListener;进口com.google.android.maps.GeoPoint;
进口com.google.android.maps.ItemizedOverlay;
进口com.google.android.maps.MapController;
进口com.google.android.maps.MapView;
进口com.google.android.maps.MapView.LayoutParams;
进口com.google.android.maps.Overlay;
进口com.google.android.maps.OverlayItem;
进口com.smartmedia.salonaudi.R;公共抽象类BalloonItemizedOverlay<项目扩展OverlayItem>扩展
ItemizedOverlay<项目> { 私人MapView类MapView类;
私人BalloonOverlayView<项目> balloonView;
私人查看clickRegion;
私人诠释viewOffset;
私人语境mContext;
私人活动mActivity;
私人布尔mCheckIn;
最后MapController MC; / **
*创建一个新BalloonItemizedOverlay
*
* @参数defaultMarker
* - 要在地图中的每个项目上画的有界绘制对象
*覆盖。
* @参数的MapView
* - 在其上覆盖物品的视图要绘制。
* /
公共BalloonItemizedOverlay(可绘制defaultMarker,MapView类MapView类){
超(defaultMarker);
this.mapView =图形页面;
mContext = mapView.getContext();
viewOffset = 0;
MC = mapView.getController();
} / **
*设置标记和的底部之间的水平距离
*信息气球。默认值是0,这对于中心为界运作良好
*标记。如果你的标记是中心底部为界,前加入调用此
*覆盖项目,以确保气球悬停完全标记上方。
*
* @参数像素
* - 中心点和的底部之间的填充
*信息气球。
* /
公共无效setBalloonBottomOffset(INT像素){
viewOffset =像素;
} 公众诠释getBalloonBottomOffset(){
返回viewOffset;
} / **
*覆盖此方法可在气球处理点击。默认情况下,不
*不并返回false。
*
* @参数指标
* - 它的气球窃听项目的索引。
返回:true,如果你处理的自来水,否则为false。
* /
保护布尔onBalloonTap(INT指数){ 返回true;
} / *
*(非Javadoc中)
*
* @see com.google.android.maps.ItemizedOverlay#中的onTap(INT)
* /
@覆盖
保护最终布尔中的onTap(INT指数){ 布尔isRecycled;
最终诠释thisIndex;
的GeoPoint点; thisIndex =指数;
点= createItem中(指数).getPoint(); 如果(balloonView == NULL){
balloonView = createBalloonOverlayView();
clickRegion =(查看)balloonView
.findViewById(R.id.balloon_inner_layout);
isRecycled = FALSE;
}其他{
isRecycled = TRUE;
} balloonView.setVisibility(View.GONE); 清单<&叠加GT; 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;
} / **
*创建气球视图。重写以创建一个子类查看可
*填充额外的子视图。
* /
保护BalloonOverlayView<项目> createBalloonOverlayView(){
返回新BalloonOverlayView<项目>(getMapView()的getContext()。
getBalloonBottomOffset());
} / **
*暴露地图视图子类。由于创作的气球意见帮助。
* /
受保护的MapView getMapView(){
返回图形页面;
} / **
*设置此覆盖的气球以GONE的可见性。
* /
保护无效hideBalloon(){
如果(balloonView!= NULL){
balloonView.setVisibility(View.GONE);
}
} / **
*隐藏任何其他BalloonItemizedOverlay实例气球视图
*这可能是对的MapView present。
*
* @参数覆盖
* - 上的MapView覆盖(包括本)的列表。
* /
私人无效hideOtherBalloons(列表<&叠加GT;覆盖){ 对于(叠加覆盖:覆盖){
如果(覆盖的instanceof BalloonItemizedOverlay<>&放大器;&安培;覆盖该=!){
((BalloonItemizedOverlay<>)叠加).hideBalloon();
}
}
} / **
*设置气球onTouchListener显示,呼叫
*重写onBalloonTap如果实施。
*
* @参数thisIndex
* - 它的气球窃听项目的索引。
* /
私人无效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异常五){
Log.e(BalloonItemizedOverlay
setBalloonTouchListener反射SecurityException异常);
返回;
}赶上(NoSuchMethodException E){
//方法不重写 - 什么都不做
返回;
} }}
解决方案
保护布尔onBalloonTap(INT指数){
意图myintent =新意图(这一点,
secondactivity.class);
mContext.startActivity(myintent);
返回true;
}
i created mapview in my application,and also markers inside the mapview which takes values from my json parsing and displayed it in the ballon popup .. again i need to start an activity when clicking in the baloon image i should start a new activity.. please help me to find a solution.. This is my BallonItemizedOverlayclass
package com.smartmedia.salonaudi.map;
import java.lang.reflect.Method;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
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.MapView.LayoutParams;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.smartmedia.salonaudi.R;
public abstract class BalloonItemizedOverlay<Item extends OverlayItem> extends
ItemizedOverlay<Item> {
private MapView mapView;
private BalloonOverlayView<Item> balloonView;
private View clickRegion;
private int viewOffset;
private Context mContext;
private Activity mActivity;
private boolean mCheckIn;
final MapController mc;
/**
* Create a new BalloonItemizedOverlay
*
* @param defaultMarker
* - A bounded Drawable to be drawn on the map for each item in
* the overlay.
* @param mapView
* - The view upon which the overlay items are to be drawn.
*/
public BalloonItemizedOverlay(Drawable defaultMarker, MapView mapView) {
super(defaultMarker);
this.mapView = mapView;
mContext = mapView.getContext();
viewOffset = 0;
mc = mapView.getController();
}
/**
* Set the horizontal distance between the marker and the bottom of the
* information balloon. The default is 0 which works well for center bounded
* markers. If your marker is center-bottom bounded, call this before adding
* overlay items to ensure the balloon hovers exactly above the marker.
*
* @param pixels
* - The padding between the center point and the bottom of the
* information balloon.
*/
public void setBalloonBottomOffset(int pixels) {
viewOffset = pixels;
}
public int getBalloonBottomOffset() {
return viewOffset;
}
/**
* Override this method to handle a "tap" on a balloon. By default, does
* nothing and returns false.
*
* @param index
* - The index of the item whose balloon is tapped.
* @return true if you handled the tap, otherwise false.
*/
protected boolean onBalloonTap(int index) {
return true;
}
/*
* (non-Javadoc)
*
* @see com.google.android.maps.ItemizedOverlay#onTap(int)
*/
@Override
protected final boolean onTap(int index) {
boolean isRecycled;
final int thisIndex;
GeoPoint point;
thisIndex = index;
point = createItem(index).getPoint();
if (balloonView == null) {
balloonView = createBalloonOverlayView();
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;
}
/**
* Creates the balloon view. Override to create a sub-classed view that can
* populate additional sub-views.
*/
protected BalloonOverlayView<Item> createBalloonOverlayView() {
return new BalloonOverlayView<Item>(getMapView().getContext(),
getBalloonBottomOffset());
}
/**
* Expose map view to subclasses. Helps with creation of balloon views.
*/
protected MapView getMapView() {
return mapView;
}
/**
* Sets the visibility of this overlay's balloon view to GONE.
*/
protected void hideBalloon() {
if (balloonView != null) {
balloonView.setVisibility(View.GONE);
}
}
/**
* Hides the balloon view for any other BalloonItemizedOverlay instances
* that might be present on the MapView.
*
* @param overlays
* - list of overlays (including this) on the MapView.
*/
private void hideOtherBalloons(List<Overlay> overlays) {
for (Overlay overlay : overlays) {
if (overlay instanceof BalloonItemizedOverlay<?> && overlay != this) {
((BalloonItemizedOverlay<?>) overlay).hideBalloon();
}
}
}
/**
* Sets the onTouchListener for the balloon being displayed, calling the
* overridden onBalloonTap if implemented.
*
* @param thisIndex
* - The index of the item whose balloon is tapped.
*/
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;
}
}
}
解决方案
protected boolean onBalloonTap(int index) {
Intent myintent = new Intent(this,
secondactivity.class);
mContext.startActivity(myintent);
return true;
}
这篇关于点击气球在图形页面应该开始另一项活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!