Android Baidu map API integration 3 search and android search
Book connection back
I. Basic Map interface address: http://www.cnblogs.com/dhr125/p/5969980.html
2. map location address: http://www.cnblogs.com/dhr125/p/5970118.html
Search function
1. Add the layout to the xml file
1 <LinearLayout 2 android: layout_width = "match_parent" 3 android: layout_height = "wrap_content"> 4 <EditText 5 android: layout_width = "0dip" 6 android: layout_height = "wrap_content" 7 android: layout_weight = "1" 8 android: hint = "Enter the search address" 9 android: id = "@ + id/etsearch"/> 10 <Button11 android: layout_width = "wrap_content" 12 android: layout_height = "wrap_content" 13 android: text = "Search" 14 android: id = "@ + id/btnsearch"/> 15 </LinearLayout>
2. Locate earearchdemo. java and prepare to copy the code.
3. Copy the following code to onCreate and enable the Activity to implement ongetaskearchresultlistener.
// Initialize the search module and register the search event listener mearch = receivearch. newInstance (); mreceivearch. setOnGetPoiSearchResultListener (this );
4. After implementation, the following three methods will be rewritten
1 @Override 2 public void onGetPoiResult(PoiResult poiResult) { 3 4 } 5 6 @Override 7 public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) { 8 9 }10 11 @Override12 public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {13 14 }
5. Add code to the override method onGetPoiResult and modify the code according to the error message.
1 if (poiResult = null | poiResult. error = SearchResult. ERRORNO. RESULT_NOT_FOUND) {2 Toast. makeText (MainActivity. this, "no results found", Toast. LENGTH_LONG) 3. show (); 4 return; 5} 6 if (poiResult. error = SearchResult. ERRORNO. NO_ERROR) {7 mBaiduMap. clear (); 8 PoiOverlay overlay = new MyPoiOverlay (mBaiduMap); 9 mBaiduMap. setOnMarkerClickListener (overlay); 10 overlay. setData (poiResult); 11 overlay. addToMap (); 12 overlay. zoomToSpan (); 13 showNearbyArea (center, radius); 14 15 return; 16}
6. Find two classes at the following position and copy them to the project.
7. Add code to the Project
1 private class MyPoiOverlay extends PoiOverlay { 2 3 public MyPoiOverlay(BaiduMap baiduMap) { 4 super(baiduMap); 5 } 6 7 @Override 8 public boolean onPoiClick(int index) { 9 super.onPoiClick(index);10 PoiInfo poi = getPoiResult().getAllPoi().get(index);11 // if (poi.hasCaterDetails) {12 mPoiSearch.searchPoiDetail((new PoiDetailSearchOption())13 .poiUid(poi.uid));14 // }15 return true;16 }17 }
1/** 2 * draw the search range. 3 * @ param center 4 * @ param radius 5 */6 public void showNearbyArea (LatLng center, int radius) {7 16}
8. Add the following two member variables to the member location.
LatLng center = null; int radius = 5000;
In addition, after the SDK listening function MyLocationListenner determines if, instantiate the center, as shown below:
@ Override public void onReceiveLocation (BDLocation location) {// After map view is destroyed, if (location = null | mMapView = null) {return ;} center = new LatLng (location. getLatitude (), location. getlongpolling ());
9. Find the custom input box and button and listen
1 etsearch = (EditText) findViewById(R.id.etsearch); 2 Button btnsearch = (Button) findViewById(R.id.btnsearch); 3 btnsearch.setOnClickListener(new View.OnClickListener() { 4 @Override 5 public void onClick(View v) { 6 PoiNearbySearchOption nearbySearchOption = new PoiNearbySearchOption().keyword(etsearch.getText() 7 .toString()).sortType(PoiSortType.distance_from_near_to_far).location(center) 8 .radius(radius).pageNum(10); 9 mPoiSearch.searchNearby(nearbySearchOption);10 }11 });
The search function is now available.