Function realization of android inquiry bus and several stations

Copyright Statement: This article is the original article of the blogger. It can not be reproduced without the permission of the blogger.

            Android Baidu Map-based Enquiry of Peripheral Buses

  
The last article introduced how to use Baidu Map to locate on Android platform. Next, it introduced how to use Baidu Map to obtain the surrounding bus information based on the location function.

In the same way as above, write a single class, which encapsulates the peripheral search function, Nearby SearchHelper. This class exposes only one function SearchNearby() interface to the outside world.

When implementing the main process, the information of the surrounding bus is obtained according to the current location, and the redundant bus route information is excluded. According to the information of the bus route searched, the uid of the bus (the unique symbol of Baidu map) is continued to be searched, and then the detailed bus information, including the starting site, is searched according to the specific uid. The code is as follows:

1. Create a Search Helper Class for Peripheral Buses: Nearby SearchHelper

  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3.   
  4. import com.baidu.mapapi.model.LatLng;  
  5. import com.baidu.mapapi.search.busline.BusLineResult;  
  6. import com.baidu.mapapi.search.busline.BusLineSearch;  
  7. import com.baidu.mapapi.search.busline.OnGetBusLineSearchResultListener;  
  8. import com.baidu.mapapi.search.core.PoiInfo;  
  9. import com.baidu.mapapi.search.core.SearchResult;  
  10. import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;  
  11. import com.baidu.mapapi.search.poi.PoiDetailResult;  
  12. import com.baidu.mapapi.search.poi.PoiNearbySearchOption;  
  13. import com.baidu.mapapi.search.poi.PoiResult;  
  14. import com.baidu.mapapi.search.poi.PoiSearch;  
  15. import com.baidu.mapapi.utils.DistanceUtil;  
  16. import com.busleep.app.CustomApplication;  
  17. import com.busleep.bean.MrNearbyBus;  
  18. import com.busleep.config.Constant;  
  19. import com.busleep.listener.MrNearbySearchListener;  
  20. import com.busleep.utils.LogUtils;  
  21.   
  22. /** 
  23.  * There are two interfaces of Baidu Map, one is Poi Search Monitor, the other is Bus Route Search Monitor. 
  24.  * @author Render; 
  25.  */  
  26. public class NearbySearchHelper implements OnGetPoiSearchResultListener,  
  27.   
  28.     OnGetBusLineSearchResultListener{  
  29.   
  30.     public static final String TAG = "NearbySearchHelper";  
  31.       
  32.     private final int BUSSTATION=0;                    //Inquire about the information of the surrounding bus stops first;  
  33.     private final int BUSLINE=1;                           //Bus information of the site;  
  34.     private PoiSearch mSearch = null;                 //Poi Point Search Module;  
  35.     private BusLineSearch mBusLineSearch = null;    //Bus route search module;  
  36.       
  37.     private int mType;                                   //Query type;  
  38.     private int nodeIndex=-1;                        //Index of queries;  
  39.       
  40.     private MrNearbySearchListener nearBySearchListener; //Query End Listener Object, which is a new listener for encapsulation;  
  41.       
  42.     private List<MrNearbyBus> nearbyBuses=null;         //The results of the query;  
  43.       
  44.     public NearbySearchHelper(){  
  45.       
  46.     }  
  47.   
  48.     public void setNearBySearchListener(MrNearbySearchListener nearBySearchListener) {  
  49.         this.nearBySearchListener = nearBySearchListener;  
  50.     }  
  51.   
  52.     /** 
  53.      * Initialization function is mainly to initialize the search object of Baidu Map and the linked list of the surrounding bus information. As for the Bean entity object MrNearby Bus, we will continue to introduce it below. 
  54.      */  
  55.     public void init(){  
  56.           
  57.         if(mSearch==null||mBusLineSearch==null){  
  58.               
  59.             mSearch = PoiSearch.newInstance();  
  60.             mSearch.setOnGetPoiSearchResultListener(this);  
  61.             mBusLineSearch = BusLineSearch.newInstance();  
  62.             mBusLineSearch.setOnGetBusLineSearchResultListener(this);  
  63.               
  64.             nearbyBuses=new ArrayList<MrNearbyBus>();  
  65.               
  66.         }else {  
  67.             nearbyBuses.clear();  
  68.         }  
  69.     }  
  70.       
  71.     /** 
  72.      * Destruction function; 
  73.      */  
  74.     public void Destory(){  
  75.           
  76.         mSearch.destroy();  
  77.         mBusLineSearch.destroy();  
  78.     }  
  79.   
  80.      /** 
  81.      * Search for peripheral functions; called directly by external programs; 
  82.      */  
  83.   
  84.     public void searchNearby(){       
  85.   
  86.        //Setting the current search type is the search for the bus site;  
  87.   
  88.         mType=BUSSTATION;  
  89.   
  90.        //Obtain the longitude and latitude of the current position, which is obtained by the above positioning method;  
  91.   
  92.         double latitude=CustomApplication.mLastLocation.getLatitude();  
  93.         double longtitude=CustomApplication.mLastLocation.getLongitude();  
  94.           
  95.         //Search the surrounding bus routes, use the Baidu map's peripheral search interface, and define the search options for peripheral points, where Constant.nearbyRadius is the radius of the peripheral search;  
  96.         mSearch.searchNearby(new PoiNearbySearchOption().keyword("Bus stop")  
  97.                 .location(new LatLng(latitude,longtitude)).radius(Constant.nearbyRadius));  
  98.           
  99.     }  
  100.       
  101.     @Override  
  102.     public void onGetBusLineResult(BusLineResult arg0) {  
  103.         // TODO Auto-generated method stub  
  104.           
  105.     }  
  106.   
  107.     @Override  
  108.     public void onGetPoiDetailResult(PoiDetailResult arg0) {  
  109.         // TODO Auto-generated method stub  
  110.           
  111.     }  
  112.   
  113.     //This function is a search callback function for the peripheral interest points of Baidu Map, which calls back in both places, so it needs to be judged according to the current search type.  
  114.   
  115.     @Override  
  116.     public void onGetPoiResult(PoiResult result) {  
  117.           
  118.         switch(mType)  
  119.         {  
  120.             case BUSSTATION:  
  121.             {  
  122.                 //If the query fails, return directly.  
  123.                 if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR)  
  124.                 {  
  125.                     LogUtils.i(TAG, "Query site failed!");  
  126.                     nearBySearchListener.onRefreshBusNearby(nearbyBuses);  
  127.                     return;  
  128.                 }  
  129.                 //Processing the search results of bus sites;  
  130.                 ProcessBusStation(result);  
  131.                 break;  
  132.             }  
  133.             case BUSLINE:  
  134.             {  
  135.                 //If the query fails, return directly.  
  136.                 if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR)  
  137.                 {  
  138.                     LogUtils.i(TAG, "Query line failed!");  
  139.                     nearBySearchListener.onRefreshBusNearby(nearbyBuses);  
  140.                     return;  
  141.                 }  
  142.                 //Processing the search results of bus routes;  
  143.                 ProcessBusLine(result);  
  144.                 break;  
  145.             }  
  146.         }  
  147.     }  
  148.   
  149.     /** 
  150.      * The main purpose is to find out whether there are the same buses in the site, and if there are, take two buses nearest to the current location. 
  151.      * Because it is searching for the surrounding bus stops, there may be a bus passing through two or three bus stops in order to eliminate redundant information. 
  152.      * Therefore, it is necessary to judge which station is nearest to the current location of the same bus. 
  153.      * @return 
  154.      */  
  155.   
  156.     private boolean FindSameBus(MrNearbyBus nearbyBus){  
  157.           
  158.         String busName=nearbyBus.getBusName();  
  159.           
  160.         LatLng nowLocation=new LatLng(CustomApplication.mLastLocation.getLatitude(),  
  161.                 CustomApplication.mLastLocation.getLongitude());  
  162.           
  163.         LatLng newLocation=nearbyBus.getStationLaction();  
  164.           
  165.         double newDistance=DistanceUtil.getDistance(nowLocation, newLocation);  
  166.         double oldDistance=0.0;  
  167.           
  168.         for(int i=0;i<nearbyBuses.size();i++)  
  169.         {  
  170.             //If there is a bus line with the same name;  
  171.             if(nearbyBuses.get(i).getBusName().equals(busName))  
  172.             {  
  173.                 LatLng oldLocation=nearbyBuses.get(i).getStationLaction();  
  174.   
  175.                //Distance Util is a tool class provided by Baidu Map to calculate the distance between two latitude and longitude coordinates.  
  176.   
  177.                 oldDistance=DistanceUtil.getDistance(nowLocation, oldLocation);  
  178.                   
  179.                 //If the current distance is less than the previous distance, the bus information will be updated.  
  180.                 if(newDistance<oldDistance)  
  181.                 {  
  182.                     nearbyBuses.get(i).setStationLaction(newLocation);  
  183.                     nearbyBuses.get(i).setStationName(nearbyBus.getStationName());  
  184.                 }  
  185.                 return true;  
  186.             }  
  187.         }  
  188.         return false;  
  189.     }  
  190.       
  191.     /** 
  192.      * The result of query processing is the situation of bus station. 
  193.      * @param result 
  194.      */  
  195.     private void ProcessBusStation(PoiResult result){  
  196.           
  197.         for (PoiInfo poi : result.getAllPoi())  
  198.         {  
  199.             if (poi.type == PoiInfo.POITYPE.BUS_STATION)  
  200.             {      
  201.                 String busNames []=poi.address.split(";");  
  202.                   
  203.                 for(int i=0;i<busNames.length;++i){  
  204.                       
  205.                     MrNearbyBus nearbyBus=new MrNearbyBus();  
  206.                     nearbyBus.setBusName(busNames[i]);  
  207.                     nearbyBus.setStationName(poi.name);  
  208.                     nearbyBus.setStationLaction(poi.location);  
  209.                       
  210.                     boolean bRes=FindSameBus(nearbyBus);  
  211.                       
  212.                     if(!bRes)  
  213.                     {  
  214.                         nearbyBuses.add(nearbyBus);  
  215.                     }          
  216.                 }  
  217.             }  
  218.         }  
  219.   
  220.         //After searching the bus site, the bus route will be searched according to the bus site. Because the search time may be long, the search listener will be called after searching the bus site.  
  221.   
  222.         //To refresh data, the UI thread implements the listener, which can refresh data directly.  
  223.   
  224.         searchBusLine();  
  225.         nearBySearchListener.onRefreshBusNearby(nearbyBuses);  
  226.     }  
  227.       
  228.     /** 
  229.      * Query bus routes; 
  230.      */  
  231.     private void searchBusLine(){  
  232.         //NoeIndex represents the index of the current surrounding site;  
  233.   
  234.         nodeIndex++;  
  235.           
  236.         mType=BUSLINE;  
  237.           
  238.         if(nearbyBuses.isEmpty()){  
  239.             return;  
  240.         }  
  241.           
  242.         //Partial refresh;  
  243.         if(nodeIndex%5==0){  
  244.             nearBySearchListener.onRefreshBusNearby(nearbyBuses);  
  245.         }  
  246.           
  247.         //If returned at this time, the query is completed.  
  248.         if(nodeIndex >= nearbyBuses.size()){  
  249.             LogUtils.i(TAG, "Fine the query!");  
  250.             nearBySearchListener.onRefreshBusNearby(nearbyBuses);  
  251.             return;  
  252.         }  
  253.           
  254.         //Here we still use the method of searching the surrounding area, which will be more efficient.  
  255.         double latitude=CustomApplication.mLastLocation.getLatitude();  
  256.         double longtitude=CustomApplication.mLastLocation.getLongitude();  
  257.         //Get the bus name of the surrounding bus under the current node;  
  258.   
  259.         String busLineName=nearbyBuses.get(nodeIndex).getBusName();  
  260.         mSearch.searchNearby(new PoiNearbySearchOption().keyword(busLineName+"transit")  
  261.                 .location(new LatLng(latitude,longtitude)).radius(Constant.nearbyRadius));  
  262.     }  
  263.       
  264.     /** 
  265.      * Processing bus route information; 
  266.      * @param result 
  267.      */  
  268.     private void ProcessBusLine(PoiResult result)  
  269.     {  
  270.         for (PoiInfo poi : result.getAllPoi())  
  271.         {  
  272.             if (poi.type == PoiInfo.POITYPE.BUS_LINE)  
  273.             {  
  274.                 MrNearbyBus nearbyBus=nearbyBuses.get(nodeIndex);  
  275.                   
  276.                 //If it is the first time to come in;  
  277.                 if(nearbyBus.getUid()==null){  
  278.       
  279.                     nearbyBus.setUid(poi.uid);  
  280.                     String drection=poi.name;  
  281.                     int index=drection.indexOf("(");  
  282.                     int length=drection.length();  
  283.                       
  284.                     drection=drection.substring(index+1,length-1);  
  285.                     nearbyBus.setBusDrection(drection);  
  286.                       
  287.                 }else {    //Otherwise, it's a return bus.  
  288.                       
  289.                     nearbyBus.setReverseUid(poi.uid);;  
  290.                 }  
  291.             }  
  292.         }  
  293.         searchBusLine();  
  294.     }  
  295. }  


2. In the UI, the Nearby SearchHelper object can quickly search the bus information around it. The code is as follows:

  1. public class SearchNearbyActivity extends Activity implements MrNearbySearchListener {  
  2.   
  3.     private NearbySearchHelper mHelper=null;  
  4.       
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.activity_splash);  
  9.   
  10.         /** 
  11.          * Create a peripheral search help class, initialize the help class, and set up a listener for the help class. 
  12.          */  
  13.         nearbySearchHelper=new NearbySearchHelper();  
  14.         nearbySearchHelper.init();  
  15.         nearbySearchHelper.setNearBySearchListener(this);  
  16.     
  17.     }  
  18.   
  19.     @Override  
  20.     public void onRefreshBusNearby(List<MrNearbyBus> list) {  
  21.          
  22.         for(int i=0;i<list.size();i++){  
  23.               
  24.             LogUtils.i(TAG, list.get(i).getBusName());      
  25.         }  
  26.   
  27.         //BusNear Adapter is an adapter for displaying bus information.  
  28.   
  29.         if(busNearAdapter==null){  
  30.             busNearAdapter=new BusNearAdapter(getActivity(), list);  
  31.             mBusNearListView.setAdapter(busNearAdapter);  
  32.         }  
  33.         else {  
  34.             busNearAdapter.updateListView(list);  
  35.         }  
  36.     }   
  37. }  

3. For the bean object mentioned above, which saves the surrounding bus information, it is a self-defined data object. The MrNearbyBus code is as follows:

  1. package com.busleep.bean;  
  2.   
  3. import com.baidu.mapapi.model.LatLng;  
  4.   
  5. /** 
  6.  * Bus information around; 
  7.  * @author Render; 
  8.  */  
  9. public class MrNearbyBus {  
  10.   
  11.     /** 
  12.      * Bus name; 
  13.      */  
  14.     private String busName=null;  
  15.       
  16.     /** 
  17.      * uid of the bus line; 
  18.      */  
  19.     private String uid=null;  
  20.       
  21.     /** 
  22.      * Uid of reverse bus; 
  23.      */  
  24.     private String reverseUid=null;  
  25.       
  26.     /** 
  27.      * Find the name of the surrounding station and the bus line according to that name. 
  28.      */  
  29.     private String stationName=null;  
  30.       
  31.     /** 
  32.      * Site location; 
  33.      */  
  34.     private LatLng stationLaction=null;  
  35.       
  36.     /** 
  37.      * The direction to go; 
  38.      */  
  39.     private String busDrection=null;  
  40.       
  41.     /** 
  42.      * Constructor; 
  43.      */  
  44.     public MrNearbyBus(){  
  45.           
  46.     }  
  47.       
  48.     public String getBusName() {  
  49.         return busName;  
  50.     }  
  51.   
  52.     public String getUid() {  
  53.         return uid;  
  54.     }  
  55.   
  56.     public void setUid(String uid) {  
  57.         this.uid = uid;  
  58.     }  
  59.   
  60.     public String getReverseUid() {  
  61.         return reverseUid;  
  62.     }  
  63.   
  64.     public void setReverseUid(String reverseUid) {  
  65.         this.reverseUid = reverseUid;  
  66.     }  
  67.   
  68.     public void setBusName(String busName) {  
  69.         this.busName = busName;  
  70.     }  
  71.   
  72.     public String getStationName() {  
  73.         return stationName;  
  74.     }  
  75.   
  76.     public void setStationName(String stationName) {  
  77.         this.stationName = stationName;  
  78.     }  
  79.   
  80.     public LatLng getStationLaction() {  
  81.         return stationLaction;  
  82.     }  
  83.   
  84.     public void setStationLaction(LatLng stationLaction) {  
  85.         this.stationLaction = stationLaction;  
  86.     }  
  87.   
  88.     public String getBusDrection() {  
  89.         return busDrection;  
  90.     }  
  91.   
  92.     public void setBusDrection(String busDrection) {  
  93.         this.busDrection = busDrection;  
  94.     }  
  95. }  

4. The final results of the peripheral bus query are as follows:

Keywords: Android Java less

Added by liljim on Sat, 29 Jun 2019 02:57:25 +0300