1.需要资源:
2.布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff" android:orientation="vertical"> <!-- 标题栏 --> <include android:id="@+id/layoutbt" layout="@layout/titlelayout" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="34dp" android:layout_margin="10dp" android:background="@drawable/addres"> <ImageView android:id="@+id/imag_ss" android:layout_width="14dp" android:layout_height="14dp" android:layout_centerVertical="true" android:layout_marginLeft="15dp" android:src="@mipmap/search_address" /> <EditText android:id="@+id/edittext" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_toLeftOf="@+id/imag_deletedz" android:layout_toRightOf="@+id/imag_ss" android:background="@null" android:hint="搜索地点或地址" android:singleLine="true" android:textColor="#232323" android:textColorHint="#999999" android:textSize="12dp" /> <ImageView android:id="@+id/imag_deletedz" android:layout_width="14dp" android:layout_height="14dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginLeft="5dp" android:layout_marginRight="10dp" android:src="@mipmap/remove_search" /> </RelativeLayout> <RelativeLayout android:id="@+id/hqxmgd" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/map_list" android:orientation="vertical"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_above="@+id/linear_sx"> <com.amap.api.maps.MapView android:id="@+id/map_local" android:layout_width="match_parent" android:layout_height="match_parent"/> <ImageView android:visibility="gone" android:layout_width="33dp" android:layout_height="40dp" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_gravity="center" android:src="@mipmap/map_locationv" android:translationY="-18dp" /> </RelativeLayout> <LinearLayout android:id="@+id/linear_sx" android:layout_width="match_parent" android:layout_height="25dp" android:layout_alignParentBottom="true" android:layout_marginBottom="10dp" android:orientation="vertical"> <LinearLayout android:id="@+id/text_hd" android:layout_width="100dp" android:layout_height="20dp" android:layout_gravity="center_horizontal" android:gravity="center"> <TextView android:layout_width="34dp" android:layout_height="5dp" android:background="@drawable/dzzy" /> </LinearLayout> </LinearLayout> </RelativeLayout> <ListView android:id="@+id/map_list" android:layout_width="match_parent" android:layout_height="283dp" android:layout_alignParentBottom="true" android:animateLayoutChanges="true" android:background="#ffffff" android:divider="@null" android:dividerHeight="0.5dp" android:scrollbars="none"></ListView> </RelativeLayout> </LinearLayout>
3.首先要实现AMap.OnMapClickListener(地图点击事件)
public class SiteControltwo extends Activity implements AMap.OnMapClickListener,LocationSource {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sitecontrol_layout);
//在activity执行onCreate时执行mMapView.onCreate(savedInstanceState),创建地图
mapView.onCreate(savedInstanceState);
mapView = findViewById(R.id.map_local);
aMap = mapView.getMap();
aMap.setOnMapClickListener(this);
UiSettings uiSettings = aMap.getUiSettings();
uiSettings.setZoomControlsEnabled(false); //隐藏缩放按钮
uiSettings.setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
aMap.setLocationSource(this);// 设置定位监听
}
/**
* map点击事件
*
* @param latLng 经纬度
*/
@Override
public void onMapClick(LatLng latLng) {
aMap.clear();
xx = latLng.latitude;
yy = latLng.longitude;
MarkerOptions otMarkerOptions = new MarkerOptions();
otMarkerOptions.icon(BitmapDescriptorFactory.fromResource(R.mipmap.map_location));
otMarkerOptions.position(latLng);
aMap.addMarker(otMarkerOptions);
doSearchQuery();
// aMap.moveCamera(CameraUpdateFactory.changeLatLng(latLng));//点击后移动到屏幕中间(这里不需要)
}
//开始地图图标点击定位
@Override
public void activate(OnLocationChangedListener onLocationChangedListener) {
aMap.clear();
//这里实现定位后的结果
}
//结束地图图标点击定位
@Override
public void deactivate() {
}
}
定位点击获取经纬度展示就完成了
3.根据点击的位置获取周边的信息:
//poi搜索类型
private String deepType = " 汽车服务|汽车销售|汽车维修|摩托车服务|餐饮服务|购物服务|生活服务|体育休闲服务|医疗保健服务|住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|交通设施服务|金融保险服务|公司企业|道路附属设施|地名地址信息|公共设施";
private PoiSearch.Query query;// Poi查询条件类
private PoiSearch poiSearch;//Poi查询条件类
private ArrayList<PoiItem> poiItems;// poi数据
private PoiListAdapteractivity adapter;//adapter
/**
* 开始进行poi搜索(需要实现PoiSearch.OnPoiSearchListener)
*/
protected void doSearchQuery() {
aMap.setOnMapClickListener(null);// 进行poi搜索时清除掉地图点击事件
query = new PoiSearch.Query("", deepType, mcity);// 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个city参数表示poi搜索区域(空字符串代表全国)
query.setPageSize(50);// 设置每页最多返回多少条poiitem
query.setPageNum(0);// 设置查第一页
LatLonPoint lp = new LatLonPoint(xx, yy);
poiSearch = new PoiSearch(this, query);
poiSearch.setOnPoiSearchListener(this);
poiSearch.setBound(new PoiSearch.SearchBound(lp, 20000, true));
// 设置搜索区域为以lp点为圆心,其周围2000米范围
poiSearch.searchPOIAsyn();// 异步搜索
}
/**
* 搜索结果
*/
@Override
public void onPoiSearched(PoiResult result, int rCode) {
aMap.setOnMapClickListener(this);// 进行poi搜索结果后恢复地图点击事件
// System.out.println("===s>>" + result.getPois());
if (rCode == AMapException.CODE_AMAP_SUCCESS) {
// 搜索poi的结果
if (result != null && result.getQuery() != null) {
poiItems = result.getPois();// 取得第一页的poiitem数据,页数从数字0开始
if (poiItems != null && poiItems.size() > 0) {
adapter = new PoiListAdapteractivity(this, poiItems);
mapList.setAdapter(adapter);
mapList.setOnItemClickListener(new SiteControltwo.mOnItemClickListener());
} else {
System.out.println("无结果");
}
} else {
System.out.println("无结果");
}
} else {
System.out.println("error_other:" + rCode);
}
}
@Override
public void onPoiItemSearched(PoiItem poiItem, int i) {
}
//点击返回上一页的值
private class mOnItemClickListener implements AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String lat = String.valueOf(poiItems.get(position).getLatLonPoint().getLatitude());
String longa = String.valueOf(poiItems.get(position).getLatLonPoint().getLongitude());
mcity = poiItems.get(position).getCityName();
System.out.println("选择信息打印:" + poiItems.get(position).getProvinceName() + "、" + poiItems.get(position).getCityName() + "、" + poiItems.get(position).getAdName());
}
}
PoiListAdapteractivity系列:
public class PoiListAdapteractivity extends BaseAdapter {
private Context ctx;
private List<PoiItem> list;
public PoiListAdapteractivity(Context context, List<PoiItem> poiList) {
this.ctx = context;
this.list = poiList;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = View.inflate(ctx, R.layout.maplistview_item, null);
holder.poititle = (TextView) convertView
.findViewById(R.id.poititle);
holder.poimiaoshu = (TextView) convertView
.findViewById(R.id.poimiaoshu);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
PoiItem item = list.get(position);
// if (position == 0) {
// SpannableStringBuilder span = new SpannableStringBuilder("【当前】" + item.getTitle());
// span.setSpan(new ForegroundColorSpan(Color.parseColor("#3FBF7F")), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// holder.poititle.setText(span);
// } else {
holder.poititle.setText(item.getTitle());
// }
holder.poimiaoshu.setText(item.getSnippet());
return convertView;
}
private class ViewHolder {
TextView poititle, poimiaoshu;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="71dp"
android:background="#ffffff"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/relat_rf"
android:layout_width="wrap_content"
android:layout_height="60dp">
<ImageView
android:id="@+id/imageView6"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_marginTop="2dp"
android:background="@mipmap/addressmap"
android:scaleType="fitXY"
android:visibility="gone" />
<TextView
android:id="@+id/poititle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/imageView6"
android:ellipsize="end"
android:gravity="center_vertical"
android:singleLine="true"
android:text="内容"
android:textColor="#262626"
android:textSize="14dp" />
<TextView
android:id="@+id/poimiaoshu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/poititle"
android:layout_below="@+id/poititle"
android:layout_marginTop="3dp"
android:ellipsize="end"
android:singleLine="true"
android:text="内容"
android:textColor="#999999"
android:textSize="12dp" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_alignParentBottom="true"
android:layout_marginTop="2dp"
android:background="#eeeeee" />
</RelativeLayout>
ok了
下面是activity的完整代码
/**
* 功 能 : 选址搜索(点击地图)
*/
public class SiteControltwo extends Activity implements View.OnClickListener, AMap.OnMapClickListener, PoiSearch.OnPoiSearchListener, LocationSource {
private ImageView black;//返回键
private TextView title;//标题
private MapView mapView;//地图控件
private AMap aMap;
private double xx, yy;
private String jsxx, jsyy;
private String mcity="";//城市
//poi搜索类型
private String deepType = " 汽车服务|汽车销售|汽车维修|摩托车服务|餐饮服务|购物服务|生活服务|体育休闲服务|医疗保健服务|住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|交通设施服务|金融保险服务|公司企业|道路附属设施|地名地址信息|公共设施";
private PoiSearch.Query query;// Poi查询条件类
private PoiSearch poiSearch;//Poi查询条件类
private ArrayList<PoiItem> poiItems;// poi数据
private PoiListAdapteractivity adapter;//adapter
private LinearLayout text_hd;
private ListView mapList;//显示listview数据
private EditText edittext;
private String SouSuo = "";//搜索输入的内容
private ImageView imag_deletedz;//清空按钮
private int GdZoom=17;//显示地图大小度
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sitecontrol_layout);
//传过来的经纬度(为0是没有经纬度需要开启定位取得经纬度)
jsxx = getIntent().getStringExtra("xx");
jsyy = getIntent().getStringExtra("yy");
mcity= getIntent().getStringExtra("city");
initUi();
//在activity执行onCreate时执行mMapView.onCreate(savedInstanceState),创建地图
mapView.onCreate(savedInstanceState);
if ("0".equals(jsxx) || "0".equals(jsyy)) {//判断第一次进入(无经纬度)
getMyJwd(this);//高德定位的方法(自己封装)
} else {
xx = Double.parseDouble(jsxx);
yy = Double.parseDouble(jsyy);
LatLng latlng = new LatLng(xx, yy);
MarkerOptions otMarkerOptions = new MarkerOptions();
otMarkerOptions.icon(BitmapDescriptorFactory.fromResource(R.mipmap.map_location));
otMarkerOptions.position(latlng);
aMap.addMarker(otMarkerOptions);
aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, GdZoom));
doSearchQuery();
}
//---搜索--
edittext = findViewById(R.id.edittext);
imag_deletedz = findViewById(R.id.imag_deletedz);
imag_deletedz.setOnClickListener(this);
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//输入前
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//内容变化(输入时)
String neirong = edittext.getText().toString();
if (!neirong.equals("")) {
SouSuo = neirong;
if (poiItems != null && poiItems.size() > 0) {
poiItems.clear();
}
Sspoi();
} else {
doSearchQuery();
}
}
@Override
public void afterTextChanged(Editable editable) {
//输入后
}
});
}
/**
* 获取当前位置经纬度和选中城市的数据
*/
HashMap<String, Object> mapdt = new HashMap<String, Object>();
public void getMyJwd(final Context context) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
MapUtil.getAddress(context, new MapUtil.Result() {
@Override
public void onSuceece(HashMap<String, Object> jwd) {
mapdt = jwd;
mcity = String.valueOf(mapdt.get("mcity"));
xx = (double) mapdt.get("mCurrentLantitude");
yy = (double) mapdt.get("mCurrentLongitude");
final LatLng latlng = new LatLng(xx, yy);
runOnUiThread(new Runnable() {
@Override
public void run() {
MarkerOptions otMarkerOptions = new MarkerOptions();
otMarkerOptions.icon(BitmapDescriptorFactory.fromResource(R.mipmap.map_location));
otMarkerOptions.position(latlng);
aMap.addMarker(otMarkerOptions);
// aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, GdZoom));
aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latlng, GdZoom), 500, null);//带动画移动
doSearchQuery();
}
});
}
});
}
});
thread.start();
}
//实例化ui
@SuppressLint("WrongConstant")
private void initUi() {
black = findViewById(R.id.black);
black.setOnClickListener(this);
title = findViewById(R.id.title);
title.setText("定位地址");
mapList = findViewById(R.id.map_list);
text_hd= findViewById(R.id.text_hd);
text_hd.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN://按住事件发生后执行代码的区域
if (mapList.getVisibility() == 8) {//GONE=8,INVISIBLE=4,VISIBLE=0
mapList.setVisibility(View.VISIBLE);
} else {
mapList.setVisibility(View.GONE);
}
break;
case MotionEvent.ACTION_UP://松开事件发生后执行代码的区域
break;
case MotionEvent.ACTION_MOVE://移动事件发生后执行代码的区域
break;
}
return true;
}
});
mapView = findViewById(R.id.map_local);
aMap = mapView.getMap();
aMap.setOnMapClickListener(this);
UiSettings uiSettings = aMap.getUiSettings();
uiSettings.setZoomControlsEnabled(false); //隐藏缩放按钮
uiSettings.setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
aMap.setLocationSource(this);// 设置定位监听
// aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层(小图标与当前范围的圈),false表示隐藏定位层并不可触发定位,默认是false
}
/**
* view点击事件
*/
@Override
public void onClick(View view) {
switch (view.getId()) {
//返回键
case R.id.black:
finish();
break;
//清空
case R.id.imag_deletedz:
edittext.setText("");
doSearchQuery();
break;
}
}
/**
* map点击事件
*
* @param latLng 经纬度
*/
@Override
public void onMapClick(LatLng latLng) {
aMap.clear();
xx = latLng.latitude;
yy = latLng.longitude;
MarkerOptions otMarkerOptions = new MarkerOptions();
otMarkerOptions.icon(BitmapDescriptorFactory.fromResource(R.mipmap.map_location));
otMarkerOptions.position(latLng);
aMap.addMarker(otMarkerOptions);
doSearchQuery();
// aMap.moveCamera(CameraUpdateFactory.changeLatLng(latLng));//点击后移动到屏幕中间(这里不需要)
}
/**
* 开始进行poi搜索
*/
protected void doSearchQuery() {
aMap.setOnMapClickListener(null);// 进行poi搜索时清除掉地图点击事件
query = new PoiSearch.Query("", deepType, mcity);// 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个city参数表示poi搜索区域(空字符串代表全国)
query.setPageSize(50);// 设置每页最多返回多少条poiitem
query.setPageNum(0);// 设置查第一页
LatLonPoint lp = new LatLonPoint(xx, yy);
poiSearch = new PoiSearch(this, query);
poiSearch.setOnPoiSearchListener(this);
poiSearch.setBound(new PoiSearch.SearchBound(lp, 20000, true));
// 设置搜索区域为以lp点为圆心,其周围2000米范围
poiSearch.searchPOIAsyn();// 异步搜索
}
//搜索poi
private void Sspoi() {
//keyWord表示搜索字符串,
//第二个参数表示POI搜索类型
//cityCode表示POI搜索区域,可以是城市编码也可以是城市名称,也可以传空字符串,空字符串代表全国在全国范围内进行搜索
PoiSearch.Query query = new PoiSearch.Query(SouSuo, deepType, mcity);
query.setPageSize(50);
PoiSearch search = new PoiSearch(this, query);
search.setOnPoiSearchListener(this);
search.searchPOIAsyn();
}
/**
* 搜索结果
*/
@Override
public void onPoiSearched(PoiResult result, int rCode) {
aMap.setOnMapClickListener(this);// 进行poi搜索结果后恢复地图点击事件
// System.out.println("===s>>" + result.getPois());
if (rCode == AMapException.CODE_AMAP_SUCCESS) {
// 搜索poi的结果
if (result != null && result.getQuery() != null) {
poiItems = result.getPois();// 取得第一页的poiitem数据,页数从数字0开始
if (poiItems != null && poiItems.size() > 0) {
adapter = new PoiListAdapteractivity(this, poiItems);
mapList.setAdapter(adapter);
mapList.setOnItemClickListener(new SiteControltwo.mOnItemClickListener());
} else {
System.out.println("无结果");
}
} else {
System.out.println("无结果");
}
} else {
System.out.println("error_other:" + rCode);
}
}
@Override
public void onPoiItemSearched(PoiItem poiItem, int i) {
}
//开始地图图标点击定位
@Override
public void activate(OnLocationChangedListener onLocationChangedListener) {
aMap.clear();
getMyJwd(this);
}
//结束地图图标点击定位
@Override
public void deactivate() {
}
//点击返回上一页的值
private class mOnItemClickListener implements AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String lat = String.valueOf(poiItems.get(position).getLatLonPoint().getLatitude());
String longa = String.valueOf(poiItems.get(position).getLatLonPoint().getLongitude());
mcity = poiItems.get(position).getCityName();
// System.out.println("选择信息打印:" + poiItems.get(position).getProvinceName() + "、" + poiItems.get(position).getCityName() + "、" + poiItems.get(position).getAdName());
EventBusSite eventBusSite = new EventBusSite(lat, longa, poiItems.get(position).getTitle());
EventBus.getDefault().post(eventBusSite);
EventBusCity eventBusCity = new EventBusCity( poiItems.get(position).getProvinceName(), mcity, poiItems.get(position).getAdName());
EventBus.getDefault().post(eventBusCity);
finish();
}
}
}