您好,登錄后才能下訂單哦!
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, 0, 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, 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) { //點擊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("", 0, currentInfo.get("cityCode"), 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, 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 關鍵字 * @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, LinearLayout.LayoutParams.MATCH_PARENT, 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的仿微信獲取位置功能實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。