1

我正在创建一个应用程序,我需要在地图上显示一些带有大头针的位置。当用户点击大头针时,它应该在地图上显示一个弹出对话框以显示有关该位置的一些信息。一切正常,但是当我放大/缩小地图弹出对话框移动很远/从/靠近销。

 package com.wheelz.util;

import java.util.ArrayList;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;
import com.wheelz.R;
import com.wheelz.fragment.CarHomeZoneFragment;
import com.wheelz.fragmentactivity.SearchFragmentActivity;

public class HoemZoneOverlay extends com.google.android.maps.ItemizedOverlay {
    private static final String TAG ="MyOVerlayItem";
    GeoPoint p;
    MapView map;
    private LinearLayout popUpLayout;
    private ArrayList<OverlayItem> aList = null;
//  CarHomeZoneFragment homeZoneFrag;
     Paint circlePaint;
     Paint circleBorder;
     String CarName;
     String OwnerName;
    public HoemZoneOverlay(Drawable defaultMarker,MapView mView, GeoPoint point, String carname, String ownername) {
        super(boundCenterBottom(defaultMarker));
        // TODO Auto-generated constructor stub
        //homeZoneFrag = carHomeZoneFragment;
        this.map = mView;
        this.p = point;
        this.CarName    =   carname;
        this.OwnerName  =   ownername;
         aList          =   new ArrayList<OverlayItem>(); 
        circlePaint = new Paint( Paint.ANTI_ALIAS_FLAG );
              circlePaint.setARGB(51,62, 167, 171);

            //  circlePaint.setColor(new Color);                      
              circlePaint.setStrokeCap(Paint.Cap.ROUND);
              circlePaint.setAntiAlias(true);
              circlePaint.setStyle(Paint.Style.FILL_AND_STROKE);
              circlePaint.setDither(false);

        circleBorder = new Paint();
        circleBorder.setARGB(255, 62, 167, 171);
        circleBorder.setStrokeWidth(2);
        circleBorder.setStrokeCap(Paint.Cap.ROUND);
        circleBorder.setAntiAlias(true);
        circleBorder.setDither(false);
        circleBorder.setStyle(Paint.Style.STROKE);



            /*  circleBorder = new Paint();
              circleBorder.setARGB(0, 62, 167, 171);
              circleBorder.setStrokeWidth(2);
              circleBorder.setStyle(Paint.Style.STROKE);*/



    }

    @Override
    protected OverlayItem createItem(int i) {
        // TODO Auto-generated method stub
        return  aList.get(i);
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return aList.size();
    }
    public void addOverlay(OverlayItem overlay){
        aList.add(overlay);
        populate();
    }

    public boolean onTap (final GeoPoint point, final MapView map){
        boolean tapped = super.onTap(point, map);
        if (tapped){
            if(  map.getChildCount() <= 0 )
           showPopUp(p,map);        
        }           
        else{
            hidePopUp();
            }                   
        return true;
    }

    //You must have this method, even if it doesn't visibly do anything

    @Override
    protected boolean onTap(int index) {

        return true;
    }


     @Override
      public boolean draw(Canvas canvas, MapView mapView, 
      boolean shadow, long when) 
      {
          super.draw(canvas, mapView, shadow);                   

          //---translate the GeoPoint to screen pixels---

          /*Point screenPts = new Point();
          p = aList.get(0).getPoint();
          mapView.getProjection().toPixels(p, screenPts);*/

          //--------------draw circle----------------------            

          Point pt = mapView.getProjection().toPixels(p,null);
          float radius =   mapView.getProjection().metersToEquatorPixels(200);
        //  float radius = (float) Math.pow(2, mapView.getZoomLevel() - 9);


         /* if(radius < canvas.getHeight()/25){
              radius = canvas.getHeight()/25;
          }*/
        //  canvas.drawCircle(screenPts.x, screenPts.y, radius, circlePaint);           
         canvas.drawCircle(pt.x, pt.y, radius, circlePaint);
         canvas.drawCircle(pt.x, pt.y, radius, circleBorder);
          //---add the marker---
        /*  Bitmap bmp = BitmapFactory.decodeResource(
              WheelzApplication.getInstance().getResources(), R.drawable.marker);            
          canvas.drawBitmap(bmp, screenPts.x, screenPts.y-bmp.getHeight(), null);    */        
          super.draw(canvas,mapView,shadow);

          return true;

      }
    /*public boolean onTouchEvent(MotionEvent event,MapView map){
        //---when user lifts his finger---
        if(event.getAction()==1){

                if( homeZoneFrag.popUpLayout != null ){
                    homeZoneFrag.hidePopUp();
                }
            return true;
        }
        else
            return false;

    }*/

     public void showPopUp( GeoPoint gp,MapView map ){
            System.out.println("coming in showpopup####################################  :"+gp);
            //GeoPoint gp= item.getPoint();
            Point p= new Point();
            Projection projection= map.getProjection();
            p = projection.toPixels(gp, p);
            p.y-=50;
            GeoPoint g= projection.fromPixels(p.x, p.y);

            popUpLayout = (LinearLayout) LayoutInflater.from(SearchFragmentActivity.mContext).inflate(R.layout.popup, null);
            final MapView.LayoutParams lp2 = new MapView.LayoutParams(
            MapView.LayoutParams.WRAP_CONTENT,
            MapView.LayoutParams.WRAP_CONTENT,
            g,
            MapView.LayoutParams.BOTTOM_CENTER);
            String Car_Owner_Name = (OwnerName+"'s "+CarName).toString();
            TextView name = ( TextView )popUpLayout.findViewById(R.id.car_owner_name_homezone);
                     name.setText( Car_Owner_Name );


            map.addView(popUpLayout,lp2);
            map.postInvalidate();
            map.getController().animateTo(gp);
            }

     public void hidePopUp() {
            System.out.println("onclick listener hide popup");
            if( map != null ){
            map.removeView( popUpLayout );

            map.invalidate();
            }

        }

     public void cleanUp(){
         if( map != null ){
             popUpLayout = null;
             map.removeAllViews();
         }
          //homeZoneFrag    = null  ;
          circlePaint   = null  ;
          circleBorder  = null  ;
          CarName       = null  ;
          OwnerName     = null  ;
     }
}

任何人都可以告诉我,即使在放大/缩小地图后,我如何才能始终在图钉上方弹出对话框。

谢谢

4

0 回答 0