Android 通过高德地图选取地址 高德地图怎么选址_低功耗

我们需要完成如图的功能。

分4步:

首先先看布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <View style="@style/separation_line"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/banner_bg">

        <RelativeLayout
            android:id="@+id/lay_back"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/common_selector_default"
            android:paddingEnd="8dp"
            android:paddingLeft="14dp"
            android:paddingRight="8dp"
            android:paddingStart="14dp">

            <ImageView
                android:id="@+id/iv_back"
                android:layout_width="18dp"
                android:layout_height="18dp"
                android:layout_centerVertical="true"
                android:background="@drawable/common_ic_back"/>

        </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="8dp"
            android:background="@drawable/common_shape_search_view_bg"
            android:gravity="center"
            android:orientation="vertical">

            <AutoCompleteTextView
                android:id="@+id/et_search"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:background="@null"
                android:drawableLeft="@drawable/common_icon_search"
                android:drawablePadding="5dp"
                android:hint="搜索地点"
                android:imeOptions="actionSearch"
                android:padding="10dp"
                android:singleLine="true"
                android:textColor="@color/gray_66"
                android:textSize="16sp"/>
        </LinearLayout>
    </LinearLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <com.amap.api.maps.MapView
            android:id="@+id/mapView"
            android:clickable="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@mipmap/app_loc"
            android:translationY="-10dp" />
    </FrameLayout>

    <RelativeLayout
        android:background="@color/gray_dd"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp">

        <TextView
            android:id="@+id/address"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/ensure"
            android:layout_toLeftOf="@+id/ensure"
            android:maxLines="1"
            android:text=""
            android:textColor="#333333"
            android:textSize="13dp"
            android:visibility="gone"/>

        <TextView
            android:id="@+id/addressDesc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/ensure"
            android:layout_centerVertical="true"
            android:maxLines="1"
            android:textColor="#999999"
            android:textSize="12dp" />

        <Button
            android:id="@+id/btn_ensure"
            android:layout_width="50dp"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="15dp"
            android:background="@drawable/common_selector_green_button"
            android:text="确定"
            android:textColor="#ffffff"
            android:textSize="13dp" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/ll_poi"
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <ListView
            android:id="@+id/lv_data"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></ListView>

    </LinearLayout>

</LinearLayout>

首先进来是定位自己的位置。
这里封装了一下地图的定位

ALocationClientFactory类

public class ALocationClientFactory {

    public static final String DEFAULT_TAG = "Default";

    public static AMapLocationClientOption createDefaultOption() {
        AMapLocationClientOption option = new AMapLocationClientOption();
        //设置定位模式为高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式
        option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置定位间隔,单位毫秒,默认为2000ms,最低1000ms。
        option.setInterval(1000);
        return option;
    }

    public static AMapLocationClientOption createOnceOption() {
        AMapLocationClientOption option = new AMapLocationClientOption();
        //设置定位模式为高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式
        option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //获取一次定位结果:
        //该方法默认为false。
//        option.setOnceLocation(true);
        //获取最近3s内精度最高的一次定位结果:
        //设置setOnceLocationLatest(boolean b)接口为true,启动定位时SDK会返回最近3s内精度最高的一次定位结果。如果设置其为true,setOnceLocation(boolean b)接口也会被设置为true,反之不会,默认为false。
        option.setOnceLocationLatest(true);
        return option;
    }

    public static AMapLocationClient createLocationClient(Context context, AMapLocationClientOption option, AMapLocationListener listener) {
        AMapLocationClient client = new AMapLocationClient(context);
        client.setLocationOption(option);
        client.setLocationListener(listener);
        return client;
    }

    public static AMapLocationClient createDefaultLocationClient(Context context) {
        return createLocationClient(context, createDefaultOption(), new EventBusLocationListener(DEFAULT_TAG));
    }



}

新建类SearchLocationActivity。

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.autonavi.amap.mapcore.ERROR_CODE;
import com.xw.changba.bus.R;
import com.xw.changba.bus.ui.BaseActivity;
import com.xw.changba.bus.ui.demand.entity.LocationBean;
import com.xw.vehicle.mgr.common.AppUtil;
import com.xw.vehicle.mgr.ext.amap.location.ALocationClientFactory;
import com.xw.vehicle.mgr.ext.amap.location.GeoCoderUtil;
import com.xw.vehicle.mgr.ext.amap.model.LatLngEntity;

import java.util.ArrayList;
import java.util.List;

public class SearchLocationActivity extends BaseActivity implements View.OnClickListener, AMapLocationListener, AdapterView.OnItemClickListener, TextWatcher, AMap.OnCameraChangeListener {
    private MapView mMapView = null;
    private AMap aMap;
    private AMapLocationClient locationClient;
    
    private ImageView iv_back;
    private TextView tvAddressDesc;
    private ListView lv_data;
    private AutoCompleteTextView autotext;
    private LinearLayout ll_poi;
    private PoiAdapter poiAdapter;
    private LocationBean currentLoc;
    
    	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.app_activity_search);
		initViews(savedInstanceState);
		showLoadingDialog();
		//定位初始化
		locationClient = ALocationClientFactory.createLocationClient(this, ALocationClientFactory.createDefaultOption(), this);
		//开启定位
		locationClient.startLocation();
	}
	
	private void initViews(Bundle savedInstanceState) {
		mMapView = (MapView) findViewById(R.id.mapView);
		mMapView.onCreate(savedInstanceState); // 此方法必须重写
		aMap = mMapView.getMap();
		UiSettings uiSettings = aMap.getUiSettings();
		uiSettings.setZoomControlsEnabled(false);  //隐藏缩放按钮
		aMap.setOnCameraChangeListener(this); // 添加移动地图事件监听器

		findViewById(R.id.btn_ensure).setOnClickListener(this);
		ll_poi = (LinearLayout) findViewById(R.id.ll_poi);
		iv_back = (ImageView) findViewById(R.id.iv_back);
		autotext =(AutoCompleteTextView) findViewById(R.id.et_search);
		autotext.addTextChangedListener(this);
		autotext.setOnItemClickListener(this);
		iv_back.setOnClickListener(this);
		tvAddressDesc = (TextView) findViewById(R.id.addressDesc);
		lv_data = (ListView) findViewById(R.id.lv_data);
		poiAdapter = new PoiAdapter(this);
		lv_data.setOnItemClickListener(this);
		lv_data.setAdapter(poiAdapter);
	}
	
	@Override
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.iv_back:
				finish();
				break;
			case R.id.btn_ensure:
				if(currentLoc != null){
					Intent intent = new Intent();
					intent.putExtra(EXTRA_DATA, currentLoc);
					setResult(RESULT_OK, intent);
					finish();
				}
				break;
		}
	}
	
	@Override
	public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		if (s.length() <= 0) {
			return;
		}else {
		    //高德地图的输入的自动提示,代码在后面
			InputTipTask.getInstance(this).setAdapter(autotext).searchTips(s.toString().trim(), "");
		}
	}

	@Override
	public void afterTextChanged(Editable editable) {

	}
	
	@Override
	public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
		if(adapterView.getId() == R.id.lv_data){
		    //POI的地址的listview的item的点击
			LocationBean bean = (LocationBean) poiAdapter.getItem(i);
			Intent intent = new Intent();
			intent.putExtra(EXTRA_DATA, bean);
			setResult(RESULT_OK, intent);
			finish();
		}else{
		    //自动提示的内容的item的点击
		    //保存自动提示的列表的内容,供后面使用
			List<LocationBean> dataLists = InputTipTask.getInstance(SearchLocationActivity.this).getBean();
			//点击后地图中心移动
			aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(dataLists.get(i).getLat(),dataLists.get(i).getLon()), 15));
		}
	}
	
	/**定位的回调*/
	@Override
	public void onLocationChanged(AMapLocation aMapLocation) {
		dismissLoadingDialog();
		if (aMapLocation != null && aMapLocation.getErrorCode() == ERROR_CODE.ERROR_NONE) {
		    //移动地图中心到当前的定位位置
			aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude()), 15));
			//获取定位信息
			double latitude = aMapLocation.getLatitude();
			double longitude = aMapLocation.getLongitude();
			String addressStr = aMapLocation.getCity() + aMapLocation.getDistrict() + aMapLocation.getStreet();
			if (!TextUtils.isEmpty(aMapLocation.getAoiName())) {
				addressStr += aMapLocation.getAoiName();
			}else {
				addressStr = addressStr + aMapLocation.getPoiName() + "附近";
			}
			resultAddress = addressStr;
			tvAddressDesc.setText(addressStr);
			//这里是定位完成之后开始poi的附近搜索,代码在后面
			PoiSearchTask.getInstance(this).setAdapter(poiAdapter).onSearch("", "",latitude,longitude);
		}
	}
	
	/**正在移动地图事件回调*/
		@Override
	public void onCameraChange(CameraPosition cameraPosition) {

	}

    /** 移动地图结束事件的回调*/
	@Override
	public void onCameraChangeFinish(final CameraPosition cameraPosition) {
		LatLngEntity latLngEntity = new LatLngEntity(cameraPosition.target.latitude, cameraPosition.target.longitude);
		//地理反编码工具类,代码在后面
		GeoCoderUtil.getInstance(SearchLocationActivity.this).geoAddress(latLngEntity, new GeoCoderUtil.GeoCoderAddressListener() {
			@Override
			public void onAddressResult(String result) {
				if(!autotext.getText().toString().trim().equals("")){
				    //输入地址后的点击搜索
					tvAddressDesc.setText(autotext.getText().toString().trim());
					currentLoc = new LocationBean(cameraPosition.target.longitude,cameraPosition.target.latitude,autotext.getText().toString().trim(),"");
				}else{
				    //拖动地图
					tvAddressDesc.setText(result);
					currentLoc = new LocationBean(cameraPosition.target.longitude,cameraPosition.target.latitude,result,"");
				}
                //地图的中心点位置改变后都开始poi的附近搜索
				PoiSearchTask.getInstance(SearchLocationActivity.this).setAdapter(poiAdapter).onSearch("", "",cameraPosition.target.latitude,cameraPosition.target.longitude);
			}
		});
	}
	
		@Override
	protected void onResume() {
		super.onResume();
		mMapView.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
		mMapView.onPause();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		mMapView.onSaveInstanceState(outState);
	}

	@Override
	protected void onDestroy() {
		mMapView.onDestroy();
		if (locationClient != null) {
			locationClient.stopLocation();
			locationClient.onDestroy();
		}
		super.onDestroy();
	}
	
	//POI搜索显示地址adapter
    class PoiAdapter extends BaseAdapter {
    
    	private List<LocationBean> datas = new ArrayList<>();
    
    	private static final int RESOURCE = R.layout.app_list_item_poi;
    
    	public PoiAdapter(Context context) {}
    
    	@Override
    	public int getCount() {
    		return datas.size();
    	}
    
    	@Override
    	public Object getItem(int position) {
    		return datas.get(position);
    	}
    
    	@Override
    	public long getItemId(int position) {
    		return position;
    	}
    
    	@Override
    	public View getView(int position, View convertView, ViewGroup parent) {
    		ViewHolder vh = null;
    		if(convertView == null){
    			vh = new ViewHolder();
    			convertView = getLayoutInflater().inflate(RESOURCE, null);
    			vh.tv_title = (TextView) convertView.findViewById(R.id.address);
    			vh.tv_text = (TextView) convertView.findViewById(R.id.addressDesc);
    			convertView.setTag(vh);
    		}else{
    			vh = (ViewHolder) convertView.getTag();
    		}
    		LocationBean bean = (LocationBean) getItem(position);
    		vh.tv_title.setText(bean.getTitle());
    		vh.tv_text.setText(bean.getContent());
    		return convertView;
    	}
    
    	private class ViewHolder{
    		public TextView tv_title;
    		public TextView tv_text;
    	}
    
    	public void setData(List<LocationBean> datas){
    		dismissLoadingDialog();
    		this.datas = datas;
    		if(datas.size()>0){
    			ll_poi.setVisibility(View.VISIBLE);
    		}else{
    			AppUtil.showToast(SearchLocationActivity.this,"没有搜索结果");
    			ll_poi.setVisibility(View.GONE);
    		}
    	}
    }
}

封装的地图的搜索自动提示类InputTipTask

import android.content.Context;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.InputtipsQuery;
import com.amap.api.services.help.Tip;
import com.xw.changba.bus.ui.demand.entity.LocationBean;

import java.util.ArrayList;
import java.util.List;

public class InputTipTask implements Inputtips.InputtipsListener {

	private static InputTipTask mInstance;
	private Inputtips mInputTips;
	private Context mContext;
	private AutoCompleteTextView et;
	private List<LocationBean> dataLists = new ArrayList<>();

	private InputTipTask(Context context) {
		this.mContext = context;
	}

    //单例模式
	public static InputTipTask getInstance(Context context) {
		if (mInstance == null) {
			synchronized (InputTipTask.class) {
				if (mInstance == null) {
					mInstance = new InputTipTask(context);
				}
			}
		}
		return mInstance;
	}

	public InputTipTask setAdapter(AutoCompleteTextView et) {
		this.et = et;
		return this;
	}

	public List<LocationBean> getBean() {
		return dataLists;
	}

	public void searchTips(String keyWord, String city) {
		//第二个参数默认代表全国,也可以为城市区号
		InputtipsQuery inputquery = new InputtipsQuery(keyWord, city);
		inputquery.setCityLimit(true);
		mInputTips = new Inputtips(mContext, inputquery);
		mInputTips.setInputtipsListener(this);
		mInputTips.requestInputtipsAsyn();
	}

	@Override
	public void onGetInputtips(final List<Tip> tipList, int rCode) {
		if (rCode == 1000) {
			ArrayList<String> datas = new ArrayList<>();
			if(tipList != null){
				dataLists.clear();
				for(Tip tip:tipList){
					datas.add(tip.getName());
					dataLists.add(new LocationBean(tip.getPoint().getLongitude(),tip.getPoint().getLatitude(),tip.getAddress(),tip.getDistrict()));
				}
			}

			ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(mContext, android.R.layout.simple_list_item_1,datas);
			et.setAdapter(arrayAdapter);
			arrayAdapter.notifyDataSetChanged();
		}
	}
}

封装的POI搜索类PoiSearchTask

import android.content.Context;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.amap.api.services.poisearch.PoiSearch.SearchBound;
import com.xw.changba.bus.ui.demand.entity.LocationBean;

import java.util.ArrayList;

public class PoiSearchTask implements PoiSearch.OnPoiSearchListener {

	private static PoiSearchTask mInstance;
	private SearchLocationActivity.PoiAdapter mAdapter;
	private PoiSearch mSearch;
	private Context mContext;

	private PoiSearchTask(Context context){
		this.mContext = context;
	}

	public static PoiSearchTask getInstance(Context context){
		if(mInstance == null){
			synchronized (PoiSearchTask.class) {
				if(mInstance == null){
					mInstance = new PoiSearchTask(context);
				}
			}
		}
		return mInstance;
	}

	public PoiSearchTask setAdapter(SearchLocationActivity.PoiAdapter adapter){
		this.mAdapter = adapter;
		return this;
	}

	public void onSearch(String key, String city,double lat,double lng){
		// 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个参数表示poi搜索区域(空字符串代表全国)
		PoiSearch.Query query = new PoiSearch.Query(key, "", city);
		mSearch = new PoiSearch(mContext, query);
		mSearch.setBound(new SearchBound(new LatLonPoint(lat, lng), 2000));//设置周边搜索的中心点以及半径
		//设置异步监听
		mSearch.setOnPoiSearchListener(this);
		//查询POI异步接口
		mSearch.searchPOIAsyn();
	}

	@Override
	public void onPoiSearched(PoiResult poiResult, int rCode) {
		if(rCode == 1000) {
			if (poiResult != null && poiResult.getQuery() != null) {
				ArrayList<LocationBean> datas = new ArrayList<>();
				ArrayList<PoiItem> items = poiResult.getPois();
				for (PoiItem item : items) {
					//获取经纬度对象
					LatLonPoint llp = item.getLatLonPoint();
					double lon = llp.getLongitude();
					double lat = llp.getLatitude();
					//获取标题
					String title = item.getTitle();
					//获取内容
					String text = item.getSnippet();
					datas.add(new LocationBean(lon, lat, title, text));
				}
				mAdapter.setData(datas);
				mAdapter.notifyDataSetChanged();
			}
		}
	}

	@Override
	public void onPoiItemSearched(PoiItem poiItem, int i) {

	}
}

封装的POI搜索类LocationBean

public class LocationBean implements Serializable{

	private double lon;
	private double lat;
	private String title;
	private String content;

	public LocationBean(double lon,double lat,String title,String content){
		this.lon = lon;
		this.lat = lat;
		this.title = title;
		this.content = content;
	}

	public double getLon() {
		return lon;
	}

	public double getLat() {
		return lat;
	}

	public String getTitle() {
		return title;
	}

	public String getContent() {
		return content;
	}
}

一些工具类

public class LatLngEntity {

    private final double longitude; // 经度
    private final double latitude;  // 纬度

    /**
     * @param value e.g "113.4114889842685,23.172753522587282"
     */
    public LatLngEntity(String value) {
        this.longitude = Double.parseDouble(value.split(",")[0]);
        this.latitude = Double.parseDouble(value.split(",")[1]);
    }

    public LatLngEntity(double latitude, double longitude) {
        this.longitude = longitude;
        this.latitude = latitude;
    }

    public LatLngEntity(LatLonPoint latLonPoint) {
        this.longitude = latLonPoint.getLongitude();
        this.latitude = latLonPoint.getLatitude();
    }

    public double getLatitude() {
        return latitude;
    }

    public double getLongitude() {
        return longitude;
    }
}

GeoCoderUtil类

import android.content.Context;
import android.text.TextUtils;

import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeAddress;
import com.amap.api.services.geocoder.GeocodeQuery;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeAddress;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.xw.vehicle.mgr.ext.amap.model.LatLngEntity;

public class GeoCoderUtil implements GeocodeSearch.OnGeocodeSearchListener {
    private GeocodeSearch geocodeSearch;
    private GeoCoderAddressListener geoCoderAddressListener;
    private GeoCoderLatLngListener geoCoderLatLngListener;

    private static GeoCoderUtil geoCoderUtil;
    public static GeoCoderUtil getInstance(Context context) {
        if (null == geoCoderUtil) {
            geoCoderUtil = new GeoCoderUtil(context);
        }
        return geoCoderUtil;
    }

    private GeoCoderUtil(Context context) {
        geocodeSearch = new GeocodeSearch(context);
        geocodeSearch.setOnGeocodeSearchListener(this);
    }

    /**
     * 经纬度转地址描述
     **/
    public void geoAddress(String latLng, GeoCoderAddressListener geoCoderAddressListener) {
        if (TextUtils.isEmpty(latLng)) {
            geoCoderAddressListener.onAddressResult("");
            return;
        }
        this.geoAddress(new LatLngEntity(latLng), geoCoderAddressListener);
    }

    /**
     * 经纬度转地址描述
     **/
    public void geoAddress(LatLngEntity latLngEntity, GeoCoderAddressListener geoCoderAddressListener) {
        if (latLngEntity == null) {
            geoCoderAddressListener.onAddressResult("");
            return;
        }
        this.geoCoderAddressListener = geoCoderAddressListener;

        LatLonPoint latLonPoint = new LatLonPoint(latLngEntity.getLatitude(), latLngEntity.getLongitude());
        // 第一个参数表示一个Latlng,第二参数表示范围多少米,第三个参数表示是火系坐标系还是GPS原生坐标系
        RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 200, GeocodeSearch.AMAP);
        geocodeSearch.getFromLocationAsyn(query);// 设置异步逆地理编码请求

    }

    /**
     * 经纬度转地址描述,同步方法
     **/
    public String geoAddress(LatLngEntity latLngEntity) {
        if (latLngEntity == null) {
            return "";
        }
        this.geoCoderAddressListener = geoCoderAddressListener;

        LatLonPoint latLonPoint = new LatLonPoint(latLngEntity.getLatitude(), latLngEntity.getLongitude());
        // 第一个参数表示一个Latlng,第二参数表示范围多少米,第三个参数表示是火系坐标系还是GPS原生坐标系
        RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 200, GeocodeSearch.AMAP);
        try {
            RegeocodeAddress regeocodeAddress = geocodeSearch.getFromLocation(query);// 设置异步逆地理编码请求
            String formatAddress = regeocodeAddress.getFormatAddress();
            return formatAddress;
        } catch (AMapException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 地址描述转经纬度
     **/
    public void geoLatLng(String cityName, String address, GeoCoderLatLngListener geoCoderLatLngListener) {
        if (TextUtils.isEmpty(cityName) || TextUtils.isEmpty(address)) {
            geoCoderLatLngListener.onLatLngResult(null);
            return;
        }
        this.geoCoderLatLngListener = geoCoderLatLngListener;
        GeocodeQuery query = new GeocodeQuery(address, cityName);
        geocodeSearch.getFromLocationNameAsyn(query);
    }

    @Override
    public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
        if (rCode != 1000 || result == null || result.getRegeocodeAddress() == null) {
            geoCoderAddressListener.onAddressResult("");
            return;
        }
        RegeocodeAddress regeocodeAddress = result.getRegeocodeAddress();
        String addressDesc = regeocodeAddress.getCity()
                + regeocodeAddress.getDistrict()
                + regeocodeAddress.getTownship()
                + regeocodeAddress.getStreetNumber().getStreet();
        if (regeocodeAddress.getAois().size() > 0) {
            addressDesc += regeocodeAddress.getAois().get(0).getAoiName();
        }
        geoCoderAddressListener.onAddressResult(addressDesc);
    }

    @Override
    public void onGeocodeSearched(GeocodeResult geocodeResult, int rCode) {
        if (geocodeResult == null || geocodeResult.getGeocodeAddressList() == null
                || geocodeResult.getGeocodeAddressList().size() <= 0) {
            geoCoderLatLngListener.onLatLngResult(null);
            return;
        }
        GeocodeAddress address = geocodeResult.getGeocodeAddressList().get(0);
        geoCoderLatLngListener.onLatLngResult(new LatLngEntity(address.getLatLonPoint()));

    }

    public interface GeoCoderAddressListener {
        void onAddressResult(String result);
    }

    public interface GeoCoderLatLngListener {
        void onLatLngResult(LatLngEntity latLng);
    }

}