Android 百度地图 对鼠标点击、移动、抬起三个事件的监听
关键点:
1、MotionEvent.ACTION_DOWN, ACTION_MOVE, ACTION_UP, ACTION_CANCEL, ACTION_OUTSIDE, ACTION_POINTER_DOWN, ACTION_POINTER_UP, EDGE_TOP, EDGE_BOTTOM, EDGE_LEFT, EDGE_RIGHT
2、先获取屏幕坐标,再转换为经纬度坐标。
还未解决的问题有:
1、经纬度坐标精度问题,如何保留两位小数。Double.toString???
2、TextView如何添加到底图上去????尝试过,会出错。
main.java
package com.example.baiduhl; import java.math.BigDecimal; import android.app.Activity; import android.content.Context; import android.content.res.Configuration; import android.os.Bundle; import android.util.Log; import android.view.DragEvent; import android.view.Menu; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.FrameLayout; import android.widget.TextView; import android.widget.Toast; import com.baidu.mapapi.BMapManager; import com.baidu.mapapi.map.MKMapViewListener; import com.baidu.mapapi.map.MapController; import com.baidu.mapapi.map.MapPoi; import com.baidu.mapapi.map.MapView; import com.baidu.platform.comapi.basestruct.GeoPoint; import com.baidu.platform.comapi.map.Projection; public class Main extends Activity { BMapManager mBMapMan = null; MapView mMapView = null; private Button button1; private Button button2; private TextView display; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mBMapMan = new BMapManager(getApplication()); mBMapMan.init("13F11B305D58BA03616000731F6E9F87976CD7C4", null); setContentView(R.layout.display); mMapView = (MapView) findViewById(R.id.bmapsView); mMapView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub // 获得屏幕点击的位置 int x = (int) event.getX(); int y = (int) event.getY(); // 将像素坐标转为地址坐标 Projection getProjection = mMapView.getProjection(); GeoPoint pt = getProjection.fromPixels(x, y); //如何将经纬度坐标保留两位小数????? String longitude = Double.toString(pt.getLongitudeE6()/1000000 ); String latitude = Double.toString(pt.getLatitudeE6()/1000000 ); //double longitude = pt.getLongitudeE6()/1000000 ; switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: display.setText("鼠标按下。当前屏幕坐标是: " + "x: " + x + " y: " + y + '\n' + "当前经纬度坐标是 " + "longitude: " + longitude + " latitude: " + latitude); break; case MotionEvent.ACTION_MOVE: display.setText("拖动地图。当前屏幕坐标是: " + "x: " + x + " y: " + y + '\n' + "当前经纬度坐标是 " + "longitude: " + longitude + " latitude: " + latitude); break; case MotionEvent.ACTION_UP: display.setText("鼠标抬起。最后屏幕坐标是: " + "x: " + x + " y: " + y + '\n' + "经纬度坐标是: " + "longitude: " + longitude + " latitude: " + latitude); break; } /* display.setText("鼠标按下。当前屏幕坐标是: " + "x: " + x + " y: " + y + '\n' + "当前经纬度坐标是 " + "longitude: " + longitude + " latitude: " + latitude); if (event.getAction() == event.ACTION_UP) { display.setText("鼠标抬起。最后屏幕坐标是: " + "x: " + x + " y: " + y + '\n' + "经纬度坐标是: " + "longitude: " + longitude + " latitude: " + latitude); } */ return false; } }); mMapView.setBuiltInZoomControls(true); // 设置启用内部的缩放控件 MapController mMapController = mMapView.getController(); GeoPoint point = new GeoPoint((int) (39.915 * 1E6), (int) (116.404 * 1E6)); // 用给定的经纬度构造一个GeoPoint,单位是微度 (度 * 1E6) mMapController.setCenter(point);// 设置地图中心点 mMapController.setZoom(12);// 设置地图zoom级别 display = (TextView) findViewById(R.id.locationdisplay); /* * mMapView.setOnClickListener(new View.OnClickListener() { * * @Override public void onClick(View v) { // TODO Auto-generated method * stub display.setText("刚刚点击了一下地图啦~~~~"); } }); */ button1 = (Button) findViewById(R.id.buttontraffic); button1.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { if (mMapView.isTraffic() == true) { mMapView.setTraffic(false);// 交通流量 Toast.makeText(Main.this, "traffic is off", Toast.LENGTH_LONG).show(); display.setText("关闭交通流量啦啦~~~~"); } else { mMapView.setTraffic(true); Toast.makeText(Main.this, "traffic is on", Toast.LENGTH_LONG).show(); display.setText("打开交通流量啦~~~~"); } } }); button2 = (Button) findViewById(R.id.buttonsatellite); button2.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { if (mMapView.isSatellite() == true) { mMapView.setSatellite(false); Toast.makeText(Main.this, "satellite is off", Toast.LENGTH_LONG).show(); display.setText("关闭卫星地图啦啦~~~~"); } else { mMapView.setSatellite(true); Toast.makeText(Main.this, "satellite is on", Toast.LENGTH_LONG).show(); display.setText("打开卫星地图啦啦~~~~"); } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.display, menu); return true; } @Override protected void onDestroy() { mMapView.destroy(); if (mBMapMan != null) { mBMapMan.destroy(); mBMapMan = null; } super.onDestroy(); } @Override protected void onPause() { mMapView.onPause(); if (mBMapMan != null) { mBMapMan.stop(); } super.onPause(); } @Override protected void onResume() { mMapView.onResume(); if (mBMapMan != null) { mBMapMan.start(); } super.onResume(); } }
图片预览