原地址:http://blog.csdn.net/binzuo/article/details/46925385
- /**
- * AMapV1地图中简单介绍OnMapClickListener, OnMapLongClickListener,
- * OnCameraChangeListener三种监听器用法
- */
- public class EventsActivity extends Activity implements OnMapClickListener,
- OnMapLongClickListener, OnCameraChangeListener {
- private AMap aMap;
- private MapView mapView;
- private TextView mTapTextView;
- private TextView mCameraTextView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.events_activity);
- mapView = (MapView) findViewById(R.id.map);
- mapView.onCreate(savedInstanceState);// 此方法必须重写
- init();
- }
- /**
- * 初始化AMap对象
- */
- private void init() {
- if (aMap == null) {
- aMap = mapView.getMap();
- setUpMap();
- }
- mTapTextView = (TextView) findViewById(R.id.tap_text);
- mCameraTextView = (TextView) findViewById(R.id.camera_text);
- }
- /**
- * amap添加一些事件监听器
- */
- private void setUpMap() {
- aMap.setOnMapClickListener(this);// 对amap添加单击地图事件监听器
- aMap.setOnMapLongClickListener(this);// 对amap添加长按地图事件监听器
- aMap.setOnCameraChangeListener(this);// 对amap添加移动地图事件监听器
- }
- /**
- * 方法必须重写
- */
- @Override
- protected void onResume() {
- super.onResume();
- mapView.onResume();
- }
- /**
- * 方法必须重写
- */
- @Override
- protected void onPause() {
- super.onPause();
- mapView.onPause();
- }
- /**
- * 方法必须重写
- */
- @Override
- protected void onSaveInstanceState(Bundle outState) {
- super.onSaveInstanceState(outState);
- mapView.onSaveInstanceState(outState);
- }
- /**
- * 方法必须重写
- */
- @Override
- protected void onDestroy() {
- super.onDestroy();
- mapView.onDestroy();
- }
- /**
- * 对单击地图事件回调
- */
- @Override
- public void onMapClick(LatLng point) {
- mTapTextView.setText("tapped, point=" + point);
- }
- /**
- * 对长按地图事件回调
- */
- @Override
- public void onMapLongClick(LatLng point) {
- mTapTextView.setText("long pressed, point=" + point);
- }
- /**
- * 对正在移动地图事件回调
- */
- @Override
- public void onCameraChange(CameraPosition cameraPosition) {
- mCameraTextView.setText("onCameraChange:" + cameraPosition.toString());
- }
- /**
- * 对移动地图结束事件回调
- */
- @Override
- public void onCameraChangeFinish(CameraPosition cameraPosition) {
- mCameraTextView.setText("onCameraChangeFinish:"
- + cameraPosition.toString());
- VisibleRegion visibleRegion = aMap.getProjection().getVisibleRegion(); // 获取可视区域、
- LatLngBounds latLngBounds = visibleRegion.latLngBounds;// 获取可视区域的Bounds
- boolean isContain = latLngBounds.contains(Constants.SHANGHAI);// 判断上海经纬度是否包括在当前地图可见区域
- if (isContain) {
- ToastUtil.show(EventsActivity.this, "上海市在地图当前可见区域内");
- } else {
- ToastUtil.show(EventsActivity.this, "上海市超出地图当前可见区域");
- }
- }
- }
利用地图的移动事件,可以得到在onCameraChange得到其可视化区域的中心点,minToCenter=comDistance(distances);,将一组地点的经纬度与之比较,得到最靠近的点。
- /**
- * 计算一组数据中的最小
- * @param distances
- * @return
- */
- double comDistance(double[] distances) {
- int size = distances.length;
- for (int a = 1; a < size; a++) {
- Log.d("hjb 各省距离中心点--", distances[a]+"");
- for (int b = 0; b < size - a; b++) {
- if (distances[b] > distances[b + 1]) {
- double temp = distances[b];
- distances[b] = distances[b + 1];
- distances[b + 1] = temp;
- }
- }
- }
- Log.d("hjb ", "max: " + distances[size-1] + " min: " + distances[0]);
- return distances[0];
- }
- /**
- * 求出地图上某地与地图可视化区域内中心点的直线距离 ,米
- * @param city
- * @return
- */
- private double distance(LatLng city) {
- // TODO Auto-generated method stub
- // 获取当前地图中心点的坐标
- LatLng mTarget = mAMap.getCameraPosition().target;
- Log.d("hjb mTarget--", mTarget.toString());
- Log.d("hjb city--", city.toString());
- Log.d("hjb 距离中心点--", KitLocationUtil.gps2m(mTarget.latitude, mTarget.longitude, city.latitude, city.longitude)+"米");
- return KitLocationUtil.gps2m(mTarget.latitude, mTarget.longitude, city.latitude, city.longitude);
- }