Android using Baidu Map API: Bus transfer Scheme search

Source: Internet
Author: User

This is the 3rd article I wrote about the call Baidu Map API Search service article, so the search to do before the other work no longer introduced, plus the code in the comments on the relevant operations, methods to do as much as possible detailed description, so directly to see the example bar.

1) Layout file Res/layout/main.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=  
"http://schemas.android.com/apk/" Res/android "
    android:orientation=" vertical "
    android:layout_width=" fill_parent "
    android:layout_" height= "Fill_parent"
    >  
    <com.baidu.mapapi.mapview android:id= "@+id/map_view"
        android:layout_ Width= "Fill_parent"
        android:layout_height= "fill_parent"
        android:clickable= "true"
    />  
</ Linearlayout>

2) inherited the activity class of com.baidu.mapapi.MapActivity

Package com.liufeng.baidumap;  
Import Android.os.Bundle;  
      
Import Android.util.Log;  
Import Com.baidu.mapapi.BMapManager;  
Import Com.baidu.mapapi.GeoPoint;  
Import Com.baidu.mapapi.MKAddrInfo;  
Import Com.baidu.mapapi.MKDrivingRouteResult;  
Import Com.baidu.mapapi.MKPlanNode;  
Import Com.baidu.mapapi.MKPoiResult;  
Import Com.baidu.mapapi.MKSearch;  
Import Com.baidu.mapapi.MKSearchListener;  
Import Com.baidu.mapapi.MKTransitRoutePlan;  
Import Com.baidu.mapapi.MKTransitRouteResult;  
Import Com.baidu.mapapi.MKWalkingRouteResult;  
Import com.baidu.mapapi.MapActivity;  
Import Com.baidu.mapapi.MapController;  
Import Com.baidu.mapapi.MapView;  
      
Import Com.baidu.mapapi.TransitOverlay;  
    public class Transitpolicyactivity extends Mapactivity {//define map engine management class private Bmapmanager Mapmanager;  
      
    Define Search service class private Mksearch mmksearch;  
    Private Mapview Mapview;  
      
    Private Mapcontroller Mapcontroller; @Override Public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);  
      
        Setcontentview (R.layout.main);  
        Initialize mapactivity Mapmanager = new Bmapmanager (Getapplication ());  
        The first parameter of the Init method is required to fill in the requested API Key Mapmanager.init ("285B415EBAB2A92293E85502150ADA7F03C777C4", null);  
      
        Super.initmapactivity (Mapmanager);  
        Mapview = (Mapview) Findviewbyid (R.id.map_view);  
        Set map mode for traffic map mapview.settraffic (true);  
      
        Set enable built-in zoom control Mapview.setbuiltinzoomcontrols (TRUE);  
      
        Construction of a latitude and longitude point (Guizhou University) Geopoint geopoint = new Geopoint (int) (26.449446 * 1E6), (int) (106.682949 * 1E6));  
        Obtain the Map controller object, which is used to control mapview Mapcontroller = Mapview.getcontroller ();  
        Set the center of the map Mapcontroller.setcenter (geopoint);  
      
        Set the map default zoom level Mapcontroller.setzoom (12); Initialize Mksearch mmksearch = new MKsearch ();  
      
        Mmksearch.init (Mapmanager, New Mysearchlistener ());  
        Starting point: Guizhou University Mkplannode Startplannode = new Mkplannode ();  
        startplannode.pt = new Geopoint ((int) (26.449446 * 1E6), (int) (106.682949 * 1E6));  
        End: Guizhou Province mobile Mkplannode endplannode = new Mkplannode ();  
        endplannode.pt = new Geopoint ((int) (26.601771 * 1E6), (int) (106.71968 * 1E6)); /** * Set up a bus transfer route search strategy, there are 4 strategies to choose from: * 1) excluding Metro: Mksearch.ebus_no_subway * 2) Time First: Mksearch.ebus 
         _time_first * 3 Minimum transfer: Mksearch.ebus_transfer_first * 4) min. Walk Distance: Mksearch.ebus_walk_first * * The search strategy we have chosen here is the minimum transfer, that is, the least number of interchange trips/Mmksearch.settransitpolicy (Mksearch.ebus_transfer_first  
        );  
    To be set in which city search Mmksearch.transitsearch ("Guiyang", Startplannode, Endplannode);  
    @Override protected Boolean isroutedisplayed () {return false;  
    }  
    @Override protected void OnDestroy () {if (Mapmanager!= null) {//program to call this method before exiting  
            Mapmanager.destroy ();  
        Mapmanager = null;  
    } Super.ondestroy ();  
            @Override protected void OnPause () {if (Mapmanager!= null) {//Terminate Baidu Map API  
        Mapmanager.stop ();  
    } super.onpause ();  
            @Override protected void Onresume () {if (Mapmanager!= null) {//open Baidu Map API  
        Mapmanager.start ();  
    } super.onresume (); /** * Implements Mksearchlistener interface for asynchronous Search service * * @author Liufeng/public class Myse Archlistener implements Mksearchlistener {/** * search for address information based on latitude and longitude results * @param result Search Results * @param ierror Error number (0 for correct return) */@Override public void Ongetaddrresult (mkaddrinfo Result, int ierror) {}/** * Driving route search Results * * @param result search results * @param ierror Error number (0 means correct return)/@Override public void Ongetdrivingrouteresult (Mkdrivingroutere  
         Sult result, int ierror) {}/** * Poi search results (range search, City poi search, peripheral search) * 
         * @param result Search results * @param type returns the result type (11,12,21:poi list 7: City list) * @param ierror Error number (0 indicates correct return)  
      
        */@Override public void Ongetpoiresult (mkpoiresult result, int type, int ierror) {} 
         /** * Bus transfer route Search results * * @param result search results * @param ierror Error number (0 indicates correct return)  
            */@Override public void Ongettransitrouteresult (mktransitrouteresult result, int ierror) {  
            if (result = = null) {return;  
      
  } log.d ("Liuyq", "Bus transfer scheme number:" + Result.getnumplan ());          Transitoverlay is a overlay transitoverlay Transitoverlay = new Trans that is provided by the Baidu Map API for displaying bus transfer routes on a map.  
            Itoverlay (Transitpolicyactivity.this, Mapview);  
            Show one of the transfer schemes Transitoverlay.setdata (Result.getplan (0));  
                  
            Show Mapview.getoverlays (). Add (Transitoverlay) on the map; /** * If you need to show all the bus transfer options on the map, please comment 165-170 lines and open the comment line 176-186 * But as the usual return of the number of bus transfer schemes, all show on the map will be very messy, may not be able to distinguish, so only 
            Shows one of the schemes *//** transitoverlay transitoverlay = null; Traversal search results, get all transfer scheme for (int i=0; I&lt;result.getnumplan (); i++) {transitoverlay = new Transi 
                Toverlay (Transitpolicyactivity.this, Mapview); 
                Set up Display data Transitoverlay.setdata (Result.getplan (i)); 
            Show Mapview.getoverlays (). Add (Transitoverlay) on the map;
                  
         } 
            */   /** * Description: * 1 bus transfer route search results usually have a variety of transfer programs; 
             * 2 The number of transfer schemes can be obtained through Result.getnumplan ().  
            * In addition to marking one of the bus transfer schemes on the map, if we want to get information on all transfer schemes, we need to traverse the search results///traverse the search results as follows, and get all the transfer schemes for (int i=0; I&lt;result.getnumplan (); i++) {//Bus Interchange scheme details class Mktransitroute  
                Plan Routeplan = Result.getplan (i); 
         Then according to the method provided by the Mktransitrouteplan class to obtain specific transfer information (omitted)}}/** * Walking route search Results * * @param result search results * @param ierror Error number (0 indicates correct return)/@Override public void Ongetwalkingrouteresult (mkwalkingrouteresult result, int ierror) {}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.