android---高德地图(3)点击获得目的经纬度,地理编码

点击获得目的地经纬度,利用经纬度将目的地编码获得目的地的名称
package com.amapv2.apis.basic;  //包名,需要修改
 
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.TextView;
import com.amap.api.maps.AMap;
import com.amap.api.maps.AMap.OnCameraChangeListener;
import com.amap.api.maps.AMap.OnMapClickListener;
import com.amap.api.maps.AMap.OnMapLongClickListener;
import com.amap.api.maps.AMap.OnMapTouchListener;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.VisibleRegion;
import com.amapv2.apis.R;
import com.amapv2.apis.util.Constants;
import com.amapv2.apis.util.ToastUtil;
 
/**
 * AMapV2地图中简单介绍OnMapClickListener, OnMapLongClickListener,
 * OnCameraChangeListener、OnMapTouchListener四种监听器用法
 */
 
public class EventsActivity extends Activity implements OnMapClickListener,
        OnMapLongClickListener, OnCameraChangeListener,OnMapTouchListener,OnGeocodeSearchListener{
    private AMap aMap;
    private MapView mapView;
    private TextView mTapTextView;
    private TextView mCameraTextView;
    private TextView mTouchTextView;
  private String destination; //目的地的字符串
private  GeocodeSearch geocoderSearch; //地理编码
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.events_activity);
        mapView = (MapView) findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);// 此方法必须重写
   //地理编码事件监听
        geocoderSearch = new GeocodeSearch(this);
        geocoderSearch.setOnGeocodeSearchListener(this);
        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);
        mTouchTextView = (TextView) findViewById(R.id.touch_text);
    }
 
    /**
     * amap添加一些事件监听器
     */
    private void setUpMap() {
         
        aMap.setOnMapClickListener(this);// 对amap添加单击地图事件监听器
        aMap.setOnMapLongClickListener(this);// 对amap添加长按地图事件监听器
        aMap.setOnCameraChangeListener(this);// 对amap添加移动地图事件监听器
        aMap.setOnMapTouchListener(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, "上海市超出地图当前可见区域");
        }
    }
    /**
     * 对触摸地图事件回调
     */
    @Override
    public void onTouch(MotionEvent event) {
          
        mTouchTextView.setText("触摸事件:屏幕位置"+event.getY()+" "+event.getY());
    }
  //地理逆编码回调函数
@Override
public void onGeocodeSearched(GeocodeResult arg0, int arg1) {
// TODO Auto-generated method stub
}
//地理编码回调函数
@Override
public void onRegeocodeSearched(RegeocodeResult result, int resultcode) {
// TODO Auto-generated method stub
if(resultcode == 0)
{
if(result != null && result.getRegeocodeAddress() != null 
&& result.getRegeocodeAddress().getFormatAddress() != null)
{
destination = result.getRegeocodeAddress().getFormatAddress();         //获得目的地名称
}
}
}

你可能感兴趣的:(Android)