在我的地图应用程序中,我在地图上显示一组叠加层.每当我点击叠加层,我需要显示一个弹出窗口,像这样

任何人可以帮我解决这个问题吗?

解决方法

是的,您可以通过点按地图设计我们的自助信息窗口来显示信息,如果您明白,请重播我,我将向您提供代码.
public class MapLocationOverlay  extends Overlay  {

    private boolean isNameAddHold=true;

     private Bitmap bitmap,bitCross,bitMoreinformation;
    int testX,testY,count=0;
    int infoWindowOffsetX,infoWindowOffsetY;
    public String name,address,argName,argAddress,argid;
    //  Store these as global instances so we don't keep reloading every time
    private Bitmap bubbleIcon,shadowIcon;

    private MapLocationViewer mapLocationViewer;
    double toLat,toLng;

    private Paint   innerPaint,borderPaint,textPaint;
    private boolean isRemovePriorPopup=false;
    private boolean temp=true,first=true,firstDraw = true;
    public boolean tempDefaule=true;
    public Moreinformation myMoreinformation;
    public PantryLocation location;

    //  The currently selected Map Location...if any is selected.  This tracks whether an information  
    //  window should be displayed & where...i.e. whether a user 'clicked' on a kNown map location
    private PantryLocation selectedMapLocation;  

    public MapLocationOverlay(MapLocationViewer mapLocationViewer,PantryLocation arglocation) {

        this.mapLocationViewer = mapLocationViewer;
        location=arglocation;
        bubbleIcon = BitmapFactory.decodeResource(mapLocationViewer.getResources(),R.drawable.bubble);
        shadowIcon = BitmapFactory.decodeResource(mapLocationViewer.getResources(),R.drawable.shadow);
        bitmap = BitmapFactory.decodeResource(mapLocationViewer.getResources(),R.drawable.infowindow);
        bitCross = BitmapFactory.decodeResource(mapLocationViewer.getResources(),R.drawable.crass);
        bitMoreinformation = BitmapFactory.decodeResource(mapLocationViewer.getResources(),R.drawable.more_informations_new);
    }

    @Override
    public boolean onTouchEvent(MotionEvent e,MapView mapView) {
        // Todo Auto-generated method stub
         if (e.getAction() == 0)
         {
             this.tempDefaule=false;
         }
         if (e.getAction() == 1)
         {
             this.tempDefaule=false;
         }
         if (e.getAction() == 2)
         {
             this.tempDefaule=false;
         }
        return super.onTouchEvent(e,mapView);
    }       

    @Override
    public boolean onTap(GeoPoint p,MapView    mapView)  {
        //this.tempDefaule=false;
        //  Store whether prior popup was displayed so we can call invalidate() & remove it if necessary.

        //  Next test whether a new popup should be displayed

            selectedMapLocation = getHitMapLocation(mapView,p);

            mapView.invalidate();       

        //  Lastly return true if we handled this onTap()
        return selectedMapLocation != null;
    }

    @Override
    public void draw(Canvas canvas,MapView mapView,boolean shadow) {

        drawMapLocations(canvas,mapView,shadow);
        //this.tempDefaule=false;
        drawInfoWindow(canvas,shadow);
        //this.tempDefaule=false;
        }

    /**
     * Test whether an information balloon should be displayed or a prior balloon hidden.
     */
    private PantryLocation getHitMapLocation(MapView    mapView,GeoPoint   tapPoint) {

        //  Track which MapLocation was hit...if any
        PantryLocation hitMapLocation = null;

        RectF hitTestRecr = new RectF();
        Point screenCoords = new Point();
        ArrayList<PantryLocation> iterator =mapLocationViewer.getMapLocations();
        for(int i=0;i<iterator.size();i++) {       
            PantryLocation testLocation = iterator.get(i);

            //  Translate the MapLocation's lat/long coordinates to screen coordinates
            mapView.getProjection().toPixels(testLocation.getPoint(),screenCoords);

            // Create a 'hit' testing Rectangle w/size and coordinates of our icon
            // Set the 'hit' testing Rectangle with the size and coordinates of our on screen icon
            hitTestRecr.set(-bubbleIcon.getWidth()/2,-bubbleIcon.getHeight(),bubbleIcon.getWidth()/2,0);
            hitTestRecr.offset(screenCoords.x,screenCoords.y);

            //  Finally test for a match between our 'hit' Rectangle and the location clicked by the user
            mapView.getProjection().toPixels(tapPoint,screenCoords);
            //hitMapLocation = testLocation;
            if (hitTestRecr.contains(screenCoords.x,screenCoords.y)) {
                hitMapLocation = testLocation;
                first = true;
                isNameAddHold = true;
                break;
            }
        }
        testX=(int)screenCoords.x;
        testY=(int)screenCoords.y;
        //  Lastly clear the newMouseSelection as it has Now been processed
        tapPoint = null;
        if(hitMapLocation==null && selectedMapLocation!=null)
        return selectedMapLocation; 

        return hitMapLocation;

    }

    private void drawMapLocations(Canvas canvas,MapView    mapView,boolean shadow) {
        int i=0;count++;
        ArrayList<PantryLocation> iterator =mapLocationViewer.getMapLocations();
        Point screenCoords = new Point();
        for(i=0;i<iterator.size();i++) {       
            PantryLocation location = iterator.get(i);
            mapView.getProjection().toPixels(location.getPoint(),screenCoords);
            //mapView.getController().setCenter(location.getPoint());
            if (shadow) {
                //  Only offset the shadow in the y-axis as the shadow is angled so the base is at x=0; 
                canvas.drawBitmap(shadowIcon,screenCoords.x,screenCoords.y - shadowIcon.getHeight(),null);
            } else {
                canvas.drawBitmap(bubbleIcon,screenCoords.x - bubbleIcon.getWidth()/2,screenCoords.y - bubbleIcon.getHeight(),null);
            }

        }
//      if(i==iterator.size()){
//          this.tempDefaule=false;}
if(tempDefaule==true)
{

            PantryLocation location1 = this.location;
            //mapView.getProjection().toPixels(location.getPoint(),screenCoords);
            Point screenCoord = new Point();
            mapView.getController().setCenter(location1.getPoint());
            mapView.getController().setZoom(15);
        //  tempDefaule=false;
                //mapView.getController().setCenter(location.getPoint());   

        }

            //tempDefaule=false;    
}           //mapView.getProjection().toPixels(location.getPoint(),screenCoord);
            //canvas.drawBitmap(bubbleIcon,screenCoord.x - bubbleIcon.getWidth()/2,screenCoord.y - bubbleIcon.getHeight(),null);
            //mapView.invalidate();


    private void drawInfoWindow(Canvas canvas,MapView  mapView,boolean shadow) {
        //this.tempDefaule=false;
        if ( selectedMapLocation != null) {
            if ( shadow) {
                //  Skip painting a shadow in this tutorial
            } else {
                //  First determine the screen coordinates of the selected MapLocation
                isRemovePriorPopup=true;
                Point selDestinationOffset = new Point();
                mapView.getProjection().toPixels(selectedMapLocation.getPoint(),selDestinationOffset);

                //  Setup the info window with the right size & location
                int INFO_WINDOW_WIDTH = 125;
                int INFO_WINDOW_HEIGHT = 25;
                RectF infoWindowRect = new RectF(0,100,20);   
                RectF closeRect = new RectF(0,20,20); 
                 infoWindowOffsetX = selDestinationOffset.x-INFO_WINDOW_WIDTH/2;
                 infoWindowOffsetY = selDestinationOffset.y-INFO_WINDOW_HEIGHT-bubbleIcon.getHeight();
                 infoWindowRect.offset(infoWindowOffsetX+95,infoWindowOffsetY-45);
                 closeRect.offset(infoWindowOffsetX+160,infoWindowOffsetY-90);
                 Paint myPaintBlack=new Paint();
                    Paint myPaintWhite=new Paint();

                    myPaintWhite.setARGB(255,255,255);


                //  Draw inner info window
                canvas.drawRoundRect(infoWindowRect,5,getInnerPaint());

                //  Draw border for info window
                canvas.drawRoundRect(infoWindowRect,getBorderPaint());

                //  Draw the MapLocation's name


                myPaintBlack.setColor(Color.BLACK);
                myPaintBlack.setTextSize(20);

                canvas.drawBitmap(bitmap,infoWindowOffsetX-50,infoWindowOffsetY-100,getInnerPaint());
                if(isNameAddHold)
                {
                argid=selectedMapLocation.getID();
                argName=selectedMapLocation.getName();
                name=selectedMapLocation.getName();
                toLat = selectedMapLocation.getLatitude();
                toLng = selectedMapLocation.getLongitude();
                if(name.length()>18)
                name=selectedMapLocation.getName().substring(0,18)+"..";
                argAddress=selectedMapLocation.getAddress();
                address=selectedMapLocation.getAddress();
                if(address.length()>30)
                address=selectedMapLocation.getAddress().substring(0,30)+"..";
                }
                canvas.drawText(name,infoWindowOffsetX-45,infoWindowOffsetY-70,myPaintBlack);
                myPaintBlack.setTextSize(13);
                canvas.drawText(address,infoWindowOffsetY-55,myPaintBlack);

            //  Draw inner info window
                canvas.drawRoundRect(infoWindowRect,getBorderPaint());
                //  Draw the MapLocation's name
                myPaintBlack.setColor(Color.CYAN);
                myPaintBlack.setTextSize(11);
                //canvas.drawText("Click for more info..",infoWindowOffsetX+105,infoWindowOffsetY-33,myPaintBlack);
                canvas.drawBitmap(bitMoreinformation,infoWindowOffsetX+95,infoWindowOffsetY-45,getInnerPaint());
                //canvas.drawBitmap(bitCross,infoWindowOffsetX+160,infoWindowOffsetY-90,getInnerPaint());

            }
        }
        if(isRemovePriorPopup)
        {
        if(((testX>(infoWindowOffsetX+95)&&testX<(infoWindowOffsetX+195)))&&(testY>(infoWindowOffsetY-45)&&testY<(infoWindowOffsetY-25)))
            {
            if(temp){
                try
                {
                    temp=false;
                    isNameAddHold=false;
                isRemovePriorPopup=false;
                Context context=mapLocationViewer.cn;
                Toast.makeText(context,"Loading...",Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(context,Moreinformation.class);
                intent.putExtra("Id",argid);
                intent.putExtra("Name",argName);
                intent.putExtra("Address",argAddress);
                intent.putExtra("Latitude",toLat);
                intent.putExtra("Longitude",toLng);

                context.startActivity(intent);

                }
                catch (Exception ex)
                {
                    Log.d("Redirected Error :",ex.getMessage());
                }
        //canvas.drawText("This is for Testing",testX,myPaintBlack);
            }}
            else if(((testX>(infoWindowOffsetX+160)&&testX<(infoWindowOffsetX+180)))&&(testY>(infoWindowOffsetY-90)&&testY<(infoWindowOffsetY-70)))
            {

                if(isRemovePriorPopup)
                {
                 isRemovePriorPopup=false;                  
                 selectedMapLocation=null;
                 draw(canvas,shadow);
                // mapLocationViewer.setDZoom();
                }

            }
        }
    }

    public Paint getInnerPaint() {
        if ( innerPaint == null) {
            innerPaint = new Paint();
            innerPaint.setARGB(225,75,75); //gray
            innerPaint.setAntiAlias(true);
        }
        return innerPaint;
    }

    public Paint getBorderPaint() {
        if ( borderPaint == null) {
            borderPaint = new Paint();
            borderPaint.setARGB(255,255);
            borderPaint.setAntiAlias(true);
            borderPaint.setStyle(Style.stroke);
            borderPaint.setstrokeWidth(2);
        }
        return borderPaint;
    }

    public Paint getTextPaint() {
        if ( textPaint == null) {
            textPaint = new Paint();
            textPaint.setARGB(255,255);
            textPaint.setAntiAlias(true);
        }
        return textPaint;
    }


}

在这段代码中,我设计了一个绘制信息窗口的函数

如何在Android中轻击叠加层显示弹出窗口?的更多相关文章

  1. 创建 Swift 自定义集合类

    在本文,你将学习用Swift的collection协议创建自定义集合类型。当文本结束,你会拥有一个强大的自定义集合类型,拥有Swift内置集合的所有功能。小于次的版本无法编译,因为Swift标准库发生了剧烈改变。在一个Set集合中,重复对象会被忽略。Swift提供了让Bag符合传统集合的所有工具。你需要先了解一下在Swift中,让一个对象变成集合需要做些什么。要理解什么是Swift集合,首先需要它继承的协议层次:Sequence协议表示类型支持排序、以迭代的方式访问其元素。

  2. swift – 使用依赖于元素类型的递归属性/方法扩展Collection

    –但调度已关闭:$1.flatCount不绑定到第二个递归版本,但总是绑定到第一个普通版本.也就是说,flatCount仅计算第一个嵌套级别.有没有办法以表达此功能的方式处理类型和/或调度?

  3. 数组 – Swift 2D数组通用扩展 – 访问第二维的问题

    我正在尝试将以下函数转换为2D数组的通用扩展.我特别难以指出如何指定约束以允许我访问第二个维度.这是一次失败的尝试:问题是编译器不知道你的扩展是用于2D数组–它只知道它是用于集合数组.因此,关联类型Indexdistance和Index不一定是Int.因此,解决方案是约束您的扩展,以便Element的Indexdistance和Index属于Int类型.这将允许您形成范围0..

  4. android – MarkerView走出图表的最后一点

    我正在使用MarkerView类在图表中显示标记视图.我创建的markerview布局包含两个textview,一个在另一个之下.我面临的问题是图表上最后一个点的标记视图是图表中的一半,而图表中的一半.下面的两张图片清楚地说明了问题:第一张图显示了图表中心点的标记视图,显示没有任何问题:第二个图像,如下所示,显示图表最后一个点的标记视图,它是图表中的一半.如何调整此标记视图以使其在图表区域内显示.Wiki不会为markerview声明任何自定义.还有更多自定义吗?

  5. android – 当app是后台FCM时,如何检索通知消息intent.getExtras()

    我正在使用FCM进行简单通知当应用程序处于前台时,一切正常.我在onMessageReceived方法中收到通知和数据消息.但是当应用程序处于后台时,我会在系统托盘中收到通知.当我点击控件时,它会转到主要活动.当我解析intent.getExtras();时,我只得到这个关键数据–google.sent_time,from,google.message_id,collapse_key.如何从intent.getExtras()获取系统托盘中可见的通知消息标题和消息?

  6. Android – 使用ORMLite DAO作为ContentProvider

    我旁边的同事真的非常想使用Ormlite,因为他不想自己编写任何映射.我知道atleap和Android-OrmliteContentProvider项目的存在.这些只为活动提供了一个光标,我的同事希望拥有模型列表或单个模型.这可以实现吗?和Contentprovider必须使用模型.但是,使用列表等仍然可以实现相同的功能吗?将事件传递给contentobservers等活动?

  7. 如何在Android中轻击叠加层显示弹出窗口?

    在我的地图应用程序中,我在地图上显示一组叠加层.每当我点击叠加层,我需要显示一个弹出窗口,像这样任何人可以帮我解决这个问题吗?解决方法是的,您可以通过点按地图设计我们的自助信息窗口来显示信息,如果您明白,请重播我,我将向您提供代码.在这段代码中,我设计了一个绘制信息窗口的函数

  8. 深入解读JavaScript中的Iterator和for-of循环

    这篇文章主要介绍了JavaScript中的Iterator和for-of循环,是JS入门学习中的基础知识,需要的朋友可以参考下

  9. Python 迭代器Iterator详情

    这篇文章主要介绍了Python 迭代器Iterator详情,迭代器可以帮助我们解决面对复杂的数据场景时,快速简便的获取数据,下文关于其详细介绍,需要的小伙伴可以参考一下

  10. PHP迭代器接口Iterator用法分析

    这篇文章主要介绍了PHP迭代器接口Iterator用法,结合实例形式分析了PHP迭代器接口Iterator的概念、功能、定义与使用方法,需要的朋友可以参考下

随机推荐

  1. bluetooth-lowenergy – Altbeacon库无法在Android 5.0上运行

    昨天我在Nexus4上获得了Android5.0的更新,并且altbeacon库停止了检测信标.似乎在监视和测距时,didEnterRegion和didRangeBeaconsInRegion都没有被调用.即使RadiusNetworks的Locate应用程序现在表现不同,一旦检测到信标的值,它们就不再得到更新,并且通常看起来好像信标超出了范围.我注意到的一点是,现在在logcat中出现以下行“B

  2. android – react-native动态更改响应者

    我正在使用react-native进行Android开发.我有一个视图,如果用户长按,我想显示一个可以拖动的动画视图.我可以使用PanResponder实现这一点,它工作正常.但我想要做的是当用户长按时,用户应该能够继续相同的触摸/按下并拖动新显示的Animated.View.如果您熟悉Google云端硬盘应用,则它具有类似的功能.当用户长按列表中的任何项目时,它会显示可拖动的项目.用户可以直接拖

  3. android – 是否有可能通过使用与最初使用的证书不同的证书对其进行签名来发布更新的应用程序

    是否可以通过使用与最初使用的证书不同的证书进行签名来发布Android应用程序的更新?我知道当我们尝试将这样的构建上传到市场时,它通常会给出错误消息.但有没有任何出路,比如将其标记为主要版本,指定市场中的某个地方?解决方法不,你不能这样做.证书是一种工具,可确保您是首次上传应用程序的人.所以总是备份密钥库!

  4. 如何检测Android中是否存在麦克风?

    ..所以我想在让用户访问语音输入功能之前检测麦克风是否存在.如何检测设备上是否有麦克风.谢谢.解决方法AndroidAPI参考:hasSystemFeature

  5. Android – 调用GONE然后VISIBLE使视图显示在错误的位置

    我有两个视图,A和B,视图A在视图B上方.当我以编程方式将视图A设置为GONE时,它将消失,并且它正下方的视图将转到视图A的位置.但是,当我再次将相同的视图设置为VISIBLE时,它会在视图B上显示.我不希望这样.我希望视图B回到原来的位置,这是我认为会发生的事情.我怎样才能做到这一点?编辑–代码}这里是XML:解决方法您可以尝试将两个视图放在RelativeLayout中并相对于彼此设置它们的位置.

  6. android – 获得一首歌的流派

    我如何阅读与歌曲相关的流派?我可以读这首歌,但是如何抓住这首歌的流派,它存放在哪里?解决方法检查此代码:

  7. android – 使用textShadow折叠工具栏

    我有一个折叠工具栏的问题,在展开状态我想在文本下面有一个模糊的阴影,我使用这段代码:用:我可以更改textColor,它可以工作,但阴影不起作用.我为阴影尝试了很多不同的值.是否可以为折叠文本投射阴影?

  8. android – 重用arm共享库

    我已经建立了armarm共享库.我有兴趣重用一个函数.我想调用该函数并获得返回值.有可能做这样的事吗?我没有任何头文件.我试过这个Android.mk,我把libtest.so放在/jni和/libs/armeabi,/lib/armeabi中.此时我的cpp文件编译,但现在是什么?我从objdump知道它的名字编辑:我试图用这个android.mk从hello-jni示例中添加prebuild库:它工作,但libtest.so相同的代码显示以下错误(启动时)libtest.so存在于libhello-j

  9. android – 为NumberPicker捕获键盘’Done’

    我有一个AlertDialog只有一些文本,一个NumberPicker,一个OK和一个取消.(我知道,这个对话框还没有做它应该保留暂停和恢复状态的事情.)我想在软键盘或其他IME上执行“完成”操作来关闭对话框,就像按下了“OK”一样,因为只有一个小部件可以编辑.看起来处理IME“Done”的最佳方法通常是在TextView上使用setonEditorActionListener.但我没有任何Te

  10. android – 想要在调用WebChromeClient#onCreateWindow时知道目标URL

    当我点击一个带有target=“_blank”属性的超链接时,会调用WebChromeClient#onCreateWindow,但我找不到新的窗口将打开的新方法?主页url是我唯一能知道的东西?我想根据目标网址更改应用行为.任何帮助表示赞赏,谢谢!

返回
顶部