Android 调用高德API,添加浮层,指定范围触发点击事件(添加了自定义的浮层View)

来源:互联网 发布:淘宝联盟什么时候结算 编辑:程序博客网 时间:2024/05/18 20:13
最近做的app中有这个功能,看了阵子API,结合了一下网上一些Demo,自己写了下这种功能如何实现,为自己做个笔记,菜鸟成长ing
哈哈,要养成这个写博客的习惯,大家一起努力

功能: 指定经纬度,添加图片,在图片范围内点击弹出浮层,点击其他地方浮层消失 

MyOverlay中myView就是自定义的View

上代码

public class MapOfMeetingActivity extends MapActivity implements OnClickListener{
private Button btnBack;// 返回按钮
private MapView mapView; 
private PoiOverlay poiOverlay; 
        private MapController mController; 
        private int zoomLevel = 14;
    
    private GeoPoint point;
private MyLocationOverlay mLocationOverlay;
    
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.meetingmap); 
        
        mapView = (MapView) findViewById(R.id.mapView); // 得到MapView对象         
        mapView.setBuiltInZoomControls(true); // 开启缩放控件  
        mController = mapView.getController(); // 获取地图控制对象    
        mController.setZoom(zoomLevel); // 设置初始缩放级别  
  
        MyLocationOverlay mylocTest = new MyLocationOverlay(MapOfMeetingActivity.this, mapView);
        mylocTest.enableMyLocation();
        mylocTest.enableCompass(); // 打开指南针
        mapView.getOverlays().add(mylocTest); 
        
        
        btnBack = (Button) findViewById(R.id.btnBack);
        btnBack.setOnClickListener(this);
        
        GeoPoint gp = new GeoPoint((int) (39.955441 * 1E6), 
              (int) (116.441807 * 1E6)); // 定位到豪成大厦,左参数为纬度,右参数为经度,单位为微度(1度=10^6微度) 
  
        mController.setCenter(gp); // 设置为地图中心  
  
        List<Overlay> list = mapView.getOverlays(); // 获取图层列表  
  
        MyOverlay demo = new MyOverlay(this, mapView, new PopUpPanel(this, mapView)); // 新建自定义Overlay  
  
        list.add(demo); // 往列表添加自定义图层  
        
        mLocationOverlay = new MyLocationOverlay(this, mapView);
        mapView.getOverlays().add(mLocationOverlay);
//实现初次定位使定位结果居中显示
// mLocationOverlay.runOnFirstFix(new Runnable() {
//            public void run() {
//             handler.sendMessage(Message.obtain(handler, Constants.FIRST_LOCATION));
//            }
//        });
        
    } 
    
    class MyOverlay extends Overlay implements OnGestureListener{
    Context context;
    TextView mTipText;
    LinearLayout info_layout;// 地点介绍布局
    PopUpPanel mTipPanel;    // 声明一个弹出框对象
    MapView mMapView;            // 声明一个地图视图对象
    GeoPoint mSelectPoint;          // 声明一个地理坐标点对象
    GestureDetector gestureScanner; // 声明一个手势监听对象
    View myView;// 自定义View
    int bitmapWidth = 0;// 地图上图标的宽度
    int bitmapHeight = 0;// 地图上图片的高度
    int drawX = 0;
    int drawY = 0;
    public MyOverlay(){
    super();
    }
   
    public MyOverlay(Context c, MapView mMapView, PopUpPanel panel){
    this();
    context = c;
    this.mTipPanel = panel;
    gestureScanner = new GestureDetector(this);
    this.mMapView = mMapView;
    myView = getLayoutInflater().inflate(R.layout.positioninfo, null);
    info_layout = (LinearLayout) myView.findViewById(R.id.info_layout);
    }


    public void draw(Canvas canvas, MapView mapView, boolean shadow) { 
     
            Projection proj = mapView.getProjection(); // 获取投影对象 
            Point mPoint = new Point(); 

            GeoPoint gp = new GeoPoint((int) (39.955441 * 1E6), 
                    (int) (116.441807 * 1E6)); 
            proj.toPixels(gp, mPoint); // 将经纬度转换成手机屏幕上的像素,存储在Point对象中 
            Paint mPaint = new Paint(); 

            Bitmap pic = BitmapFactory.decodeResource(getResources(), 
                    R.drawable.map_coordinate); // 得到Bitmap对象 
            
            bitmapWidth = pic.getWidth();
            bitmapHeight = pic.getHeight();
            drawX = mPoint.x;
            drawY = mPoint.y;
            
            canvas.drawBitmap(pic, drawX, drawY, mPaint); // 绘图 
            super.draw(canvas, mapView, shadow); 
        }
   
public boolean onDown(MotionEvent e) {
return false;
}


public void onShowPress(MotionEvent e) {

}


public boolean onSingleTapUp(MotionEvent e) {
return false;
}


public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
return false;
}


/************************
* 长按屏幕触发的事件
*/
public void onLongPress(MotionEvent e) {

}


public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return false;
}


/**************************************
* 点击屏幕触发的事件
*/
public boolean onTouchEvent(MotionEvent e, MapView mapView) {
int x = (int) e.getX();
    int y = (int) e.getY();
    mSelectPoint = mapView.getProjection().fromPixels(x, y);
    BWLog.d("---------点击屏幕的x,y------->>" + x +"////" + y);
    if (e.getAction() == MotionEvent.ACTION_DOWN){
    if (x > drawX && x < drawX+bitmapWidth && y >drawY && y<drawY+bitmapHeight){
//     Toast.makeText(context, "现在地图的缩放级别是" + mapView.getZoomLevel(), Toast.LENGTH_LONG).show();
    showTap(mSelectPoint);
    }else {
    removeTipPanel();
    }
    }
return gestureScanner.onTouchEvent(e);
}
   
//移走弹出窗口
   public void removeTipPanel(){     
          mMapView.removeView(myView);
   }

//显示弹出窗口
   public boolean showTap(GeoPoint p) {
      mMapView.removeView(myView);
      //布局参数设置
      MapView.LayoutParams geoLP = new MapView.LayoutParams(
      MapView.LayoutParams.WRAP_CONTENT,
      MapView.LayoutParams.WRAP_CONTENT, p,
      MapView.LayoutParams.BOTTOM_CENTER);
      //弹出窗口的文本显示
      mTipText = (TextView) myView.findViewById(R.id.meetingInfo);
      
      //在地图视图上添加该弹出窗口视图

      mMapView.addView(myView, geoLP);

      info_layout.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MapOfMeetingActivity.this, MeetingMapActivity.class);
startActivity(intent);
}
});
      return false;
   }
    }


public void onClick(View v) {
switch(v.getId()){
case R.id.btnBack:
finish();
break;
}
}

@Override
protected void onPause() {
    this.mLocationOverlay.disableMyLocation();
super.onPause();
}


@Override
protected void onResume() {
this.mLocationOverlay.enableMyLocation();
super.onResume();
}

}


功能实现了,其实也不是很懂,所以记录下,注释都是根据自己理解写出来的再就是网上Demo里带的了,有不对的地方希望各位大牛指出来

上个图片看看效果 

点击地图上的标记



原创粉丝点击