006 走上移動開發之安卓項目實戰(百度地圖:如何進行經緯度轉換)

來源:互聯網
上載者:User

還請大家注意在文章進行轉載的時候麻煩註明出處

《http://blog.csdn.net/shuaiyinoo》謝謝

今天是百度地圖的最後一個部落格的更新了,看了上面幾篇部落格不知道大家有沒有感受或者有沒有什麼體會,基本上百度地圖提供的查詢的方法基本上都是非同步呼叫的,通過非同步呼叫顯示在  MySearchListener implements MKSearchListener  實現了這個介面的各個方法當中


當然這次的經緯度查詢和轉換同樣是在這裡面有所體會,在進行經緯度轉換的時候,只需要調用百度地圖的下面兩個方法,就可以成功的知道如何進行經緯度的轉換了



總得來說百度地圖做起來比Google地圖的API做起來簡單,原因可能就是應為百度地圖的API是中文的,中國人開發的更加適合中國人的使用習慣等等,加油,成功是所於大家的,下面我就把這一幾篇部落格的核心代碼貼出來,供大家參考參考


然後附上這幾個部落格的總體的項目,請大家笑納......有很多不足的地方還請大家多多包含......

--->>>飛機直達原始碼下載地


package com.shuaiyin.baidu;import java.util.ArrayList;import java.util.List;import android.content.Context; import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Point;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.widget.Toast;import com.baidu.mapapi.BMapManager;import com.baidu.mapapi.GeoPoint;import com.baidu.mapapi.ItemizedOverlay;import com.baidu.mapapi.MKAddrInfo;import com.baidu.mapapi.MKBusLineResult;import com.baidu.mapapi.MKDrivingRouteResult;import com.baidu.mapapi.MKGeneralListener;import com.baidu.mapapi.MKPlanNode;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.MapController;import com.baidu.mapapi.MapView;import com.baidu.mapapi.Overlay;import com.baidu.mapapi.OverlayItem;import com.baidu.mapapi.RouteOverlay;import com.baidu.mapapi.TransitOverlay;/** * 讓百度地圖繼承MapActivity * @author shuaiyin * */public class BaiDu_SuYiActivity extends MapActivity {//添加百度地圖的相關控制項private MapView mapView;//載入百度地圖的引起private BMapManager bMapManager;//定義百度地圖的KEYprivate String key = "3DA88729DD94B0429A4BEE30797E04D91B0211C4";//在百度地圖上添加相應的控制項private MapController mapController;//定義搜尋對象private MKSearch mkSearch ;//定義一個起點private MKPlanNode start;//定義一個終點private MKPlanNode end;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                //首先執行個體化mapView        mapView = (MapView) this.findViewById(R.id.bmapView);        bMapManager = new BMapManager(BaiDu_SuYiActivity.this);        //調用百度地圖載入KEY        bMapManager.init(key, new MKGeneralListener() {@Overridepublic void onGetPermissionState(int arg0) {if(arg0 == 300){Toast.makeText(BaiDu_SuYiActivity.this, "您輸入的KEY有問題,請核實", 2).show();}}@Overridepublic void onGetNetworkState(int arg0) {}});                this.initMapActivity(bMapManager);        //表示可以設定縮放功能        mapView.setBuiltInZoomControls(true);        mapController = mapView.getController();                //V1.00        //在百度地圖上標註一個中心點        //GeoPoint geoPoint = new GeoPoint((int)(39.915*1E6),(int)(116.404*1E6));        //給地圖對象設定一個中心點        //mapController.setCenter(geoPoint);        //設定地圖的縮放層級        //mapController.setZoom(12);        //顯示交通地圖        //mapView.setTraffic(true);        //顯示衛星地圖        //mapView.setSatellite(true);                //v2.00        //給地圖對象設定標註物        //mapView.getOverlays().add(new MyOverlay());        //mapController.setZoom(12);                //v3.00        //給地圖設定多個標註物        //顯示標註的表徵圖        //Drawable drawable = getResources().getDrawable(R.drawable.iconmarka);        //mapView.getOverlays().add(new MyOverLayItem(drawable));                //v4.00        //mapController.setZoom(14);        //在地圖範圍內實現對物體的搜尋        //mkSearch = new MKSearch();        //mkSearch.init(bMapManager, new MySearchListener());        //這個方法表示根據中心點搜尋周邊的內容        //mkSearch.poiSearchNearBy("廁所", new GeoPoint((int)(39.915*1E6),(int)(116.404*1E6)), 5000);                //v5.00駕車路線的搜尋    //mapController.setZoom(14);    //在地圖範圍內實現對物體的搜尋    //mkSearch = new MKSearch();    //mkSearch.init(bMapManager, new MySearchListener());    //給定第一個座標    //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.setDrivingPolicy(MKSearch.EBUS_TIME_FIRST);    //調用路線的回調方法    //mkSearch.drivingSearch(null, start, null, end);                //6.0行走路線的搜尋        //mapController.setZoom(12);        //在地圖範圍內實現對物體的搜尋    //mkSearch = new MKSearch();    //mkSearch.init(bMapManager, new MySearchListener());    //給定第一個座標    //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.walkingSearch(null, start, null, end);                //7.0公交轉乘路線的搜尋        //mapController.setZoom(12);        //在地圖範圍內實現對物體的搜尋    //mkSearch = new MKSearch();    //mkSearch.init(bMapManager, new MySearchListener());    //給定第一個座標    //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);             //8.0公交詳細路線的搜尋        //mapController.setZoom(12);        //在地圖範圍內實現對物體的搜尋    //mkSearch = new MKSearch();    //mkSearch.init(bMapManager, new MySearchListener());    //設定回調的行走路線的方法    //mkSearch.poiSearchInCity("北京", "300");                //v9.00駕車路線多條搜尋    mapController.setZoom(14);    //在地圖範圍內實現對物體的搜尋    mkSearch = new MKSearch();    mkSearch.init(bMapManager, new MySearchListener());    //給定第一個座標    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);    }        //搜尋對象的實現    public class MySearchListener implements MKSearchListener{        //根據名稱擷取經緯度@Overridepublic void onGetAddrResult(MKAddrInfo res, int error) {if(error != 0){Toast.makeText(BaiDu_SuYiActivity.this, "對不起,找不到您的結果", 2).show();return;}mapView.getController().animateTo(res.geoPt);String msg = String.valueOf(res.geoPt.getLatitudeE6()/1E6)+String.valueOf(res.geoPt.getLongitudeE6()/1E6);Drawable drawable = getResources().getDrawable(R.drawable.iconmarka);mapView.getOverlays().clear();}@Overridepublic void onGetBusDetailResult(MKBusLineResult result, int type) {if(result == null||type != 0){Toast.makeText(BaiDu_SuYiActivity.this, "對不起,找不到您的結果", 2).show();return;}RouteOverlay routeOverlay = new RouteOverlay(BaiDu_SuYiActivity.this, mapView);routeOverlay.setData(result.getBusRoute());//清空所有的公交資訊mapView.getOverlays().clear();mapView.getOverlays().add(routeOverlay);mapView.invalidate();mapView.getController().animateTo(result.getBusRoute().getStart());}@Overridepublic void onGetDrivingRouteResult(MKDrivingRouteResult result, int type) {//對調用的搜尋方法進行回調響應if(result == null){return;}RouteOverlay routeOverlay = new RouteOverlay(BaiDu_SuYiActivity.this, mapView);//裡面的參賽之所以填0是應為我們只需要返回第一條線路routeOverlay.setData(result.getPlan(0).getRoute(0));mapView.getOverlays().add(routeOverlay);mapView.invalidate();}@Overridepublic void onGetPoiDetailSearchResult(int result, int type) {}@Overridepublic void onGetPoiResult(MKPoiResult result, int type, int iError) {//對調用的搜尋方法進行回調響應//v3.00//if(result == null){//return;//}//PoiOverlay poiOverlay = new PoiOverlay(BaiDu_SuYiActivity.this, mapView);//poiOverlay.setData(result.getAllPoi());//mapView.getOverlays().add(poiOverlay);//mapView.invalidate();//v8.00if(result == null||iError != 0){Toast.makeText(BaiDu_SuYiActivity.this, "對不起,找不到您的結果", 2).show();return;}//定義一個公交線路圖描述類MKPoiInfo mkPoiInfo = null;//獲得公交線路圖的總的網站int totalPoiNum = result.getNumPois();for(int i = 0;i<totalPoiNum;i++){mkPoiInfo = result.getPoi(i);if(mkPoiInfo.ePoiType == 2){break;//如果說返回的時候公交的線路圖就返回}}mkSearch.busLineSearch("北京", mkPoiInfo.uid);}@Overridepublic void onGetRGCShareUrlResult(String arg0, int arg1) {}@Overridepublic void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {}@Overridepublic void onGetTransitRouteResult(MKTransitRouteResult result, int type) {//公交轉乘的回調方法//v7.00//if(result == null){//return;//}//TransitOverlay transitOverlay = new TransitOverlay(BaiDu_SuYiActivity.this, mapView);//transitOverlay.setData(result.getPlan(0));//mapView.getOverlays().add(transitOverlay);//重新整理地圖//mapView.invalidate();//v9.00if(result == null){return;}//獲得所有的駕車線路圖int planNum = result.getNumPlan();for(int i = 0 ;i<planNum;i++){TransitOverlay transitOverlay = new TransitOverlay(BaiDu_SuYiActivity.this, mapView);transitOverlay.setData(result.getPlan(i));mapView.getOverlays().add(transitOverlay);}mapView.invalidate();mapView.getController().animateTo(result.getStart().pt);}@Overridepublic void onGetWalkingRouteResult(MKWalkingRouteResult result, int type) {//對調用的搜尋方法進行回調響應if(result == null){return;}RouteOverlay routeOverlay = new RouteOverlay(BaiDu_SuYiActivity.this, mapView);routeOverlay.setData(result.getPlan(0).getRoute(0));mapView.getOverlays().add(routeOverlay);//重新整理地圖mapView.invalidate();}    }        //標註一個遮蓋物    public class MyOverlay extends Overlay{    //聲明一個地點    private GeoPoint geoPoint = new GeoPoint((int)(39.915*1E6),(int)(116.404*1E6));    //聲明一個畫筆工具    private Paint paint = new Paint();        @Override    public void draw(Canvas arg0, MapView arg1, boolean arg2) {    super.draw(arg0, arg1, arg2);        Point point = mapView.getProjection().toPixels(geoPoint, null);    arg0.drawText("這裡是天安門", point.x,point.y,paint);    }    }        //標註多個遮蓋物    public class MyOverLayItem extends ItemizedOverlay<OverlayItem>{        private List<OverlayItem> overlayItem = new ArrayList<OverlayItem>();        //定義一組座標    private double mLat1 = 39.90923;//經    private double mLot1 = 116.397428;//緯        private double mLat2 = 39.92923;//經    private double mLot2 = 116.377428;//緯        private double mLat3 = 39.94923;//經    private double mLot3 = 116.357428;//緯        private double mLat4 = 39.96923;//經    private double mLot4 = 116.337428;//緯        //用於在地圖上標識座標,用一個圖片標註public MyOverLayItem(Drawable drawable) {super(drawable);GeoPoint geoPoint1 = new GeoPoint((int)(mLat1*1E6),(int)(mLot1*1E6));GeoPoint geoPoint2 = new GeoPoint((int)(mLat2*1E6),(int)(mLot2*1E6));GeoPoint geoPoint3 = new GeoPoint((int)(mLat3*1E6),(int)(mLot3*1E6));GeoPoint geoPoint4 = new GeoPoint((int)(mLat4*1E6),(int)(mLot4*1E6));overlayItem.add(new OverlayItem(geoPoint1, "A", "這是第一個"));overlayItem.add(new OverlayItem(geoPoint2, "B", "這是第二個"));overlayItem.add(new OverlayItem(geoPoint3, "C", "這是第三個"));overlayItem.add(new OverlayItem(geoPoint4, "D", "這是第四個"));//重新整理地圖populate();}//返回指定的List集合中每一個座標@Overrideprotected OverlayItem createItem(int arg0) {return overlayItem.get(arg0);}@Overridepublic int size() {return overlayItem.size();}    //當標註物被點擊的時候@Overridepublic boolean onTap(int i) {Toast.makeText(BaiDu_SuYiActivity.this, overlayItem.get(i).getSnippet(), 2).show();return true;}    }                @Override    protected void onDestroy() {    super.onDestroy();    if(bMapManager != null){    bMapManager.destroy();    bMapManager = null;    }    }    @Override    protected void onResume() {    super.onResume();    if(bMapManager != null){    bMapManager.start();    }    }    @Override    protected void onPause() {    super.onPause();    if(bMapManager != null){    bMapManager.stop();    }    }    @Overrideprotected boolean isRouteDisplayed() {return false;}}


相關關鍵詞:
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.