1. 程式人生 > >Android定位&地圖&導航——自定義公交路線程式碼

Android定位&地圖&導航——自定義公交路線程式碼

public class BuslineSearch extends MapActivity {
    Button mBtnSearch = null; // 搜尋按鈕
    MapView mMapView = null; // 地圖View
    MKSearch mSearch = null; // 搜尋模組,也可去掉地圖模組獨立使用
    String mCityName = null;
    LocationListener loc_listener;
    App app = null;
    static boolean flag = false;
    static
Thread thread; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.buslinesearch); app = (App) this.getApplication(); if (app.mBMapMan == null) { app.mBMapMan = new BMapManager(getApplication()); app.mBMapMan.init(app.mStrKey,
new App.MyGeneralListener()); } app.mBMapMan.start(); // 如果使用地圖SDK,請初始化地圖Activity super.initMapActivity(app.mBMapMan); mMapView = (MapView) findViewById(R.id.bmapView); mMapView.setBuiltInZoomControls(true); // 設定在縮放動畫過程中也顯示overlay,預設為不繪製 mMapView.setDrawOverlayWhenZooming(true
); mMapView.setBuiltInZoomControls(true); // 初始化搜尋模組,註冊事件監聽 MapController mMapController = mMapView.getController(); // 得到mMapView的控制權,可以用它控制和驅動平移和縮放 GeoPoint point = new GeoPoint((int) (39.915 * 1E6), (int) (116.404 * 1E6)); // 用給定的經緯度構造一個GeoPoint,單位是微度 (度 * 1E6) mMapController.setCenter(point); // 設定地圖中心點 mMapController.setZoom(15); // 設定地圖zoom級別 mSearch = new MKSearch(); mSearch.init(app.mBMapMan, new MKSearchListener() { public void onGetPoiResult(MKPoiResult res, int type, int error) { // 錯誤號可參考MKEvent中的定義 if (error != 0 || res == null) { Toast.makeText(BuslineSearch.this, "抱歉,未找到結果", Toast.LENGTH_LONG).show(); return; } // System.out.println(res.toString()); // 找到公交路線poi node MKPoiInfo curPoi = null; int totalPoiNum = res.getNumPois(); for (int idx = 0; idx < totalPoiNum; idx++) { Log.d("busline", "the busline is " + idx); curPoi = res.getPoi(idx); if (2 == curPoi.ePoiType) { break; } } mSearch.busLineSearch(mCityName, curPoi.uid); } public void onGetDrivingRouteResult(MKDrivingRouteResult res, int error) { } public void onGetTransitRouteResult(MKTransitRouteResult res, int error) { res.getPlan(0).getDistance(); } public void onGetWalkingRouteResult(MKWalkingRouteResult res, int error) { } public void onGetAddrResult(MKAddrInfo res, int error) { } public void onGetBusDetailResult(MKBusLineResult result, int iError) { if (iError != 0 || result == null) { Toast.makeText(BuslineSearch.this, "抱歉,未找到結果", Toast.LENGTH_LONG).show(); return; } // result.getBusRoute().get // result.getBusRoute().getStart().toString(); CustomRouteOverLay routeOverlay = new CustomRouteOverLay( BuslineSearch.this, mMapView); routeOverlay.setData(result.getBusRoute()); mMapView.getOverlays().clear(); System.out.println(mMapView.getOverlays().size()); mMapView.getOverlays().add(routeOverlay); mMapView.invalidate(); mMapView.getController().animateTo( result.getBusRoute().getStart()); } @Override public void onGetSuggestionResult(MKSuggestionResult res, int arg1) { // TODO Auto-generated method stub } }); // mLocationManager.requestLocationUpdates(listener); // 註冊定位事件 loc_listener = new LocationListener() { @Override public void onLocationChanged(Location location) { if (location != null) { String strLog = String.format("您當前的位置:\r\n" + "緯度:%f\r\n" + "經度:%f", location.getLongitude(), location.getLatitude()); flag = true; Drawable marker = getResources() .getDrawable(R.drawable.ic_launcher); final GeoPoint p = new GeoPoint( (int) (location.getLatitude() * 1E6), (int) (location.getLongitude() * 1E6)); CustomOverlayItem item = new CustomOverlayItem(marker, BuslineSearch.this, p, "我的位置", "", false); mMapView.getOverlays().add(item); mMapView.getController().animateTo(p); } } }; // 設定搜尋按鈕的響應 mBtnSearch = (Button) findViewById(R.id.search); OnClickListener clickListener = new OnClickListener() { public void onClick(View v) { SearchButtonProcess(v); } }; mBtnSearch.setOnClickListener(clickListener); } void SearchButtonProcess(View v) { if (mBtnSearch.equals(v)) { mMapView.getOverlays().clear(); mMapView.getOverlays().removeAll(mMapView.getOverlays()); mMapView.invalidate(); EditText editCity = (EditText) findViewById(R.id.city); EditText editSearchKey = (EditText) findViewById(R.id.searchkey); mCityName = editCity.getText().toString(); mSearch.poiSearchInCity(mCityName, editSearchKey.getText() .toString()); } } @Override protected void onPause() { if (null == app) app = (App) this.getApplication(); app.mBMapMan.getLocationManager().removeUpdates(loc_listener); app.mBMapMan.stop(); super.onPause(); } @Override protected void onResume() { if (null == app) app = (App) this.getApplication(); app.mBMapMan.start(); super.onResume(); app.mBMapMan.getLocationManager().requestLocationUpdates(loc_listener);// 定位 } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); } }

相關推薦

Android定位&地圖&導航——定義公交路線程式碼

public class BuslineSearch extends MapActivity { Button mBtnSearch = null; // 搜尋按鈕 MapView mMapView = null; // 地圖View MKSearch mSearch = n

Android定位&地圖&導航——基於百度地圖,實現定義圖示繪製並點選時彈出泡泡

public class MainActivity extends Activity { private EditText txtAddr; // 定位相關 LocationClient mLocClient; LocationData locData = nul

Android定位&地圖&導航——基於百度地圖移動獲取位置和自動定位

public class MainActivity extends Activity { public MyApplication app; static MapView mMapView = null; public MKMapViewListener mMapListen

Android定位&地圖&導航——基於百度地圖實現的定位功能

public class MyApplication extends Application{ public LocationClient mLocationClient = null; public GeofenceClient mGeofenceClient; publi

高德地圖Android版SDK的應用(定位,新增定義標記點)

需要先下載高德地圖的 Android SDK和 Android 定位SDK : http://code.autonavi.com/index package com.fproject.DXCBuy; import com.amap.api.location.AMap

ArcGIS for Android 100.3的學習與應用(三) 實現地圖新增定義指北針

圖為高德地圖實現指北針的效果,那麼ArcGIS如何實現呢? 實現方式: 新增地圖的旋轉監聽: map.addMapRotationChangedListener(new MapRotationChangedListener() { @Override

Android百度地圖開發-定義地圖的使用和坑

設定個性化地圖config檔案路徑 */ public static void setMapCustomFile(Context context, String PATH) { FileOutputStream out = null; InputStream inputStream = null; Strin

Android高德地圖定義底圖(午夜藍主題風格地圖

官網上介紹:地址連結 從 3D 地圖 SDK V4.1.3版本開始支援自定義地圖底圖功能。 功能說明:支援對部分地圖元素自定義顏色,包括:填充色、邊框色、文字顏色。 先上圖,我自己做出來的自定義地圖(底圖) 效果圖就是以上這樣,下面來說一下實現的步驟

百度地圖api 定義駕車線路規劃 車輛實時定位

var coordinateArr = [] //定義一個全聚德所有的繪製線路點座標陣列 var icona = []; //定義標註圖示陣列 var zhandianpoint = []; //定義站點座標陣列 var map = new BMap.Map("allmap"

Android整合高德地圖如何定義marker

高德地圖自定義Marker 高德地圖預設的marker樣式是這種 一般的修改樣式是通過icon介面來調整 MarkerOptions markerOptions = new MarkerOpt

Android 百度地圖新增定義marker(覆蓋物)不顯示圖片的解決方法

在使用百度地圖新增自定義Marker View時,顯示不出來自定義marker view裡面的頭像,百度地圖的新增自定義marker顯示網路圖片本身就是一個坑(我是這樣認為的)... 我的需求是這樣的,當有人員上報位置時,就根據經緯度把人員位置資訊用自定義Marker去載入

android:如何通過定義工程模板讓新建的工程都默認支持lambda表達式

wan tro idt ref height 代碼 spa span oid 首先參考這篇文章:自定義Android Studio工程模板,了解如何自定義模板然後結合我們上一篇文章 android: 在android studio中使用retrolambda的步驟的要點,修

Android編程入門--定義Application

如何 使用 shu ppc android系統 contex target logs @override Android系統自動會為每個程序運行時創建一個Application類的對象且只創建一個 參考博客:Android 當中 application的使用 參考文章:

Android 修改源碼定義SwipeRefreshLayout樣式——高仿微信朋友圈下拉刷新

樣式 post and 微信 修改 size roi 自定義 details 修改源碼自定義SwipeRefreshLayout樣式——高仿微信朋友圈下拉刷新Android 修改源碼自定義SwipeRefreshLayout樣式——高仿微信朋友圈下拉

Android中快速實現定義字體!

sdk true fcm version ttf spa pre ets 怎麽 前言:我們都知道,Android中默認的字體是黑體,而大多數app也都是使用的這種字體,但我們發現,大多數app中,個別地方字體非常好看,例如app的標題欄,菜單欄等地方,那他們是怎麽做到的呢?

百度地圖API 定義標註圖標

cit rop 設置 src rip ddc 使用 options city 通過Icon類可實現自定義標註的圖標,下面示例通過參數MarkerOptions的icon屬性進行設置, 也可以使用marker.setIcon()方法。 <script type="te

Android 7.0 獲取定義許可權

private String[] permissions = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, // Manifest.permission.ACCESS_COARSE_

百度地圖API 定義座標點及圖片

var map = new BMap.Map("allmap");var point = new BMap.Point(105.955754,36.525109);map.centerAndZoom(point,8);map.en

(三)高德地圖定義縮放及縮放動畫效果

這一節主要實現的功能是地圖的自定義縮放及縮放的動畫效果,還是直接放上程式碼更直觀些,主要部位裡面基本有註解 還是老樣子,首先是新建activity_zoom_animate.xml佈局檔案 <?xml version="1.0" encoding="utf-8"?> <

Android 使用Zxing生成定義二維碼

最近有小夥伴問我,如何在Android端生成自己想要的二維碼,我看了下,大致上二維碼分為兩種,一種是純二維碼,一種是中間帶圖片的二維碼。下面我們看一下這兩種是如何實現的。 執行截圖 初始頁 輸入你想生成的連結或文字 再試試帶圖片的二維碼 生成後可以直接掃