Android Baidu Map of the bus line details search _android

Source: Internet
Author: User
Tags stub

This article can realize Baidu bus line search, you can select more than one line, the specific implementation code as follows:

First, the bus route details search

The details of the bus line are mainly divided into two steps, 1. Get the uid,2 of the bus line. Get the details of the bus line by UID.

1. Get the UID of the bus line:


     * * Obtain the UID of the bus route map and initiate the search for details of the bus line based on the UID
     * * @see *
     Com.baidu.mapapi.mksearchlistener#ongetpoiresult ( Com.baidu.mapapi
     *. Mkpoiresult, int, int)
     *
    /@Override public
    void Ongetpoiresult (Mkpoiresult arg0, int arg1, int arg2) {
      / /TODO auto-generated Method Stub
      if (arg0 = null | | | arg2!= 0) {
        toast.maketext (mainactivity.this, "No results found", 1) ;
        return;
      }
      Define a bus line description class
      mkpoiinfo mkpoiinfo = null;
      Get this time love you the total site of the bus line
      int totalpoinum = Arg0.getnumpois ();
      for (int i = 0; i < Totalpoinum i++) {
        mkpoiinfo = Arg0.getpoi (i);
        if (Mkpoiinfo.epoitype = = 2) {break
          ;
        }
      }
      Mksearch.buslinesearch ("Kaifeng", Mkpoiinfo.uid);
    }

2. Get Bus line details:

* *
     to obtain details of the bus route map * *
     @see *
     com.baidu.mapapi.mksearchlistener#ongetbusdetailresult (com.baidu).
     * Mapapi. Mkbuslineresult, int)
     *
    /@Override public
    void Ongetbusdetailresult (Mkbuslineresult arg0, int arg1) {
      //TODO auto-generated method Stub
      if (arg0 = null | | | arg1!= 0) {
        toast.maketext (mainactivity.this, "no Knot found Fruit ", 1);
        return;
      }
      Routeoverlay routeoverlay = new Routeoverlay (mainactivity.this,
          mapview);
      Routeoverlay.setdata (Arg0.getbusroute ())//Get Bus line

      mapview.getoverlays (). Clear ()//clearly identify
      the bus site Mapview.getoverlays (). Add (routeoverlay);
      Mapview.invalidate ();
      Use animated form to describe the bus site
      mapview.getcontroller (). Animateto (Arg0.getbusroute (). Getstart ());
    

When querying, just provide city name and line

Mksearch.poisearchincity ("Kaifeng", "3");

Complete code:

Package com.arlen.android.baidumap08;
Import Android.os.Bundle;
Import Android.view.Menu;

Import Android.widget.Toast;
Import Com.baidu.mapapi.BMapManager;
Import Com.baidu.mapapi.MKAddrInfo;
Import Com.baidu.mapapi.MKBusLineResult;
Import Com.baidu.mapapi.MKDrivingRouteResult;
Import Com.baidu.mapapi.MKGeneralListener;
Import Com.baidu.mapapi.MKPoiInfo;
Import Com.baidu.mapapi.MKPoiResult;
Import Com.baidu.mapapi.MKSearch;
Import Com.baidu.mapapi.MKSearchListener;
Import Com.baidu.mapapi.MKSuggestionResult;
Import Com.baidu.mapapi.MKTransitRouteResult;
Import Com.baidu.mapapi.MKWalkingRouteResult;
Import com.baidu.mapapi.MapActivity;
Import Com.baidu.mapapi.MapView;

Import Com.baidu.mapapi.RouteOverlay;
  public class Mainactivity extends Mapactivity {private Bmapmanager bmapmanager;
  Private Mapview Mapview;
  Private String key = "43d220aab0dec976d2eabc83cfc9eee52bccca7a";

  Private Mksearch Mksearch; @Override protected void OnCreate (Bundle savedinstancestate) {supEr.oncreate (savedinstancestate);

    Setcontentview (R.layout.activity_main);
    Bmapmanager = new Bmapmanager (mainactivity.this);
        Bmapmanager.init (Key, New Mkgenerallistener () {@Override public void ongetpermissionstate (int arg0) { TODO auto-generated Method stub} @Override public void ongetnetworkstate (int arg0) {//
    TODO auto-generated Method stub}});
    Initmapactivity (Bmapmanager);
    Mapview = (Mapview) Findviewbyid (R.id.bmapview);
    Mksearch = new Mksearch ();

    Mksearch.init (Bmapmanager, New Mysearchlistener ());
  Mksearch.poisearchincity ("Kaifeng", "3"); Private class Mysearchlistener implements Mksearchlistener {@Override public void Ongetaddrresult (Mkaddrinf o arg0, int arg1) {//TODO auto-generated Method stub}/* * Get details of bus route map * * @see * C
     Om.baidu.mapapi.mksearchlistener#ongetbusdetailresult (Com.baidu. * Mapapi.
Mkbuslineresult, int) * *    @Override public void Ongetbusdetailresult (Mkbuslineresult arg0, int arg1) {//TODO auto-generated method s
        Tub if (arg0 = null | | arg1!= 0) {toast.maketext (Mainactivity.this, "No results found", 1);
      Return
      } routeoverlay Routeoverlay = new Routeoverlay (mainactivity.this, Mapview); Routeoverlay.setdata (Arg0.getbusroute ());//Get Bus line Mapview.getoverlays (). Clear ()//clearly identify the bus site identification Mapview.getover
      Lays (). Add (Routeoverlay);
      Mapview.invalidate ();
    The public transport site Mapview.getcontroller () Animateto (Arg0.getbusroute (). Getstart ()) is described in animated form. @Override public void Ongetdrivingrouteresult (Mkdrivingrouteresult arg0, int arg1) {//TODO Auto-generat Ed method stub} @Override public void Ongetpoidetailsearchresult (int arg0, int arg1) {//TODO auto-g enerated Method Stub} * * Obtains the UID of the bus route map and initiates the search for details of the bus line based on the UID * * @see * Com.baidu.mapapi.MKS Earchlistener#ongetpOiresult (COM.BAIDU.MAPAPI *.
      Mkpoiresult, int, int) */@Override public void Ongetpoiresult (Mkpoiresult arg0, int arg1, int arg2) { TODO auto-generated Method Stub if (arg0 = null | | | arg2!= 0) {toast.maketext (Mainactivity.this, "No results found
        ", 1);
      Return
      //define a bus line description class Mkpoiinfo mkpoiinfo = null;
      Get this time love you the total site of the bus line int totalpoinum = Arg0.getnumpois ();
        for (int i = 0; i < Totalpoinum i++) {mkpoiinfo = Arg0.getpoi (i);
        if (Mkpoiinfo.epoitype = = 2) {break;
    } mksearch.buslinesearch ("Kaifeng", Mkpoiinfo.uid);

    @Override public void Ongetrgcshareurlresult (String arg0, int arg1) {//TODO auto-generated method stub @Override public void Ongetsuggestionresult (Mksuggestionresult arg0, int arg1) {//TODO Auto-genera Ted Method stub} @Override the public void Ongettransitrouteresult (Mktransitrouteresult arg0, int ARG1) {//TODO auto-generated method stub} @Override public void Ongetwalkingrouteresult (Mkwalkingr  Outeresult arg0, int arg1) {//TODO auto-generated Method stub}} @Override protected void OnDestroy ()
    {//TODO auto-generated Method Stub Super.ondestroy ();
      if (Bmapmanager!= null) {Bmapmanager.destroy ();
    Bmapmanager = null;
    } @Override protected void Onresume () {//TODO auto-generated Method Stub super.onresume ();
    if (Bmapmanager!= null) {Bmapmanager.start ();
    } @Override protected void OnPause () {//TODO auto-generated Method Stub super.onpause ();
    if (Bmapmanager!= null) {bmapmanager.stop (); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; the adds items to the Actio
    n Bar if it is present.
    Getmenuinflater (). Inflate (R.menu.main, menu);
  return true; } @Override protected Boolean IsroutedisPlayed () {//TODO auto-generated method stub return false;

 }

}

Two, multiple line display

Displaying multiple navigation lines on the same map only needs to be done in two steps,

1. Define Start point End

start = new Mkplannode ();
    start.pt = new Geopoint ((int) (39.915 * 1E6), (int) (116.404 * 1E6));
    end = new Mkplannode ();
    end.pt = new Geopoint (40057031, 116307852);

    Mksearch.transitsearch ("Beijing", Start, end);

2. Circulate all the circuit details on the map

@Override public
    void Ongettransitrouteresult (Mktransitrouteresult arg0, int arg1) {
      //TODO auto-generated Method Stub
      if (arg0 = = null) {return
        ;
      }
      int plannum = Arg0.getnumplan ();//Get all driving routes for
      (int i = 0; i < Plannum; i++) {
        Transitoverlay overlay = new T Ransitoverlay (Mainactivity.this,
            mapview);
        Overlay.setdata (Arg0.getplan (i));
        Mapview.getoverlays (). Add (overlay);
      Mapview.invalidate ();
      Mapview.getcontroller (). Animateto (Arg0.getstart (). PT);
    

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.