【Android】3.14 公交线路查询功能
分类:C#、Android、VS2015、百度地图应用; 创建日期:2016-02-04
一、简介
利用BusLineSearch方法可查询公交线路的详情信息。
二、运行截图
简介:介绍查询公交线路功能
(1)点击“开始”按钮可查询公交线路;
(2)搜索出公交线路后点击“下一条”按钮可查询该线路的反方向公交线路;
本示例运行截图如下:
三、设计步骤
1、添加demo14_busLine.xml文件
在layout文件夹下添加该文件,然后将代码改为下面的内容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="在" /> <EditText
android:id="@+id/city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="北京" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="市内找" /> <EditText
android:id="@+id/searchkey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="717" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="公交车" /> <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <Button
android:id="@+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:layout_weight="1"
android:background="@drawable/button_style"
android:text="开始" /> <Button
android:id="@+id/nextline"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:layout_weight="1"
android:background="@drawable/button_style"
android:text="下一条" />
</LinearLayout>
</LinearLayout> <RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <fragment
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.baidu.mapapi.map.TextureMapFragment"
android:clickable="true" /> <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignWithParentIfMissing="false"
android:layout_centerHorizontal="true"
android:layout_centerVertical="false"
android:layout_marginBottom="10dip" > <Button
android:id="@+id/pre"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:layout_weight="1.0"
android:background="@drawable/pre_" /> <Button
android:id="@+id/next"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:layout_weight="1.0"
android:background="@drawable/next_" />
</LinearLayout>
</RelativeLayout> </LinearLayout>
2、添加Demo14BusLine.cs文件
在SrcSdkDemos文件夹下添加该文件,然后将代码改为下面的内容:
using Android.App;
using Android.Content.PM;
using Android.OS;
using Android.Views;
using Android.Widget;
using Android.Support.V4.App;
using Com.Baidu.Mapapi.Map;
using Com.Baidu.Mapapi.Model;
using Com.Baidu.Mapapi.Search.Busline;
using Com.Baidu.Mapapi.Search.Core;
using Com.Baidu.Mapapi.Search.Poi;
using System.Collections.Generic;
using BdMapV371Demos.SrcOverlayUtil; namespace BdMapV371Demos.SrcSdkDemos
{
/// <summary>
/// 此demo用来展示如何进行公交线路详情检索,并使用RouteOverlay在地图上绘制 同时展示如何浏览路线节点并弹出泡泡
/// </summary>
[Activity(Label = "@string/demo_name_bus",
ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.KeyboardHidden,
ScreenOrientation = ScreenOrientation.Sensor)]
public class Demo14BusLineSearch : FragmentActivity,
IOnGetPoiSearchResultListener,
IOnGetBusLineSearchResultListener,
BaiduMap.IOnMapClickListener
{
private Button mBtnPre = null;// 上一个节点
private Button mBtnNext = null;// 下一个节点
private int nodeIndex = -;// 节点索引,供浏览节点时使用
private BusLineResult route = null;// 保存驾车/步行路线数据的变量,供浏览节点时使用
private IList<string> busLineIDList = null;
private int busLineIndex = ; // 搜索相关
private PoiSearch mSearch = null; // 搜索模块,也可去掉地图模块独立使用
private BusLineSearch mBusLineSearch = null; private BaiduMap mBaiduMap = null;
BusLineOverlay overlay;//公交路线绘制对象 protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.demo14_busline); mBtnPre = FindViewById<Button>(Resource.Id.pre);
mBtnPre.Click += delegate { NodeClick(Resource.Id.pre); }; mBtnNext = FindViewById<Button>(Resource.Id.next);
mBtnPre.Click += delegate { NodeClick(Resource.Id.next); }; mBtnPre.Visibility = ViewStates.Invisible;
mBtnNext.Visibility = ViewStates.Invisible;
mBaiduMap = FragmentManager.FindFragmentById<TextureMapFragment>(Resource.Id.bmapView).BaiduMap;
mBaiduMap.SetOnMapClickListener(this); mSearch = PoiSearch.NewInstance();
mSearch.SetOnGetPoiSearchResultListener(this); mBusLineSearch = BusLineSearch.NewInstance();
mBusLineSearch.SetOnGetBusLineSearchResultListener(this); busLineIDList = new List<string>();
overlay = new BusLineOverlay(mBaiduMap);
mBaiduMap.SetOnMarkerClickListener(overlay); //【开始】按钮
var btnSearch = FindViewById<Button>(Resource.Id.search);
btnSearch.Click += delegate
{
busLineIDList.Clear();
busLineIndex = ;
mBtnPre.Visibility = ViewStates.Invisible;
mBtnNext.Visibility = ViewStates.Invisible;
EditText editCity = FindViewById<EditText>(Resource.Id.city);
EditText editSearchKey = FindViewById<EditText>(Resource.Id.searchkey);
//// 发起poi检索,从得到所有poi中找到公交线路类型的poi,再使用该poi的uid进行公交详情搜索
mSearch.SearchInCity(new PoiCitySearchOption()
.City(editCity.Text).Keyword(editSearchKey.Text));
}; //【下一条】按钮
var btnNextline = FindViewById<Button>(Resource.Id.nextline);
btnNextline.Click += delegate
{
SearchNextBusline();
};
} private void SearchNextBusline()
{
if (busLineIndex >= busLineIDList.Count)
{
busLineIndex = ;
}
if (busLineIndex >= && busLineIndex < busLineIDList.Count
&& busLineIDList.Count > )
{
mBusLineSearch.SearchBusLine(new BusLineSearchOption()
.City(FindViewById<EditText>(Resource.Id.city).Text)
.Uid(busLineIDList[busLineIndex]));
busLineIndex++;
}
} /// <summary>
/// 节点浏览示例
/// </summary>
/// <param name="id">按钮的id</param>
public void NodeClick(int id)
{
if (nodeIndex < - || route == null
|| nodeIndex >= route.Stations.Count)
return; TextView popupText = new TextView(this);
popupText.SetBackgroundResource(Resource.Drawable.popup);
popupText.SetTextColor(Android.Graphics.Color.Black); // 上一个节点
if (id == Resource.Id.pre && nodeIndex > )
{
// 索引减
nodeIndex--;
}
// 下一个节点
if (id == Resource.Id.next && nodeIndex < (route.Stations.Count - ))
{
// 索引加
nodeIndex++;
}
if (nodeIndex >= )
{
// 移动到指定索引的坐标
mBaiduMap.SetMapStatus(MapStatusUpdateFactory.NewLatLng(
route.Stations[nodeIndex].Location));
// 弹出泡泡
popupText.Text = route.Stations[nodeIndex].Title;
mBaiduMap.ShowInfoWindow(new InfoWindow(popupText,
route.Stations[nodeIndex].Location, ));
}
} protected override void OnPause()
{
base.OnPause();
} protected override void OnResume()
{
base.OnResume();
} protected override void OnDestroy()
{
mSearch.Destroy();
mBusLineSearch.Destroy();
base.OnDestroy();
} public void OnGetBusLineResult(BusLineResult result)
{
if (result == null || result.Error != SearchResult.ERRORNO.NoError)
{
Toast.MakeText(this, "抱歉,未找到结果", ToastLength.Long).Show();
return;
}
mBaiduMap.Clear();
route = result;
nodeIndex = -;
overlay.RemoveFromMap();
overlay.SetData(result);
overlay.AddToMap();
overlay.ZoomToSpan();
mBtnPre.Visibility = ViewStates.Visible;
mBtnNext.Visibility = ViewStates.Visible;
Toast.MakeText(this, result.BusLineName, ToastLength.Short).Show();
} public void OnGetPoiResult(PoiResult result)
{
if (result == null || result.Error != SearchResult.ERRORNO.NoError)
{
Toast.MakeText(this, "抱歉,未找到结果", ToastLength.Long).Show();
return;
}
// 遍历所有poi,找到类型为公交线路的poi
busLineIDList.Clear();
foreach (PoiInfo poi in result.AllPoi)
{
if (poi.Type == PoiInfo.POITYPE.BusLine
|| poi.Type == PoiInfo.POITYPE.SubwayLine)
{
busLineIDList.Add(poi.Uid);
}
}
SearchNextBusline();
route = null;
} public void OnGetPoiDetailResult(PoiDetailResult result)
{ } public void OnMapClick(LatLng point)
{
mBaiduMap.HideInfoWindow();
} public bool OnMapPoiClick(MapPoi poi)
{
return false;
}
}
}
3、修改MainActivity.cs
在MainActivity.cs文件的demos字段定义中,去掉【示例14】下面的注释。
运行观察结果。
【Android】3.14 公交线路查询功能的更多相关文章
- AMap公交线路查询
<!doctype html> <html> <head> <meta charset="utf-8"> <meta http ...
- 百度地图公交线路查询,并绘制到地图上并获取所有路径经纬度点(可供echarts 路径图使用)
github地址 https://github.com/a1115040996/MyHTML/tree/gh-pages/BDMap 源代码 <!DOCTYPE html PUBLIC &quo ...
- 【百度地图API】小学生找哥哥——小学生没钱打车,所以此为公交查询功能
原文:[百度地图API]小学生找哥哥--小学生没钱打车,所以此为公交查询功能 任务描述: 有位在魏公村附近上小学的小朋友,要去北京邮电大学找哥哥.他身上钱很少,只够坐公交的.所以,百度地图API快帮帮 ...
- Android百度地图开发(五)公交线路详情搜索、多条线路显示
一.公交线路详情检索 获取公交线路的详情主要分来两步,1.获取公交线路的Uid,2.通过Uid获取公交线路详情. 1.获取公交线路的Uid: /* * 获得公交线路图的Uid,并且根据系Uid发起公交 ...
- 【Android】3.13 路径规划功能
分类:C#.Android.VS2015.百度地图应用: 创建日期:2016-02-04 一.简介 线路规划支持以下功能: 公交信息查询:可对公交详细信息进行查询: 公交换乘查询:根据起.终点,查询策 ...
- 公交线路免费api接口代码
描写叙述:本接口主要是依据城市名称 + 线路名称 模糊查找城市公交线路信息. 开源api接口:http://openapi.aibang.com/bus/lines?app_key=keyvalue ...
- ArcGIS for Android示例解析之空间查询-----QueryTask
转自:http://blog.csdn.net/wozaifeiyang0/article/details/7331450 QueryTask 查询功能在GIS中是一个不可或缺的重要功能,示例中提供了 ...
- 济南最新公交线路一览(BRT)
济南最新公交线路一览(BRT) 济南BRT1路 高速公交线路 黄岗路东5:30-21:30 全福立交桥西5:30-21:30 票价1元,刷卡9折,月票有效 高速公交公司 去程:黄岗路东(BRT) - ...
- 【百度地图API】如何制作公交线路的搜索?如331路
原文:[百度地图API]如何制作公交线路的搜索?如331路 摘要: 从A点到B点的公交导航大家都知道怎么做了,那么单独查询331路公交车的公交路线,如何制作呢?我们一起来学习一下~ --------- ...
随机推荐
- Asp.Net 之 <%%>相关内联代码块用法
1.<%@ ... %> 用来添加命名空间引用,如:<%@ import namespace="system.data"> 2.<% ... %> ...
- 推荐10 款 SVG 动画的 JavaScript 库
SVG 通常可以用作跨分辨率视频.这意味着在一块高分屏幕上不会降低图片的锐度.此外,你甚至可以让SVG动起来,通过使用一些javascript类库.下面,我们分享一些javascript类库,这些类库 ...
- Linux 添加硬盘设备
fdisk命令用于管理磁盘分区,格式为:“fdisk [磁盘名称]”. 管理Linux系统中的硬盘设备最常用的方法就当属是用fdisk命令了,这条命令提供了添加.删除.转换分区等等功能于一身的“一站式 ...
- 用 Qt Creator 开发非 Qt 的 C/C++ 程序
在Windows还是习惯用VS2005但是现在到了Linux下,开发起来C/C++程序就没有那么得心应手的IDE了.虽然很多人推荐E开头那个主要作为Java开发的IDE,不过安上插件后感觉不大好,一个 ...
- HTML5 CANVAS 弹幕插件
概述 修改了普通弹幕运动的算法,新增了部分功能 详细 代码下载:http://www.demodashi.com/demo/10595.html 修改了普通弹幕运动的算法,新增了部分功能,具体请参看附 ...
- 从一个Idea到产品需要经历哪些阶段?
从一个Idea到产品需要经历哪些阶段? Lkey 07月19日 16:520 现实工作中,不免遇到这样的情况.什么嘛?老板(领导)又有新想法了?又有其他Idea了?心里一阵骂娘xxxxxx.或者产品负 ...
- 【LeetCode】【Python题解】Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...
- ORACLE 11G 利用泠备份恢复standby库
利用泠备份恢复standby数据库 開始使用泠备份进行db恢复 2.1,停止掉standby库 SQL> shutdown immediate; Database closed. Databas ...
- 【laravel5.4】php artisan migrate报错:Specified key was too long; max key length is 767 bytes
1.原因:在进行 迁移文件生成时,程序并未给varchar类型字段设置 合适的长度,导致报错. 2.解决办法:找到database/ 目标迁移文件,修改其中类型为string的字段长度,建议不要超过2 ...
- function声明的深刻含义和jquery属性注入区别
在js中有两类对象 1.json对象,仅仅代表对象而已 2.function声明的对象 (1) 它定义了构造器 可以用new 对象 来初始化 数据对象 (2) 它指明对象是一个函数对象 通过后面加 ...