Android 开发过程中,有很多手势操作都很让人又爱又恨。一方面可以更便捷的体现更多功能,提升应用的体验,一方面繁复多变的操作背后有着许多开发技巧和难题。这里分享一个长按地图获取位置信息的手势。为了更明了,先上个展示效果:长按地图某点显示该点地理位置信息功能通过构造一个locationSelectOverlay类来定义该功能,在地图上对长按手势进行监听,一旦有这个发生就调用getAddressFromServer()方法来显示地址信息。在该工程中分别定义4个类longPressMap.java,locationSelectOverlay.java,popUpPanel.java,Constants.javalongPressMap.java 为显示一个地图类,通过实例化一个locationSelectOverlay类实现长按
地图显示地理位置信息功能代码如下://longPressMap 类继承MapActivity对mapview资源进行管理public class longPressMap extends MapActivity {private MapView mMapView;locationSelectOverlay mSelectLay;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//使用setContentView方法调用R.layout.activity_regeocoder布局文件,显示地图setContentView(R.layout.geocoder);//获取地图视图的id,赋值给mMapView mMapView = ((MapView) findViewById(R.id.geocode_MapView));// 设置启用内置的缩放控件mMapView.setBuiltInZoomControls(true); //实例化一个locationSelectOverlay类mSelectLay = new locationSelectOverlay(this, mMapView, new popUpPanel(this, mMapView));//将该功能加载到此地图上,启用长按地图显示该点地址信息的功能mMapView.getOverlays().add(mSelectLay);} }复制代码locationSelectOverlay 示例代码如下://locationSelectOverlay类继承Overlay接口,实现OnGestureListener手势监听public class locationSelectOverlay extends Overlay implements OnGestureListener {public popUpPanel mTipPanel; //声明一个弹出框对象GeoPoint mSelectPoint; //声明一个地理坐标点对象MapView mMapView; //声明一个地图视图对象Context mContext; //活动对象TextView mTipText=null; //声明一个文本对象private static String nearbystr="";private GestureDetector gestureScanner; //声明一个手势监听对象private Geocoder coder; //声明一个逆地理编码对象private String addressName=""; //声明一个地址名称字符串//长按地图某点获取信息的构造函数。public locationSelectOverlay(Activity context,MapView mapView,popUpPanel panel){this.mContext=context; this.mMapView=mapView;this.mTipPanel=panel; gestureScanner = new GestureDetector(this); //声明一个手势监听对象coder = new Geocoder(context); //声明一个逆地理编码对象}//用Handler函数处理传递来的地址信息,显示在文本框中private Handler mGeocoderHandler = new Handler(){public void handleMessage(Message msg){//如果有地址信息的消息发送过来,将文本框中设置为该地址信息if(msg.what == Constants.REOCODER_RESULT){if(mTipText!=null)mTipText.setText(addressName);}//如果显示错误,则文本框中设置报错信息else if(msg.what == Constants.ERROR){Toast.makeText(mContext, "获取地址失败,请重试", Toast.LENGTH_SHORT).show();removeTipPanel();}}};//显示弹出窗口public boolean showTap(GeoPoint p) {View view = mTipPanel.getView();mMapView.removeView(view);//布局参数设置MapView.LayoutParams geoLP = new MapView.LayoutParams(MapView.LayoutParams.WRAP_CONTENT,MapView.LayoutParams.WRAP_CONTENT, p,MapView.LayoutParams.BOTTOM_CENTER);//弹出窗口的文本显示mTipText = (TextView) view.findViewById(R.id.GeoName);mTipText.setText("正在加载地址...");mTipText.setOnClickListener(new OnClickListener() {public void onClick(View v) {}});//在地图视图上添加该弹出窗口视图mMapView.addView(view, geoLP);return false;}//从经纬度坐标点获取对应的地址信息public void getAddressFromServer(final GeoPoint nt,final Handler handler){//声明一个线程new Thread(){public void run(){try {// 逆地理编码getFromLocation()函数获取该点对应的前3个地址信息List<Address> address = coder.getFromLocation((double)nt.getLatitudeE6()/1E6,(double)nt.getLongitudeE6()/1E6, 3);if (address != null) {//获取第一个地址信息Address addres = address.get(0);addressName = "";if(addres.getAdminArea()!=null)addressName+=addres.getAdminArea();if(addres.getSubLocality()!=null)addressName += addres.getSubLocality();if(addres.getFeatureName()!=null)addressName += addres.getFeatureName();addressName += "附近";handler.sendMessage(Message.obtain(handler, Constants.REOCODER_RESULT));}} catch (AMapException e) {// TODO Auto-generated catch blockhandler.sendMessage(Message.obtain(handler, Constants.ERROR));}}}.start(); //线程启动}//移走弹出窗口public void removeTipPanel(){ View view = mTipPanel.getView();mMapView.removeView(view);}//获取手势操作public boolean onTouchEvent(MotionEvent event, MapView mapView) {return gestureScanner.onTouchEvent(event);}@Overridepublic boolean onDown(MotionEvent e) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {// TODO Auto-generated method stubreturn false;}//长按地图,弹出提示框,显示该点地址信息@Overridepublic void onLongPress(MotionEvent e) {// TODO Auto-generated method stubint x = (int)e.getX();int y = (int)e.getY();mSelectPoint = mMapView.getProjection().fromPixels(x, y);//调用显示提示框函数showTap(mSelectPoint);//调用从经纬度点获取地址信息函数getAddressFromServer(mSelectPoint,mGeocoderHandler);}@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {// TODO Auto-generated method stubreturn false;}复制代码pouUpPanel定义了弹出窗口类public class popUpPanel{private boolean isVisible = false;private MapView mMapView;private View popup;public popUpPanel(Activity paramActivity, MapView paramMapView){this.mMapView = paramMapView;ViewGroup localViewGroup = (ViewGroup)this.mMapView.getParent();//设置弹出的视图是id为R.layout.activity_long_press_map的视图this.popup = paramActivity.getLayoutInflater().inflate(R.layout.activity_long_press_map, localViewGroup, false);…复制代码Constants定义了传递的常量对应的值,如public static finalint REOCODER_RESULT=3000; 表示逆地理编码结果常量,public staticfinal int ERROR=1001; 表示出现错误常量。