欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android百度地图之公交线路详情搜索

程序员文章站 2024-03-04 14:35:47
本篇文章可以实现百度公交线路搜索,可以选择多条线路,具体实现代码如下: 一、公交线路详情检索 获取公交线路的详情主要分来两步,1.获取公交线路的uid,2.通过...

本篇文章可以实现百度公交线路搜索,可以选择多条线路,具体实现代码如下:

一、公交线路详情检索

获取公交线路的详情主要分来两步,1.获取公交线路的uid,2.通过uid获取公交线路详情。

1.获取公交线路的uid:

/*
     * 获得公交线路图的uid,并且根据系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, "未找到结果", 1);
        return;
      }
      // 定义一个公交线路描述类
      mkpoiinfo mkpoiinfo = null;
      // 获得本次就爱你所的公交线路的总站点
      int totalpoinum = arg0.getnumpois();
      for (int i = 0; i < totalpoinum; i++) {
        mkpoiinfo = arg0.getpoi(i);
        if (mkpoiinfo.epoitype == 2) {
          break;
        }
      }
      mksearch.buslinesearch("开封", mkpoiinfo.uid);
    }

2.获取公交线路详情:

/*
     * 得到公交线路图的详情
     * 
     * @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, "未找到结果", 1);
        return;
      }
      routeoverlay routeoverlay = new routeoverlay(mainactivity.this,
          mapview);
      routeoverlay.setdata(arg0.getbusroute());// 获得公交线路

      mapview.getoverlays().clear();// 清楚公交站点的标识
      mapview.getoverlays().add(routeoverlay);
      mapview.invalidate();
      // 采用动画形式描述公交站点
      mapview.getcontroller().animateto(arg0.getbusroute().getstart());
    }

查询时,只需提供城市名和线路

mksearch.poisearchincity("开封", "3");

完整代码:

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("开封", "3");
  }

  private class mysearchlistener implements mksearchlistener {

    @override
    public void ongetaddrresult(mkaddrinfo arg0, int arg1) {
      // todo auto-generated method stub

    }

    /*
     * 得到公交线路图的详情
     * 
     * @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, "未找到结果", 1);
        return;
      }
      routeoverlay routeoverlay = new routeoverlay(mainactivity.this,
          mapview);
      routeoverlay.setdata(arg0.getbusroute());// 获得公交线路

      mapview.getoverlays().clear();// 清楚公交站点的标识
      mapview.getoverlays().add(routeoverlay);
      mapview.invalidate();
      // 采用动画形式描述公交站点
      mapview.getcontroller().animateto(arg0.getbusroute().getstart());
    }

    @override
    public void ongetdrivingrouteresult(mkdrivingrouteresult arg0, int arg1) {
      // todo auto-generated method stub

    }

    @override
    public void ongetpoidetailsearchresult(int arg0, int arg1) {
      // todo auto-generated method stub

    }

    /*
     * 获得公交线路图的uid,并且根据系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, "未找到结果", 1);
        return;
      }
      // 定义一个公交线路描述类
      mkpoiinfo mkpoiinfo = null;
      // 获得本次就爱你所的公交线路的总站点
      int totalpoinum = arg0.getnumpois();
      for (int i = 0; i < totalpoinum; i++) {
        mkpoiinfo = arg0.getpoi(i);
        if (mkpoiinfo.epoitype == 2) {
          break;
        }
      }
      mksearch.buslinesearch("开封", 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-generated method stub

    }

    @override
    public void ongettransitrouteresult(mktransitrouteresult arg0, int arg1) {
      // todo auto-generated method stub

    }

    @override
    public void ongetwalkingrouteresult(mkwalkingrouteresult 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; this adds items to the action bar if it is present.
    getmenuinflater().inflate(r.menu.main, menu);
    return true;
  }

  @override
  protected boolean isroutedisplayed() {
    // todo auto-generated method stub
    return false;
  }

}

二、多条线路显示

在同一个地图上显示多条导航线路,也只需要分两步完成,

1.定义起始点个终点

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("北京", start, end);

2.将所有的线路详情循环显示在地图上

@override
    public void ongettransitrouteresult(mktransitrouteresult arg0, int arg1) {
      // todo auto-generated method stub
      if (arg0 == null) {
        return;
      }
      int plannum = arg0.getnumplan();// 获取所有的驾车路线
      for (int i = 0; i < plannum; i++) {
        transitoverlay overlay = new transitoverlay(mainactivity.this,
            mapview);
        overlay.setdata(arg0.getplan(i));
        mapview.getoverlays().add(overlay);
      }
      mapview.invalidate();
      mapview.getcontroller().animateto(arg0.getstart().pt);
    }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。