您好,登錄后才能下訂單哦!
1.打開頁面自動(dòng)定位,同時(shí)搜索周邊所有poi
2.點(diǎn)擊搜索按鈕,輸入關(guān)鍵子,獲取關(guān)鍵字搜索結(jié)果
3。選取listview中的一項(xiàng)即可定位到該位置,或者獲取任何消息
4.文件類
1、MapActivity
public class MapActivity extends Activity implements PoiSearch.OnPoiSearchListener { private MapView mMapView = null; private AMap aMap; private MyLocationStyle myLocationStyle; //poiSearch相關(guān) private PoiSearch poiSearch; private PoiSearch.Query query; boolean isPoiSearched = false; //是否進(jìn)行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執(zhí)行onCreate時(shí)執(zhí)行mMapView.onCreate(savedInstanceState),創(chuàng)建地圖 mMapView.onCreate(savedInstanceState); initAMap(); } /** * 獲取view對(duì)象,初始化一些對(duì)象 */ 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); //設(shè)置標(biāo)題字體 tf = Typeface.createFromAsset(getAssets(), "font/f1.ttf"); (title = (TextView) findViewById(R.id.title)).setTypeface(tf); myHandler = new MyHandler(); } /** * 設(shè)置點(diǎn)擊事件 */ void setAllViewOnclickLinster() { //當(dāng)搜索圖標(biāo)點(diǎn)擊時(shí),切換顯示效果 searchIv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (title.getVisibility() == View.VISIBLE) { hideTitle(); } else if (title.getVisibility() == View.GONE) { showTitle(); } } }); //點(diǎn)擊搜索按鈕時(shí),搜索關(guān)鍵字 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, 0, currentInfo.get("cityCode"), false); } } }); //使editText監(jiān)聽回車事件,進(jìn)行搜索,效果同上 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, 0, currentInfo.get("cityCode"), 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) { //點(diǎn)擊ok的處理事件 //獲取數(shù)據(jù)并返回上一個(gè)activity即可 } }); //listview點(diǎn)擊事件 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()); //在地圖上添加一個(gè)marker,并將地圖中移動(dòng)至此處 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); //存儲(chǔ)當(dāng)前點(diǎn)擊位置 selectIndex = i; //存儲(chǔ)當(dāng)前點(diǎn)擊view,并修改view和上一個(gè)選中view的定位圖標(biāo) 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() { //初始化地圖控制器對(duì)象 if (aMap == null) { aMap = mMapView.getMap(); } //地圖加載監(jiān)聽器 aMap.setOnMapLoadedListener(new AMap.OnMapLoadedListener() { @Override public void onMapLoaded() { //aMap.setMapType(); aMap.setMyLocationEnabled(true); aMap.animateCamera(CameraUpdateFactory.zoomTo(aMap.getMaxZoomLevel() - 1)); } }); myLocationStyle = new MyLocationStyle();//初始化定位藍(lán)點(diǎn)樣式類 myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE);//連續(xù)定位、且將視角移動(dòng)到地圖中心點(diǎn),定位點(diǎn)依照設(shè)備方向旋轉(zhuǎn),并且會(huì)跟隨設(shè)備移動(dòng)。(1秒1次定位)如果不設(shè)置myLocationType,默認(rèn)也會(huì)執(zhí)行此種模式。 myLocationStyle.interval(2000); //設(shè)置連續(xù)定位模式下的定位間隔,只在連續(xù)定位模式下生效,單次定位模式下不會(huì)生效。單位為毫秒。 myLocationStyle.radiusFillColor(0x70f3ff); myLocationStyle.strokeColor(0xe3f9fd); aMap.setMyLocationStyle(myLocationStyle);//設(shè)置定位藍(lán)點(diǎn)的Style aMap.getUiSettings().setMyLocationButtonEnabled(true);//設(shè)置默認(rèn)定位按鈕是否顯示,非必需設(shè)置。 aMap.setMyLocationEnabled(true);// 設(shè)置為true表示啟動(dòng)顯示定位藍(lán)點(diǎn),false表示隱藏定位藍(lán)點(diǎn)并不進(jìn)行定位,默認(rèn)是false。 aMap.setMaxZoomLevel(aMap.getMaxZoomLevel()); //地址監(jiān)聽事件 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()); //存儲(chǔ)定位數(shù)據(jù) 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("", 0, currentInfo.get("cityCode"), true); //latitude=41.652146#longitude=123.427205#province=遼寧省#city=沈陽市#district=渾南區(qū)#cityCode=024#adCode=210112#address=遼寧省沈陽市渾南區(qū)創(chuàng)新一路靠近東北大學(xué)渾南校區(qū)#country=中國#road=創(chuàng)新一路#poiName=東北大學(xué)渾南校區(qū)#street=創(chuàng)新一路#streetNum=193號(hào)#aoiName=東北大學(xué)渾南校區(qū)#poiid=#floor=#errorCode=0#errorInfo=success#locationDetail=24 #csid:1cce9508143d493182a8da7745eb07b3#locationType=5 } } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { //點(diǎn)擊返回鍵時(shí),將瀏覽器后退 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中數(shù)據(jù) 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, View view, 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 關(guān)鍵字 * @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表示搜索字符串, //第二個(gè)參數(shù)表示POI搜索類型,二者選填其一, //POI搜索類型共分為以下20種:汽車服務(wù)|汽車銷售| //汽車維修|摩托車服務(wù)|餐飲服務(wù)|購物服務(wù)|生活服務(wù)|體育休閑服務(wù)|醫(yī)療保健服務(wù)| //住宿服務(wù)|風(fēng)景名勝|(zhì)商務(wù)住宅|政府機(jī)構(gòu)及社會(huì)團(tuán)體|科教文化服務(wù)|交通設(shè)施服務(wù)| //金融保險(xiǎn)服務(wù)|公司企業(yè)|道路附屬設(shè)施|地名地址信息|公共設(shè)施 //cityCode表示POI搜索區(qū)域,可以是城市編碼也可以是城市名稱,也可以傳空字符串,空字符串代表全國在全國范圍內(nèi)進(jìn)行搜索 query.setPageSize(50);// 設(shè)置每頁最多返回多少條poiitem query.setPageNum(pageNum);//設(shè)置查詢頁碼 poiSearch = new PoiSearch(this, query); poiSearch.setOnPoiSearchListener(this); if (nearby) poiSearch.setBound(new PoiSearch.SearchBound(new LatLonPoint(mCurrentLat, mCurrentLng), 1500));//設(shè)置周邊搜索的中心點(diǎn)以及半徑 poiSearch.searchPOIAsyn(); } @Override public void onPoiSearched(PoiResult poiResult, int i) { int index = 0; //填充數(shù)據(jù),并更新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執(zhí)行onDestroy時(shí)執(zhí)行mMapView.onDestroy(),銷毀地圖 mMapView.onDestroy(); } @Override protected void onResume() { super.onResume(); //在activity執(zhí)行onResume時(shí)執(zhí)行mMapView.onResume (),重新繪制加載地圖 mMapView.onResume(); } @Override protected void onPause() { super.onPause(); //在activity執(zhí)行onPause時(shí)執(zhí)行mMapView.onPause (),暫停地圖的繪制 mMapView.onPause(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); //在activity執(zhí)行onSaveInstanceState時(shí)執(zhí)行mMapView.onSaveInstanceState (outState),保存地圖當(dāng)前的狀態(tài) mMapView.onSaveInstanceState(outState); } /** * 顯示標(biāo)題欄,即默認(rèn)狀態(tài) */ void showTitle() { //顯示標(biāo)題欄 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); } /** * 隱藏標(biāo)題欄,即進(jìn)行搜索 */ 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, LinearLayout.LayoutParams.MATCH_PARENT, 50)); mMapView.setVisibility(View.GONE); onSearch = true; } /** * 強(qiáng)制關(guān)閉軟鍵盤 */ 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); //關(guān)閉軟鍵盤,開啟方法相同,這個(gè)方法是切換開啟與關(guān)閉狀態(tài)的 } } }
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="大會(huì)的反饋撒" android:textColor="#000" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/sub" android:textSize="15dp" android:text="大會(huì)的反饋撒" 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>
總結(jié)
以上所述是小編給大家介紹的Android基于高德地圖poi的仿微信獲取位置功能實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。