android使用百度地图、定位SDK实现地图和定位功能!(最新、可用+吐槽)

一、吐槽
在百度地图看了几个小时的教程,发现种种问题,很大部分是百度对于定位API 网页上的DEMO代码一大堆错误!这极可能是定位SDK升级后而网页上的DEMO部分代码沿用旧版导致的。
错误1:
 
在该示例中取了个变量叫mLocationClient,后面居然叫mLocClient,我找了半天,说这变量哪来的呢
 
错误2:
这个错误是最致命的,在网页上的DEMO(开发指南)里居然连开始定位这个函数至始至终都没调用过!!!
 
新手咋看以为调用这个就可以定位了,擦,其实还应该调用mLocClient.start(); 才行,否则压根就没启动定位。。。
 
二、使用百度地图V2.2和定位V4.0实现地图和定位功能
1、首先将必要的库文件导入到你的项目里,具体参看这里
http://developer.baidu.com/map/sdkandev-2.htm
http://developer.baidu.com/map/geosdk-android-developv4.0.htm
2、编写代码 (MainActivity.java)
 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package com.android.test;  
    
import android.app.Activity;  
import android.os.Bundle;  
import android.os.Handler;  
import android.util.Log;  
import android.view.Menu;  
import android.view.MenuItem;  
    
import com.baidu.location.BDLocation;  
import com.baidu.location.BDLocationListener;  
import com.baidu.location.LocationClient;  
import com.baidu.location.LocationClientOption;  
import com.baidu.mapapi.BMapManager;  
import com.baidu.mapapi.map.LocationData;  
import com.baidu.mapapi.map.MapController;  
import com.baidu.mapapi.map.MapView;  
import com.baidu.mapapi.map.MyLocationOverlay;  
import com.baidu.platform.comapi.basestruct.GeoPoint;  
    
public class MainActivity extends Activity {  
    
     //百度Key  
     private static final String BD_KEY= "请在这里输入你的百度地图Key,这里我删除了我自己的,你自己填" ;   
        
     //地图管理器  
     private BMapManager mBMapMan= null ;  
     //地图视图  
     private MapView mMapView= null ;  
        
     private LocationClient mLocationClient= null ;  
        
      //我的位置覆盖物  
     private MyLocationOverlay myOverlay;  
     //位置在图层中的索引  
     private int myOverlayIndex= 0 ;  
     //是否定位到我的位置  
     private boolean bmyLocal= true ;  
        
     @Override  
     protected void onCreate(Bundle savedInstanceState) {  
         super .onCreate(savedInstanceState);  
            
         mBMapMan= new BMapManager(getApplication());    
         mBMapMan.init(BD_KEY, null );   
            
         setContentView(R.layout.activity_main);  
       
         //注意:请在试用setContentView前初始化BMapManager对象,否则会报错    
         setContentView(R.layout.activity_main);    
         mMapView=(MapView)findViewById(R.id.bmapsView);    
         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( 12 ); //设置地图zoom级别    
            
            
         定位功能代码开始  
         mLocationClient= new LocationClient( this );  
         mLocationClient.setAK(BD_KEY);  
            
         myOverlay= new MyLocationOverlay(mMapView);  
         LocationClientOption option= new LocationClientOption();  
         option.setOpenGps( true );  
         option.setAddrType( "all" ); //返回的定位结果包含地址信息  
         option.setCoorType( "bd09ll" ); //返回的定位结果是百度经纬度,默认值gcj02  
            
         //当不设此项,或者所设的整数值小于1000(ms)时,采用一次定位模式。  
         //option.setScanSpan(5000);//设置发起定位请求的间隔时间为5000ms  
         option.disableCache( true ); //禁止启用缓存定位  
         option.setPoiNumber( 5 ); //最多返回POI个数  
         option.setPoiDistance( 1000 ); //poi查询距离  
         option.setPoiExtraInfo( true ); //是否需要POI的电话和地址等详细信息   
         mLocationClient.setLocOption(option);  
         //注册位置监听  
         mLocationClient.registerLocationListener(locationListener);  
         if (mLocationClient!= null &&!mLocationClient.isStarted())  
         {  
             mLocationClient.requestLocation();  
             mLocationClient.start();  
         }  
         else  
             Log.e( "LocSDK3" , "locClient is null or not started" );  
            
            
     }  
        
     private BDLocationListener locationListener= new BDLocationListener()  
     {  
         @Override  
         public void onReceiveLocation(BDLocation arg0) {  
             Dispose(arg0);  
         }  
    
         @Override  
         public void onReceivePoi(BDLocation arg0) {  
             Dispose(arg0);  
                
         }  
         private void Dispose(BDLocation location)  
         {  
             if (location== null )  
                 return ;  
              StringBuffer sb = new StringBuffer( 256 );  
              sb.append( "time : " );  
              sb.append(location.getTime());  
              sb.append( "\nerror code : " );  
              sb.append(location.getLocType());  
              sb.append( "\nlatitude : " );  
              sb.append(location.getLatitude());  
              sb.append( "\nlontitude : " );  
              sb.append(location.getLongitude());  
              sb.append( "\nradius : " );  
              sb.append(location.getRadius());  
              if (location.getLocType() == BDLocation.TypeGpsLocation){  
                  sb.append( "\nspeed : " );  
                  sb.append(location.getSpeed());  
                  sb.append( "\nsatellite : " );  
                  sb.append(location.getSatelliteNumber());  
              }   
              else if (location.getLocType() == BDLocation.TypeNetWorkLocation){  
                  sb.append( "\naddr : " );  
                  sb.append(location.getAddrStr());  
              }  
              //poiLocation  
              if (location.hasPoi()){  
                  sb.append( "\nPoi:" );  
                  sb.append(location.getPoi());  
                  } else {  
                  sb.append( "\nnoPoi information" );  
             }   
              //需要定位到我的位置?  
              if (bmyLocal)  
              {  
                  double lat=location.getLatitude();  
                  double lon=location.getLongitude();  
                     
                    
                  LocationData data= new LocationData();  
                  data.latitude=lat;  
                  data.longitude=lon;  
                  data.direction= 2 .0f;  
                  myOverlay.setData(data);  
                  //检查覆盖物是否存在,存在则修改,否则则添加  
                  if (mMapView.getOverlays().contains(myOverlay))  
                  {  
                      mMapView.getOverlays().set(myOverlayIndex,myOverlay);  
                  }  
                  else {  
                      myOverlayIndex=mMapView.getOverlays().size();  
                      mMapView.getOverlays().add(myOverlay);   
                  }  
                     
                     
                  GeoPoint geoPoint= new GeoPoint(( int )(lat* 1E6),( int )(lon* 1E6));                  
                  mMapView.getController().setCenter(geoPoint);  
                                 
                  mMapView.refresh();  
                  bmyLocal= false ;  
              }  
              Log.e( "定位结果:" ,sb.toString());  
         }  
     };  
        
     //创建菜单  
         @Override  
         public boolean onCreateOptionsMenu(Menu menu)  
         {  
             //组、ID、排序、菜单名  
    
             menu.add( 0 , 1 , 1 , "我的位置" ).setIcon(R.drawable.root_icon);  
        
             return true ;  
         }  
            
         //处理菜单  
         @Override  
         public boolean onOptionsItemSelected(MenuItem item)  
         {  
             switch (item.getItemId())  
             {  
        
             case 1 //我的位置  
                 bmyLocal= true ;  
                 mLocationClient.requestLocation();  
                 mLocationClient.start();  
        
                 break ;  
            
             }  
             return true ;  
         }  
            
        
         @Override    
         protected void onDestroy(){    
                 if (mLocationClient!= null &&mLocationClient.isStarted())  
                     mLocationClient.stop();  
                 mMapView.destroy();    
                 if (mBMapMan!= null ){    
                         mBMapMan.destroy();    
                         mBMapMan= null ;    
                 }    
                 super .onDestroy();    
         }    
         @Override    
         protected void onPause(){    
                 mMapView.onPause();    
                 if (mBMapMan!= null ){    
                        mBMapMan.stop();    
                 }    
                 super .onPause();    
         }    
         @Override    
         protected void onResume(){    
                 mMapView.onResume();    
                 if (mBMapMan!= null ){    
                         mBMapMan.start();    
                 }    
                super .onResume();    
         }    
}
 
3、在 AndroidManifest.xml里注册和添加权限
 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<? xml version = "1.0" encoding = "utf-8" ?>  
< manifest xmlns:android = "http://schemas.android.com/apk/res/android"  
     package = "com.android.test"  
     android:versionCode = "1"  
     android:versionName = "1.0"  
     >  
    
     <!-- 添加屏幕及版本支持 -->  
     < supports-screens android:largeScreens = "true"    
                 android:normalScreens = "true"    
                 android:smallScreens = "true"    
                 android:resizeable = "true"    
                 android:anyDensity = "true" />    
     < uses-sdk android:minSdkVersion = "7" />  
    
    <!-- 在sdcard中创建/删除文件的权限 -->  
     < uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE" />  
     <!-- 挂载和反挂载的权限 -->  
     < uses-permission android:name = "android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />  
     <!-- 读取手机状态 ,如来了新电话-->  
     < uses-permission android:name = "android.permission.READ_PHONE_STATE" />  
     <!-- 震动权限 -->  
     < uses-permission android:name = "android.permission.VIBRATE" />    
     <!-- 网络访问权限 -->  
     < uses-permission android:name = "android.permission.ACCESS_NETWORK_STATE" />  
     < uses-permission android:name = "android.permission.ACCESS_WIFI_STATE" />  
     < uses-permission android:name = "android.permission.CHANGE_WIFI_STATE" />   
     < uses-permission android:name = "android.permission.INTERNET" />  
        
     <!-- 百度地图定位功能所需权限 -->  
     < uses-permission android:name = "android.permission.ACCESS_FINE_LOCATION" />      
     < permission android:name = "android.permission.BAIDU_LOCATION_SERVICE" />    
     < uses-permission android:name = "android.permission.BAIDU_LOCATION_SERVICE" />    
     < uses-permission android:name = "android.permission.ACCESS_COARSE_LOCATION" />    
     < uses-permission android:name = "android.permission.ACCESS_MOCK_LOCATION" />      
     < uses-permission android:name = "android.permission.ACCESS_GPS" />      
     < uses-permission android:name = "android.permission.READ_LOGS" />  
     < application  
         android:allowBackup = "true"          
         android:icon = "@drawable/icon"  
         android:label = "@string/app_name"  
         android:theme = "@android:style/Theme.Black"    
         android:persistent = "true" >  
         < activity  
             android:name = ".MainActivity"  
             android:label = "@string/app_name"  
             android:screenOrientation = "portrait"   
             >  
             < intent-filter >  
                   < action android:name = "android.intent.action.MAIN" />  
                    < category android:name = "android.intent.category.LAUNCHER" />  
             </ intent-filter >  
         </ activity >  
            
          
         <!-- 百度定位服务    android:permission="android.permission.BAIDU_LOCATION_SERVICE">-->  
         < service android:name = "com.baidu.location.f" android:enabled = "true" android:process = ":remote" >  
             
         </ service >  
          
     </ application >  
    
</ manifest >
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值