Android地圖開發之百度地圖一


  最近搞的項目中需要用到地圖,用來展示使用者附近如肯德基餐廳的位置。之前通過高德地圖實現了,今天又通過百度地圖來實現一下。廢話不多說!

  首先,從百度地圖下載Android平台下面的API開發包,示例代碼和http://dev.baidu.com/wiki/imap/index.php

  解壓示例代碼,導入eclipse運行,如下圖:

   

  但是沒有我們所需要的代碼,所以還是要自己動手來實現一個DEMO,創建一個Android工程,在manifest加入百度地圖所需權限:

1     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
2     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
3     <uses-permission android:name="android.permission.INTERNET"></uses-permission>
4     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
5     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>  
6     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission> 
7     <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

  在main.xml中加入:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical" 
 4     android:layout_width="fill_parent"
 5     android:layout_height="fill_parent">
 6     
 7     <TextView 
 8         android:layout_width="fill_parent" 
 9         android:layout_height="wrap_content" 
10         android:text="@string/hello" />
11     
12     <com.baidu.mapapi.MapView 
13         android:id="@+id/bmapsView" 
14         android:layout_width="fill_parent" 
15         android:layout_height="fill_parent" 
16         android:clickable="true" />
17 
18 </LinearLayout>

 

  根據百度的開發指南在MyBaiduMap的文件中添加如下代碼:

  1 package com.lee.mybaidumap;
  2 
  3 import com.baidu.mapapi.BMapManager;
  4 import com.baidu.mapapi.GeoPoint;
  5 import com.baidu.mapapi.LocationListener;
  6 import com.baidu.mapapi.MKAddrInfo;
  7 import com.baidu.mapapi.MKBusLineResult;
  8 import com.baidu.mapapi.MKDrivingRouteResult;
  9 import com.baidu.mapapi.MKPoiResult;
 10 import com.baidu.mapapi.MKSearch;
 11 import com.baidu.mapapi.MKSearchListener;
 12 import com.baidu.mapapi.MKTransitRouteResult;
 13 import com.baidu.mapapi.MKWalkingRouteResult;
 14 import com.baidu.mapapi.MapActivity;
 15 import com.baidu.mapapi.MapView;
 16 import com.baidu.mapapi.MyLocationOverlay;
 17 import com.baidu.mapapi.PoiOverlay;
 18 
 19 import android.location.Location;
 20 import android.os.Bundle;
 21 
 22 public class MyBaiduMap extends MapActivity {
 23     
 24     MapView mMapView = null;
 25     BMapManager mBMapMan = null;
 26     LocationListener mLocationListener = null;//onResume時注冊此listener,onPause時需要Remove
 27     MyLocationOverlay mLocationOverlay = null;    //定位圖層
 28     
 29     MKSearchListener mMKSearchListener = null;
 30     MKSearch mMKSearch = null;
 31             
 32     /** Called when the activity is first created. */
 33     @Override
 34     public void onCreate(Bundle savedInstanceState) {
 35         super.onCreate(savedInstanceState);
 36         setContentView(R.layout.main);
 37         
 38         mBMapMan = new BMapManager(getApplication());
 39         mBMapMan.init(this.getString(R.string.baidumap_key), null);
 40      // 如果使用地圖SDK,請初始化地圖Activity
 41         super.initMapActivity(mBMapMan);
 42         mMapView = (MapView) findViewById(R.id.bmapsView);
 43         mMapView.setBuiltInZoomControls(true); //設置啟用內置的縮放控件
 44       //設置在縮放動畫過程中也顯示overlay,默認為不繪制
 45         mMapView.setDrawOverlayWhenZooming(true);
 46         
 47         // 添加我的位置定位圖層
 48         mLocationOverlay = new MyLocationOverlay(this, mMapView);
 49         mMapView.getOverlays().add(mLocationOverlay);
 50         
 51         // 初始化搜索模塊,注冊事件監聽
 52         mMKSearch = new MKSearch();
 53         mMKSearch.init(mBMapMan, new MKSearchListener(){
 54 
 55             @Override
 56             public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
 57                 // TODO Auto-generated method stub
 58                 
 59             }
 60 
 61             @Override
 62             public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
 63                 // TODO Auto-generated method stub
 64                 
 65             }
 66 
 67             @Override
 68             public void onGetDrivingRouteResult(MKDrivingRouteResult arg0,
 69                     int arg1) {
 70                 // TODO Auto-generated method stub
 71                 
 72             }
 73 
 74             @Override
 75             public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {
 76                 // TODO Auto-generated method stub
 77                 if(arg0 == null){
 78                     return;
 79                 }
 80                 PoiOverlay poioverlay = new PoiOverlay(MyBaiduMap.this, mMapView);
 81                 poioverlay.setData(arg0.getAllPoi());
 82                 mMapView.getOverlays().add(poioverlay);
 83             }
 84 
 85             @Override
 86             public void onGetTransitRouteResult(MKTransitRouteResult arg0,
 87                     int arg1) {
 88                 // TODO Auto-generated method stub
 89                 
 90             }
 91 
 92             @Override
 93             public void onGetWalkingRouteResult(MKWalkingRouteResult arg0,
 94                     int arg1) {
 95                 // TODO Auto-generated method stub
 96                 
 97             }
 98             
 99         });
100         
101         // 注冊定位事件
102         mLocationListener = new LocationListener(){
103 
104             @Override
105             public void onLocationChanged(Location location) {
106                 if (location != null){
107                     GeoPoint pt = new GeoPoint((int)(location.getLatitude()*1e6),
108                             (int)(location.getLongitude()*1e6));
109                     mMapView.getController().animateTo(pt);
110                     mMapView.getController().setZoom(15); //設置地圖zoom級別
111                     //尋找我的位置附近的KFC餐廳
112                     mMKSearch.poiSearchNearBy("KFC", pt, 2000);
113                 }
114             }
115         };
116     }
117     
118     @Override
119     protected void onDestroy(){
120         // 在activity destroy前mBMapMan.destroy()
121         if(mBMapMan != null){
122             mBMapMan.destroy();
123             mBMapMan = null;
124         }
125         super.onDestroy();
126     }
127     
128     @Override
129     protected void onPause(){
130         if(mBMapMan != null){
131             mBMapMan.getLocationManager().removeUpdates(mLocationListener);
132             mLocationOverlay.disableMyLocation();
133             mLocationOverlay.disableCompass(); // 關閉指南針
134             mBMapMan.stop(); //終止百度地圖API,調用此函數后,不會再發生回調
135         }
136         super.onPause();
137     }
138     
139     @Override
140     protected void onResume(){
141         if(mBMapMan != null){
142             // 注冊定位事件,定位后將地圖移動到定位點
143             mBMapMan.getLocationManager().requestLocationUpdates(mLocationListener);
144             mLocationOverlay.enableMyLocation();
145             mLocationOverlay.enableCompass(); // 打開指南針
146             mBMapMan.start(); //開啟百度地圖API
147         }
148         super.onResume();
149     }
150 
151     @Override
152     protected boolean isRouteDisplayed() {
153         // TODO Auto-generated method stub
154         return false;
155     }
156 }

上述代碼可以實現顯示使用者附近2000以內的肯德基餐廳。需要在string.xml文件中增加 baidumap_key 字段。運行如下圖:

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM