android 实践项目四

本周主要是开发android baidumap实现公交的查询

1、权限的取得和对屏幕的支持

     <uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" >
</uses-permission>
<!-- <uses-permission android:name="android.permission.WRITE_APN_SETTINGS"></uses-permission> -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
</uses-permission>
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="android.permission.WAKE_LOCK" >
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<!-- SDK1.5需要android.permission.GET_TASKS权限判断本程序是否为当前运行的应用? -->
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.CAMERA" >
</uses-permission>
<uses-permission android:name="android.permission.RECORD_AUDIO" >
</uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<!-- 来电消音 -->
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> <supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="false"
android:resizeable="true"
android:smallScreens="true" />

2、main.java实现

 package baidumapsdk.demo;

 import java.util.ArrayList;
import java.util.List; import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.SupportMapFragment;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.overlayutil.BusLineOverlay;
import com.baidu.mapapi.search.busline.BusLineResult;
import com.baidu.mapapi.search.busline.BusLineSearch;
import com.baidu.mapapi.search.busline.BusLineSearchOption;
import com.baidu.mapapi.search.busline.OnGetBusLineSearchResultListener;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch; /**
* 此demo用来展示如何进行公交线路详情检索,并使用RouteOverlay在地图上绘制 同时展示如何浏览路线节点并弹出泡泡
*/
public class BusLineSearchDemo extends FragmentActivity implements
OnGetPoiSearchResultListener, OnGetBusLineSearchResultListener,
BaiduMap.OnMapClickListener {
private Button mBtnPre = null;// 上一个节点
private Button mBtnNext = null;// 下一个节点
private int nodeIndex = -;// 节点索引,供浏览节点时使用
private BusLineResult route = null;// 保存驾车/步行路线数据的变量,供浏览节点时使用
private List<String> busLineIDList = null;
private int busLineIndex = ;
// 搜索相关
private PoiSearch mSearch = null; // 搜索模块,也可去掉地图模块独立使用
private BusLineSearch mBusLineSearch = null;//城市公交查询
private BaiduMap mBaiduMap = null; protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_busline);
CharSequence titleLable = "公交线路查询功能";//在图层中显示名称
setTitle(titleLable);
mBtnPre = (Button) findViewById(R.id.pre);
mBtnNext = (Button) findViewById(R.id.next);
mBtnPre.setVisibility(View.INVISIBLE);
mBtnNext.setVisibility(View.INVISIBLE);
mBaiduMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.bmapView)).getBaiduMap();
mBaiduMap.setOnMapClickListener(this);//地图单击事件监听接口
mSearch = PoiSearch.newInstance();//POI检索接口
mSearch.setOnGetPoiSearchResultListener(this);//设置poi检索监听者
mBusLineSearch = BusLineSearch.newInstance();
mBusLineSearch.setOnGetBusLineSearchResultListener(this);//设置公交详情检索结果监听者
busLineIDList = new ArrayList<String>(); } /**
* 发起检索
*
* @param v
*/
public void searchButtonProcess(View v) {
busLineIDList.clear();
busLineIndex = ;
mBtnPre.setVisibility(View.INVISIBLE);
mBtnNext.setVisibility(View.INVISIBLE);
EditText editCity = (EditText) findViewById(R.id.city);
EditText editSearchKey = (EditText) findViewById(R.id.searchkey);
// 发起poi检索,从得到所有poi中找到公交线路类型的poi,再使用该poi的uid进行公交详情搜索
mSearch.searchInCity((new PoiCitySearchOption()).city(
editCity.getText().toString()).keyword(
editSearchKey.getText().toString()));
} public void SearchNextBusline(View v) {
if (busLineIndex >= busLineIDList.size()) {
busLineIndex = ;
}//在编辑中输入文本得然后获得公交路线
if (busLineIndex >= && busLineIndex < busLineIDList.size()
&& busLineIDList.size() > ) {
mBusLineSearch.searchBusLine((new BusLineSearchOption()
.city(((EditText) findViewById(R.id.city)).getText()
.toString()).uid(busLineIDList.get(busLineIndex)))); busLineIndex++;
} } /**
* 节点浏览示例
*
* @param v
*/
public void nodeClick(View v) { if (nodeIndex < - || route == null
|| nodeIndex >= route.getStations().size())
return;
TextView popupText = new TextView(this);
popupText.setBackgroundResource(R.drawable.popup);//设置背景源
popupText.setTextColor(0xff000000);//设置文本颜色
// 上一个节点
if (mBtnPre.equals(v) && nodeIndex > ) {
// 索引减
nodeIndex--;
}
// 下一个节点
if (mBtnNext.equals(v) && nodeIndex < (route.getStations().size() - )) {
// 索引加
nodeIndex++;
}
if(nodeIndex >= ){
// 移动到指定索引的坐标
mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(route
.getStations().get(nodeIndex).getLocation()));
// 弹出泡泡
popupText.setText(route.getStations().get(nodeIndex).getTitle());
mBaiduMap.showInfoWindow(new InfoWindow(popupText, route.getStations()
.get(nodeIndex).getLocation(), ));
}
} @Override
protected void onPause() {
super.onPause();
} @Override
protected void onResume() {
super.onResume();
} @Override
protected void onDestroy() {
mSearch.destroy();
mBusLineSearch.destroy();
super.onDestroy();
} @Override
public void onGetBusLineResult(BusLineResult result) {
if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(BusLineSearchDemo.this, "抱歉,未找到结果",
Toast.LENGTH_LONG).show();
return;
}
mBaiduMap.clear();
route = result;
nodeIndex = -;
BusLineOverlay overlay = new BusLineOverlay(mBaiduMap);
mBaiduMap.setOnMarkerClickListener(overlay);
overlay.setData(result);
overlay.addToMap();
overlay.zoomToSpan();
mBtnPre.setVisibility(View.VISIBLE);
mBtnNext.setVisibility(View.VISIBLE);
Toast.makeText(BusLineSearchDemo.this, result.getBusLineName(),
Toast.LENGTH_SHORT).show();
} @Override
public void onGetPoiResult(PoiResult result) { if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(BusLineSearchDemo.this, "抱歉,未找到结果",
Toast.LENGTH_LONG).show();
return;
}
// 遍历所有poi,找到类型为公交线路的poi
busLineIDList.clear();
for (PoiInfo poi : result.getAllPoi()) {
if (poi.type == PoiInfo.POITYPE.BUS_LINE
|| poi.type == PoiInfo.POITYPE.SUBWAY_LINE) {
busLineIDList.add(poi.uid);
}
}
SearchNextBusline(null);
route = null;
} @Override
public void onGetPoiDetailResult(PoiDetailResult result) { } @Override
public void onMapClick(LatLng point) {
mBaiduMap.hideInfoWindow();
} @Override
public boolean onMapPoiClick(MapPoi poi) {
return false;
}
}

3、布局的实现.xml

 <?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="" /> <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=""
android:background="@drawable/button_style"
android:onClick="searchButtonProcess"
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=""
android:background="@drawable/button_style"
android:onClick="SearchNextBusline"
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.SupportMapFragment"
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_"
android:onClick="nodeClick" /> <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_"
android:onClick="nodeClick" />
</LinearLayout>
</RelativeLayout> </LinearLayout>

4、默认的是北京83路公交
android 实践项目四-LMLPHP

05-08 14:55