Android基于高德地图poi的仿微信获取位置功能实例代码

Android基于高德地图poi的仿微信获取位置功能实例代码,第1张

概述 1.打开页面自动定位,同时搜索周边所有poi2.点击搜索按钮,输入关键子,获取关键字搜索结果

 1.打开页面自动定位,同时搜索周边所有poi

2.点击搜索按钮,输入关键子,获取关键字搜索结果

3。选取ListvIEw中的一项即可定位到该位置,或者获取任何消息

4.文件类

1、MapActivity

public class MapActivity extends Activity implements PoiSearch.OnPoiSearchListener { private MapVIEw mMapVIEw = null; private AMap aMap; private MyLocationStyle myLocationStyle; //poiSearch相关 private PoiSearch poiSearch; private PoiSearch.query query; boolean isPoiSearched = false; //是否进行poi搜索 //ListvIEw private ListVIEw ll; ArrayList<PoiItem> arrayList; MyAdpter adapter; MyHandler myHandler; //字体 Typeface tf; //搜索栏 FrameLayout frameLayout; ImageVIEw searchIv; EditText searchEt; TextVIEw Title; button btn; ImageVIEw success; boolean onSearch = false; //是否打开搜索栏 ImageVIEw back; private double mCurrentLat; private double mCurrentLng; Map<String,String> currentInfo = new HashMap<>(); int selectIndex = -1; ImageVIEw currentSelectItem = null; @OverrIDe protected voID onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentVIEw(R.layout.activity_map);  findAllVIEw();  setAllVIEwOnclicklinster();  //在activity执行onCreate时执行mMapVIEw.onCreate(savedInstanceState),创建地图  mMapVIEw.onCreate(savedInstanceState);  initAMap(); } /**  * 获取vIEw对象,初始化一些对象  */ voID findAllVIEw() {  //获取地图控件引用  mMapVIEw = (MapVIEw) findVIEwByID(R.ID.map);  frameLayout = (FrameLayout) findVIEwByID(R.ID.searchLayout);  searchEt = (EditText) findVIEwByID(R.ID.search_input);  searchIv = (ImageVIEw) findVIEwByID(R.ID.search);  btn = (button) findVIEwByID(R.ID.search_go_btn);  success = (ImageVIEw) findVIEwByID(R.ID.success);  back = (ImageVIEw) findVIEwByID(R.ID.back);  //初始化ListvIEw  ll = (ListVIEw) findVIEwByID(R.ID.ll);  arrayList = new ArrayList<>();  adapter = new MyAdpter();  ll.setAdapter(adapter);  //设置标题字体  tf = Typeface.createFromAsset(getAssets(),"Font/f1.ttf");  (Title = (TextVIEw) findVIEwByID(R.ID.Title)).setTypeface(tf);  myHandler = new MyHandler(); } /**  * 设置点击事件  */ voID setAllVIEwOnclicklinster() {  //当搜索图标点击时,切换显示效果  searchIv.setonClickListener(new VIEw.OnClickListener() {   @OverrIDe   public voID onClick(VIEw vIEw) {    if (Title.getVisibility() == VIEw.VISIBLE) {     hIDeTitle();    } else if (Title.getVisibility() == VIEw.GONE) {     showTitle();    }   }  });  //点击搜索按钮时,搜索关键字  btn.setonClickListener(new VIEw.OnClickListener() {   @OverrIDe   public voID onClick(VIEw vIEw) {    String key = searchEt.getText().toString();    if (!key.trim().isEmpty()) {     if (currentSelectItem != null) {      currentSelectItem.setVisibility(VIEw.INVISIBLE);     }     searchPoi(key,currentInfo.get("cityCode"),false);    }   }  });  //使editText监听回车事件,进行搜索,效果同上  searchEt.setonKeyListener(new VIEw.OnKeyListener() {   @OverrIDe   public boolean onKey(VIEw v,int keyCode,KeyEvent event) {    if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {     String key = searchEt.getText().toString();     if (!key.trim().isEmpty()) {      if (currentSelectItem != null) {       currentSelectItem.setVisibility(VIEw.INVISIBLE);      }      searchPoi(key,false);     }     return true;    }    return false;   }  });  //返回处理事件  back.setonClickListener(new VIEw.OnClickListener() {   @OverrIDe   public voID onClick(VIEw vIEw) {    if (onSearch) {     showTitle();    } else {     finish();    }   }  });  //完成事件  success.setonClickListener(new VIEw.OnClickListener() {   @OverrIDe   public voID onClick(VIEw vIEw) {    //点击ok的处理事件    //获取数据并返回上一个activity即可   }  });  //ListvIEw点击事件  ll.setonItemClickListener(new AdapterVIEw.OnItemClickListener() {   @OverrIDe   public voID onItemClick(AdapterVIEw<?> adapterVIEw,VIEw vIEw,int i,long l) {    Log.e(ProConfig.TAG,i + "");    PoiItem item = arrayList.get(i);    Log.e(ProConfig.TAG,item.getLatLonPoint().toString());    Log.e(ProConfig.TAG,item.toString());    Log.e(ProConfig.TAG,item.getAdname());    //在地图上添加一个marker,并将地图中移动至此处    MarkerOptions mk = new MarkerOptions();    mk.icon(BitmapDescriptorFactory.defaultMarker());    mk.Title(item.getAdname());    LatLng ll = new LatLng(item.getLatLonPoint().getLatitude(),item.getLatLonPoint().getLongitude());    mk.position(ll);    //清除所有marker等,保留自身    aMap.clear(true);    CameraUpdate cu = CameraUpdateFactory.newLatLng(ll);    aMap.animateCamera(cu);    aMap.addMarker(mk);    //存储当前点击位置    selectIndex = i;    //存储当前点击vIEw,并修改vIEw和上一个选中vIEw的定位图标    ImageVIEw iv = (ImageVIEw) vIEw.findVIEwByID(R.ID.yes);    iv.setVisibility(VIEw.VISIBLE);    if (currentSelectItem != null) {     currentSelectItem.setVisibility(VIEw.INVISIBLE);    }    currentSelectItem = iv;    if (onSearch) {     //退出搜索模式,显示地图     showTitle();    }   }  }); } /**  * 初始化高德地图  */ voID initAMap() {  //初始化地图控制器对象  if (aMap == null) {   aMap = mMapVIEw.getMap();  }  //地图加载监听器  aMap.setonMapLoadedListener(new AMap.OnMapLoadedListener() {   @OverrIDe   public voID onMapLoaded() {    //aMap.setMapType();    aMap.setMyLocationEnabled(true);    aMap.animateCamera(CameraUpdateFactory.zoomTo(aMap.getMaxZoomLevel() - 1));   }  });  myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类  myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE);//连续定位、且将视角移动到地图中心点,定位点依照设备方向旋转,并且会跟随设备移动。(1秒1次定位)如果不设置myLocationType,默认也会执行此种模式。  myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔,只在连续定位模式下生效,单次定位模式下不会生效。单位为毫秒。  myLocationStyle.radiusFillcolor(0x70f3ff);  myLocationStyle.strokecolor(0xe3f9fd);  aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style  aMap.getUiSettings().setMyLocationbuttonEnabled(true);//设置默认定位按钮是否显示,非必需设置。  aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点,false表示隐藏定位蓝点并不进行定位,默认是false。  aMap.setMaxZoomLevel(aMap.getMaxZoomLevel());  //地址监听事件  aMap.setonMyLocationchangelistener(new AMap.OnMyLocationchangelistener() {   @OverrIDe   public voID onMyLocationChange(Location location) {    if (!isPoiSearched) {     Log.e(ProConfig.TAG,location.toString());     Log.e(ProConfig.TAG,location.getProvIDer());     Log.e(ProConfig.TAG,location.getLatitude() + ":" + location.getLongitude());     //存储定位数据     mCurrentLat = location.getLatitude();     mCurrentLng = location.getLongitude();     String[] args = location.toString().split("#");     for (String arg : args) {      String[] data = arg.split("=");      if (data.length >= 2)       currentInfo.put(data[0],data[1]);     }     //搜索poi     searchPoi("",true);     //latitude=41.652146#longitude=123.427205#province=辽宁省#city=沈阳市#district=浑南区#cityCode=024#adCode=210112#address=辽宁省沈阳市浑南区创新一路靠近东北大学浑南校区#country=中国#road=创新一路#poiname=东北大学浑南校区#street=创新一路#streetNum=193号#aoiname=东北大学浑南校区#poiID=#floor=#errorCode=0#errorInfo=success#locationDetail=24 #csID:1cce9508143d493182a8da7745eb07b3#locationType=5    }   }  }); } @OverrIDe public boolean onKeyDown(int keyCode,KeyEvent event) {  //点击返回键时,将浏览器后退  if (keyCode == KeyEvent.KEYCODE_BACK) {   if (onSearch) {    showTitle();    return true;   } else    return super.onKeyDown(keyCode,event);  }  return super.onKeyDown(keyCode,event); } class MyHandler extends Handler {  @OverrIDe  public voID handleMessage(Message msg) {   super.handleMessage(msg);   switch (msg.what) {    case 0x001:     //加载ListvIEw中数据     adapter.notifyDataSetChanged();     break;   }  } } /**  * 自定义adpter  */ class MyAdpter extends BaseAdapter {  @OverrIDe  public int getCount() {   return arrayList.size();  }  @OverrIDe  public Object getItem(int i) {   return arrayList.get(i);  }  @OverrIDe  public long getItemID(int i) {   return i;  }  @OverrIDe  public VIEw getVIEw(int i,VIEwGroup vIEwGroup) {   //布局加载器   LayoutInflater inflater = LayoutInflater.from(MapActivity.this);   //加载location_item布局   VIEw vIEw1 = inflater.inflate(R.layout.location_item,null);   //修改文字和字体   TextVIEw v1 = (TextVIEw) vIEw1.findVIEwByID(R.ID.name);   TextVIEw v2 = (TextVIEw) vIEw1.findVIEwByID(R.ID.sub);   ImageVIEw iv = (ImageVIEw) vIEw1.findVIEwByID(R.ID.yes);   v1.setText(arrayList.get(i).getTitle());   v1.setTypeface(tf);   v2.setText(arrayList.get(i).getSnippet());   v2.setTypeface(tf);   if (selectIndex == i) {    iv.setVisibility(VIEw.VISIBLE);    currentSelectItem = iv;   } else {    iv.setVisibility(VIEw.INVISIBLE);   }   return vIEw1;  } } /**  * 搜索poi  *  * @param key  关键字  * @param pageNum 页码  * @param cityCode 城市代码,或者城市名称  * @param nearby 是否搜索周边  */ voID searchPoi(String key,int pageNum,String cityCode,boolean nearby) {  Log.e(ProConfig.TAG,key);  isPoiSearched = true;  query = new PoiSearch.query(key,"",cityCode);  //keyWord表示搜索字符串,  //第二个参数表示POI搜索类型,二者选填其一,  //POI搜索类型共分为以下20种:汽车服务|汽车销售|  //汽车维修|摩托车服务|餐饮服务|购物服务|生活服务|体育休闲服务|医疗保健服务|  //住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|交通设施服务|  //金融保险服务|公司企业|道路附属设施|地名地址信息|公共设施  //cityCode表示POI搜索区域,可以是城市编码也可以是城市名称,也可以传空字符串,空字符串代表全国在全国范围内进行搜索  query.setPageSize(50);// 设置每页最多返回多少条poiitem  query.setPageNum(pageNum);//设置查询页码  poiSearch = new PoiSearch(this,query);  poiSearch.setonPoiSearchListener(this);  if (nearby)   poiSearch.setBound(new PoiSearch.SearchBound(new LatLonPoint(mCurrentLat,mCurrentLng),1500));//设置周边搜索的中心点以及半径  poiSearch.searchPOIAsyn(); } @OverrIDe public voID onPoiSearched(PoiResult poiResult,int i) {  int index = 0;  //填充数据,并更新ListvIEw  ArrayList<PoiItem> result = poiResult.getPois();  if (result.size() > 0) {   arrayList.clear();   selectIndex = -1;   arrayList.addAll(result);   myHandler.sendEmptyMessage(0x001);  }  for (PoiItem item : poiResult.getPois()) {   Log.e(ProConfig.TAG,item.toString());   Log.e(ProConfig.TAG,item.getDirection());   Log.e(ProConfig.TAG,item.getAdname());  } } @OverrIDe public voID onPoiItemSearched(PoiItem poiItem,int i) { } @OverrIDe protected voID onDestroy() {  super.onDestroy();  //在activity执行onDestroy时执行mMapVIEw.onDestroy(),销毁地图  mMapVIEw.onDestroy(); } @OverrIDe protected voID onResume() {  super.onResume();  //在activity执行onResume时执行mMapVIEw.onResume (),重新绘制加载地图  mMapVIEw.onResume(); } @OverrIDe protected voID onPause() {  super.onPause();  //在activity执行onPause时执行mMapVIEw.onPause (),暂停地图的绘制  mMapVIEw.onPause(); } @OverrIDe protected voID onSaveInstanceState(Bundle outState) {  super.onSaveInstanceState(outState);  //在activity执行onSaveInstanceState时执行mMapVIEw.onSaveInstanceState (outState),保存地图当前的状态  mMapVIEw.onSaveInstanceState(outState); } /**  * 显示标题栏,即默认状态  */ voID showTitle() {  //显示标题栏  Title.setVisibility(VIEw.VISIBLE);  success.setVisibility(VIEw.VISIBLE);  searchEt.setVisibility(VIEw.GONE);  btn.setVisibility(VIEw.GONE);  frameLayout.setLayoutParams(    new linearLayout.LayoutParams(      linearLayout.LayoutParams.WRAP_CONTENT,linearLayout.LayoutParams.MATCH_PARENT,5));  mMapVIEw.setVisibility(VIEw.VISIBLE);  onSearch = false;  closeKeyboard(this); } /**  * 隐藏标题栏,即进行搜索  */ voID hIDeTitle() {  //显示搜索框  Title.setVisibility(VIEw.GONE);  success.setVisibility(VIEw.GONE);  searchEt.setVisibility(VIEw.VISIBLE);  btn.setVisibility(VIEw.VISIBLE);  frameLayout.setLayoutParams(    new linearLayout.LayoutParams(      linearLayout.LayoutParams.WRAP_CONTENT,50));  mMapVIEw.setVisibility(VIEw.GONE);  onSearch = true; } /**  * 强制关闭软键盘  */ public voID closeKeyboard(Context context) {  inputMethodManager imm = (inputMethodManager) context.getSystemService(Context.input_METHOD_SERVICE);  if (imm.isActive()) {   //如果开启   imm.hIDeSoftinputFromWindow(getCurrentFocus().getwindowToken(),inputMethodManager.HIDE_NOT_ALWAYS);   //关闭软键盘,开启方法相同,这个方法是切换开启与关闭状态的  } }}

2、activity_map.xml

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:background="#e9e7ef" androID:orIEntation="vertical" tools:context="com.hzstz.mymanager.MapActivity"> <linearLayout  androID:layout_wIDth="match_parent"  androID:layout_height="50dp"  androID:layout_weight="1"  androID:background="#009dff"  androID:orIEntation="horizontal">  <ImageVIEw   androID:ID="@+ID/back"   androID:layout_wIDth="30dp"   androID:layout_height="25dp"   androID:layout_gravity="center_vertical"   androID:layout_weight="6"   androID:src="@drawable/left" />  <TextVIEw   androID:ID="@+ID/Title"   androID:layout_wIDth="wrap_content"   androID:layout_height="match_parent"   androID:layout_weight="80"   androID:gravity="center"   androID:text="@string/app_name"   androID:textcolor="#000"   androID:textSize="25dp"   androID:visibility="visible" />  <FrameLayout   androID:ID="@+ID/searchLayout"   androID:layout_wIDth="wrap_content"   androID:layout_height="match_parent"   androID:layout_weight="5">   <EditText    androID:ID="@+ID/search_input"    androID:layout_wIDth="match_parent"    androID:layout_height="40dp"    androID:layout_gravity="center_vertical"    androID:layout_margintop="2dp"    androID:hint="@string/app_name"    androID:paddingStart="40dp"    androID:visibility="gone" />   <ImageVIEw    androID:ID="@+ID/search"    androID:layout_wIDth="35dp"    androID:layout_height="35dp"    androID:layout_gravity="center_vertical"    androID:paddingStart="10dp"    androID:src="@drawable/search" />  </FrameLayout>  <button   androID:ID="@+ID/search_go_btn"   androID:layout_wIDth="wrap_content"   androID:layout_height="match_parent"   androID:background="@drawable/cling_button_normal"   androID:text="搜索"   androID:textcolor="#fff"   androID:visibility="gone" />  <ImageVIEw   androID:ID="@+ID/success"   androID:layout_wIDth="30dp"   androID:layout_height="30dp"   androID:layout_gravity="center_vertical"   androID:layout_margin="5dp"   androID:layout_weight="1"   androID:src="@drawable/yes"   androID:visibility="visible" /> </linearLayout> <com.amap.API.maps.MapVIEw  androID:ID="@+ID/map"  androID:layout_wIDth="match_parent"  androID:layout_height="750dp"  androID:layout_weight="100" /> <ListVIEw  androID:ID="@+ID/ll"  androID:layout_wIDth="match_parent"  androID:layout_height="wrap_content"  androID:layout_weight="90"  androID:background="#ffffff" /></linearLayout>

3、location.xml

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:background="#fff" androID:padding="10dp" androID:orIEntation="horizontal"> <linearLayout  androID:layout_wIDth="match_parent"  androID:orIEntation="vertical"  androID:layout_weight="100"  androID:layout_height="wrap_content">  <TextVIEw   androID:ID="@+ID/name"   androID:textSize="20dp"   androID:text="大会的反馈撒"   androID:textcolor="#000"   androID:layout_wIDth="wrap_content"   androID:layout_height="wrap_content" />  <TextVIEw   androID:ID="@+ID/sub"   androID:textSize="15dp"   androID:text="大会的反馈撒"   androID:textcolor="#808080"   androID:layout_wIDth="wrap_content"   androID:layout_height="wrap_content" /> </linearLayout> <ImageVIEw  androID:ID="@+ID/yes"  androID:layout_wIDth="30dp"  androID:layout_height="30dp"  androID:layout_gravity="center"  androID:src="@drawable/location"/></linearLayout>

总结

以上所述是小编给大家介绍的AndroID基于高德地图poi的仿微信获取位置功能实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的Android基于高德地图poi的仿微信获取位置功能实例代码全部内容,希望文章能够帮你解决Android基于高德地图poi的仿微信获取位置功能实例代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1143196.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存