android 实践项目四
本周主要是开发android baidumap实现公交的查询
1、权限的取得和对屏幕的支持
1 <uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE" > 2 </uses-permission> 3 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" > 4 </uses-permission> 5 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" > 6 </uses-permission> 7 <uses-permission android:name="android.permission.INTERNET" > 8 </uses-permission> 9 <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" > 10 </uses-permission> 11 <!-- <uses-permission android:name="android.permission.WRITE_APN_SETTINGS"></uses-permission> --> 12 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" > 13 </uses-permission> 14 <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" /> 15 <uses-permission android:name="android.permission.WAKE_LOCK" > 16 </uses-permission> 17 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> 18 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 19 <uses-permission android:name="android.permission.ACCESS_GPS" /> 20 <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 21 <uses-permission android:name="android.permission.READ_CONTACTS" /> 22 <uses-permission android:name="android.permission.CALL_PHONE" /> 23 <uses-permission android:name="android.permission.READ_SMS" /> 24 <uses-permission android:name="android.permission.SEND_SMS" /> 25 <!-- SDK1.5需要android.permission.GET_TASKS权限判断本程序是否为当前运行的应用? --> 26 <uses-permission android:name="android.permission.GET_TASKS" /> 27 <uses-permission android:name="android.permission.CAMERA" > 28 </uses-permission> 29 <uses-permission android:name="android.permission.RECORD_AUDIO" > 30 </uses-permission> 31 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" > 32 </uses-permission> 33 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 34 <uses-permission android:name="android.permission.BROADCAST_STICKY" /> 35 <uses-permission android:name="android.permission.WRITE_SETTINGS" /> 36 <!-- 来电消音 --> 37 <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" > 38 </uses-permission> 39 <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 40 <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> 41 42 <supports-screens 43 android:anyDensity="true" 44 android:largeScreens="true" 45 android:normalScreens="false" 46 android:resizeable="true" 47 android:smallScreens="true" />
2、main.java实现
1 package baidumapsdk.demo; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import android.os.Bundle; 7 import android.support.v4.app.FragmentActivity; 8 import android.view.View; 9 import android.widget.Button; 10 import android.widget.EditText; 11 import android.widget.TextView; 12 import android.widget.Toast; 13 14 import com.baidu.mapapi.map.BaiduMap; 15 import com.baidu.mapapi.map.InfoWindow; 16 import com.baidu.mapapi.map.MapPoi; 17 import com.baidu.mapapi.map.MapStatusUpdateFactory; 18 import com.baidu.mapapi.map.SupportMapFragment; 19 import com.baidu.mapapi.model.LatLng; 20 import com.baidu.mapapi.overlayutil.BusLineOverlay; 21 import com.baidu.mapapi.search.busline.BusLineResult; 22 import com.baidu.mapapi.search.busline.BusLineSearch; 23 import com.baidu.mapapi.search.busline.BusLineSearchOption; 24 import com.baidu.mapapi.search.busline.OnGetBusLineSearchResultListener; 25 import com.baidu.mapapi.search.core.PoiInfo; 26 import com.baidu.mapapi.search.core.SearchResult; 27 import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener; 28 import com.baidu.mapapi.search.poi.PoiCitySearchOption; 29 import com.baidu.mapapi.search.poi.PoiDetailResult; 30 import com.baidu.mapapi.search.poi.PoiResult; 31 import com.baidu.mapapi.search.poi.PoiSearch; 32 33 /** 34 * 此demo用来展示如何进行公交线路详情检索,并使用RouteOverlay在地图上绘制 同时展示如何浏览路线节点并弹出泡泡 35 */ 36 public class BusLineSearchDemo extends FragmentActivity implements 37 OnGetPoiSearchResultListener, OnGetBusLineSearchResultListener, 38 BaiduMap.OnMapClickListener { 39 private Button mBtnPre = null;// 上一个节点 40 private Button mBtnNext = null;// 下一个节点 41 private int nodeIndex = -2;// 节点索引,供浏览节点时使用 42 private BusLineResult route = null;// 保存驾车/步行路线数据的变量,供浏览节点时使用 43 private List<String> busLineIDList = null; 44 private int busLineIndex = 0; 45 // 搜索相关 46 private PoiSearch mSearch = null; // 搜索模块,也可去掉地图模块独立使用 47 private BusLineSearch mBusLineSearch = null;//城市公交查询 48 private BaiduMap mBaiduMap = null; 49 50 protected void onCreate(Bundle savedInstanceState) { 51 super.onCreate(savedInstanceState); 52 setContentView(R.layout.activity_busline); 53 CharSequence titleLable = "公交线路查询功能";//在图层中显示名称 54 setTitle(titleLable); 55 mBtnPre = (Button) findViewById(R.id.pre); 56 mBtnNext = (Button) findViewById(R.id.next); 57 mBtnPre.setVisibility(View.INVISIBLE); 58 mBtnNext.setVisibility(View.INVISIBLE); 59 mBaiduMap = ((SupportMapFragment) getSupportFragmentManager() 60 .findFragmentById(R.id.bmapView)).getBaiduMap(); 61 mBaiduMap.setOnMapClickListener(this);//地图单击事件监听接口 62 mSearch = PoiSearch.newInstance();//POI检索接口 63 mSearch.setOnGetPoiSearchResultListener(this);//设置poi检索监听者 64 mBusLineSearch = BusLineSearch.newInstance(); 65 mBusLineSearch.setOnGetBusLineSearchResultListener(this);//设置公交详情检索结果监听者 66 busLineIDList = new ArrayList<String>(); 67 68 } 69 70 /** 71 * 发起检索 72 * 73 * @param v 74 */ 75 public void searchButtonProcess(View v) { 76 busLineIDList.clear(); 77 busLineIndex = 0; 78 mBtnPre.setVisibility(View.INVISIBLE); 79 mBtnNext.setVisibility(View.INVISIBLE); 80 EditText editCity = (EditText) findViewById(R.id.city); 81 EditText editSearchKey = (EditText) findViewById(R.id.searchkey); 82 // 发起poi检索,从得到所有poi中找到公交线路类型的poi,再使用该poi的uid进行公交详情搜索 83 mSearch.searchInCity((new PoiCitySearchOption()).city( 84 editCity.getText().toString()).keyword( 85 editSearchKey.getText().toString())); 86 } 87 88 public void SearchNextBusline(View v) { 89 if (busLineIndex >= busLineIDList.size()) { 90 busLineIndex = 0; 91 }//在编辑中输入文本得然后获得公交路线 92 if (busLineIndex >= 0 && busLineIndex < busLineIDList.size() 93 && busLineIDList.size() > 0) { 94 mBusLineSearch.searchBusLine((new BusLineSearchOption() 95 .city(((EditText) findViewById(R.id.city)).getText() 96 .toString()).uid(busLineIDList.get(busLineIndex)))); 97 98 busLineIndex++; 99 } 100 101 } 102 103 /** 104 * 节点浏览示例 105 * 106 * @param v 107 */ 108 public void nodeClick(View v) { 109 110 if (nodeIndex < -1 || route == null 111 || nodeIndex >= route.getStations().size()) 112 return; 113 TextView popupText = new TextView(this); 114 popupText.setBackgroundResource(R.drawable.popup);//设置背景源 115 popupText.setTextColor(0xff000000);//设置文本颜色 116 // 上一个节点 117 if (mBtnPre.equals(v) && nodeIndex > 0) { 118 // 索引减 119 nodeIndex--; 120 } 121 // 下一个节点 122 if (mBtnNext.equals(v) && nodeIndex < (route.getStations().size() - 1)) { 123 // 索引加 124 nodeIndex++; 125 } 126 if(nodeIndex >= 0){ 127 // 移动到指定索引的坐标 128 mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(route 129 .getStations().get(nodeIndex).getLocation())); 130 // 弹出泡泡 131 popupText.setText(route.getStations().get(nodeIndex).getTitle()); 132 mBaiduMap.showInfoWindow(new InfoWindow(popupText, route.getStations() 133 .get(nodeIndex).getLocation(), 0)); 134 } 135 } 136 137 @Override 138 protected void onPause() { 139 super.onPause(); 140 } 141 142 @Override 143 protected void onResume() { 144 super.onResume(); 145 } 146 147 @Override 148 protected void onDestroy() { 149 mSearch.destroy(); 150 mBusLineSearch.destroy(); 151 super.onDestroy(); 152 } 153 154 @Override 155 public void onGetBusLineResult(BusLineResult result) { 156 if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) { 157 Toast.makeText(BusLineSearchDemo.this, "抱歉,未找到结果", 158 Toast.LENGTH_LONG).show(); 159 return; 160 } 161 mBaiduMap.clear(); 162 route = result; 163 nodeIndex = -1; 164 BusLineOverlay overlay = new BusLineOverlay(mBaiduMap); 165 mBaiduMap.setOnMarkerClickListener(overlay); 166 overlay.setData(result); 167 overlay.addToMap(); 168 overlay.zoomToSpan(); 169 mBtnPre.setVisibility(View.VISIBLE); 170 mBtnNext.setVisibility(View.VISIBLE); 171 Toast.makeText(BusLineSearchDemo.this, result.getBusLineName(), 172 Toast.LENGTH_SHORT).show(); 173 } 174 175 @Override 176 public void onGetPoiResult(PoiResult result) { 177 178 if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) { 179 Toast.makeText(BusLineSearchDemo.this, "抱歉,未找到结果", 180 Toast.LENGTH_LONG).show(); 181 return; 182 } 183 // 遍历所有poi,找到类型为公交线路的poi 184 busLineIDList.clear(); 185 for (PoiInfo poi : result.getAllPoi()) { 186 if (poi.type == PoiInfo.POITYPE.BUS_LINE 187 || poi.type == PoiInfo.POITYPE.SUBWAY_LINE) { 188 busLineIDList.add(poi.uid); 189 } 190 } 191 SearchNextBusline(null); 192 route = null; 193 } 194 195 @Override 196 public void onGetPoiDetailResult(PoiDetailResult result) { 197 198 } 199 200 @Override 201 public void onMapClick(LatLng point) { 202 mBaiduMap.hideInfoWindow(); 203 } 204 205 @Override 206 public boolean onMapPoiClick(MapPoi poi) { 207 return false; 208 } 209 }
3、布局的实现.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <LinearLayout 8 xmlns:android="http://schemas.android.com/apk/res/android" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:orientation="horizontal" > 12 13 <TextView 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:text="在" /> 17 18 <EditText 19 android:id="@+id/city" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:text="北京" /> 23 24 <TextView 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:text="市内找" /> 28 29 <EditText 30 android:id="@+id/searchkey" 31 android:layout_width="wrap_content" 32 android:layout_height="wrap_content" 33 android:text="83" /> 34 35 <TextView 36 android:layout_width="wrap_content" 37 android:layout_height="wrap_content" 38 android:text="公交车" /> 39 40 <LinearLayout 41 xmlns:android="http://schemas.android.com/apk/res/android" 42 android:layout_width="fill_parent" 43 android:layout_height="wrap_content" 44 android:orientation="horizontal" > 45 46 <Button 47 android:id="@+id/search" 48 android:layout_width="fill_parent" 49 android:layout_height="wrap_content" 50 android:layout_marginLeft="2dip" 51 android:layout_marginRight="2dip" 52 android:layout_weight="1" 53 android:background="@drawable/button_style" 54 android:onClick="searchButtonProcess" 55 android:text="开始" /> 56 57 <Button 58 android:id="@+id/nextline" 59 android:layout_width="fill_parent" 60 android:layout_height="wrap_content" 61 android:layout_marginLeft="2dip" 62 android:layout_marginRight="2dip" 63 android:layout_weight="1" 64 android:background="@drawable/button_style" 65 android:onClick="SearchNextBusline" 66 android:text="下一条" /> 67 </LinearLayout> 68 </LinearLayout> 69 70 <RelativeLayout 71 xmlns:android="http://schemas.android.com/apk/res/android" 72 android:layout_width="match_parent" 73 android:layout_height="match_parent" > 74 75 <fragment 76 android:id="@+id/bmapView" 77 android:layout_width="fill_parent" 78 android:layout_height="fill_parent" 79 class="com.baidu.mapapi.map.SupportMapFragment" 80 android:clickable="true" /> 81 82 <LinearLayout 83 xmlns:android="http://schemas.android.com/apk/res/android" 84 android:layout_width="wrap_content" 85 android:layout_height="wrap_content" 86 android:layout_alignParentBottom="true" 87 android:layout_alignWithParentIfMissing="false" 88 android:layout_centerHorizontal="true" 89 android:layout_centerVertical="false" 90 android:layout_marginBottom="10dip" > 91 92 <Button 93 android:id="@+id/pre" 94 android:layout_width="fill_parent" 95 android:layout_height="fill_parent" 96 android:layout_marginLeft="2dip" 97 android:layout_marginRight="2dip" 98 android:layout_weight="1.0" 99 android:background="@drawable/pre_" 100 android:onClick="nodeClick" /> 101 102 <Button 103 android:id="@+id/next" 104 android:layout_width="fill_parent" 105 android:layout_height="fill_parent" 106 android:layout_marginLeft="2dip" 107 android:layout_marginRight="2dip" 108 android:layout_weight="1.0" 109 android:background="@drawable/next_" 110 android:onClick="nodeClick" /> 111 </LinearLayout> 112 </RelativeLayout> 113 114 </LinearLayout>
4、默认的是北京83路公交
时间: 2024-10-19 10:57:44