android开发百度地图定位功能实现

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

android 百度地图定位功能实现
00
package com.example.foreveross.office;
1
00
2
00
import com.baidu.location.BDLocation;
3
00
import com.baidu.location.BDLocationListener;
4
00
import com.baidu.location.LocationClient;
5
00
import com.baidu.location.LocationClientOption;
6
00
import com.baidu.mapapi.BMapManager;
7
00
import com.baidu.mapapi.map.LocationData;
8
00
import com.baidu.mapapi.map.MKMapViewListener;
9
01
import com.baidu.mapapi.map.MapController;
01
import com.baidu.mapapi.map.MapPoi;
1
01
import com.baidu.mapapi.map.MapView;
2
01
import com.baidu.mapapi.map.MyLocationOverlay;
3
01
import api.basestruct.GeoPoint; 4
01
import com.example.wenandroid.R;
5
01
01
import android.app.Activity;
7
01
import android.content.res.Configuration; 8
01
import android.location.Location;
9
02
import android.location.LocationListener; 0
02
import android.location.LocationManager;
1
02
import android.os.Bundle;
2
02
import android.view.Menu;
3
02
import android.widget.FrameLayout;
4
02
import android.widget.Toast;
5
02
6
02
public class MapActivity extends Activity { 7
02
BMapManager mBMapMan = null;
8
02
MapView mMapView = null;
9
03
MapController mMapController;
03
MyLocationOverlay mLocationOverlay;
1
03 LocationData locData = null;
03
3
LocationClient mLocClient;
03 4 public MyLocationListenner myListener = new MyLocationListenner();
03
5
03
6
@Override
03
7
protected void onCreate(Bundle savedInstanceState) {
03
8
super.onCreate(savedInstanceState);
03
9
mBMapMan = new BMapManager(getApplication());
04
mBMapMan.init("BB3299207a600b8a035b76982adde60f", null);
04
1
setContentView(yout.activity_main);
04
2
mMapView = (MapView) findViewById(R.id.bmapsView);
04
3
mMapView.setBuiltInZoomControls(true);
04
4
// 设置启用内置的缩放控件
04
5
mMapController = mMapView.getController();
04
6
mMapController.setZoom(12);// 设置地图zoom级别
04
7
04 // 定位初始化
04
mLocClient = new LocationClient(this);
9
05
locData = new LocationData();
05
mLocClient.registerLocationListener(myListener);
1
05
LocationClientOption option = new LocationClientOption(); 2
05
option.setOpenGps(true);// 打开gps
3
05
option.setCoorType("bd09ll"); // 设置坐标类型
4
05
option.setScanSpan(5000);
5
05
mLocClient.setLocOption(option);
6
05
mLocClient.start();
7
05
8
05
// 定位图层初始化
9
06
mLocationOverlay = new MyLocationOverlay(mMapView);
06
// 设置定位数据
1
06
mLocationOverlay.setData(locData);
2
06
// 添加定位图层
3
06 mMapView.getOverlays().add(mLocationOverlay);
06
5
mLocationOverlay.enableCompass(); 06
6
// 修改定位数据后刷新图层生效
06
7
mMapView.refresh();
06
8
06
9
}
07
//实现监听器
07 1 public class MyLocationListenner implements BDLocationListen er {
072
07
3
@Override
07
4
public void onReceiveLocation(BDLocation location) {
07
5
if (location == null)
07
6
return ;
07
7
07
8
titude = location.getLatitude();
07
9
locData.longitude = location.getLongitude();
08
//如果不显示定位精度圈,将accuracy赋值为0即可
08
1
locData.accuracy = location.getRadius(); 08
2
locData.direction = location.getDerect();
08
3
//更新定位数据
08
4
mLocationOverlay.setData(locData);
08
5
//更新图层数据执行刷新后生效
08
6
mMapView.refresh();
08
7
//是手动触发请求或首次定位时,移动到定位点08
8
08 9 mMapController.animateTo(newGeoPoint((int)(locData .latitude* 1e6), (int)(locData.longitude * 1e6)));
09
}
09
1
09
2
public void onReceivePoi(BDLocation poiLocation) {
09
3
if (poiLocation == null){
09
4
return ;
09
5
}
09
6
}
09
}
7
09
8
09
@Override
9
10
protected void onDestroy() {
10
// TODO Auto-generated method stub 1
10
mMapView.destroy();
2
103 if (mBMapMan != null) {
104 mBMapMan.destroy();
10
mBMapMan = null;
5
10
}
6
10
super.onDestroy();
7
10
}
8
10
9
11
@Override
11
protected void onPause() {
1
11
// TODO Auto-generated method stub 2
11
mMapView.onPause();
3
11
if (mBMapMan != null) {
4
11
mBMapMan.stop();
5
11
}
6
11
super.onPause();
7
11
}
8
11
9
12
@Override
12
protected void onResume() {
1
12
// 获取位置管理者,视图根据位置监听更新位置2
123 mLocationOverlay.getMyLocation();
124 mLocationOverlay.enableCompass();
12
mMapView.onResume();
5
12
if (mBMapMan != null) {
6
12
mBMapMan.start();
7
12
}
8
12
super.onResume();
9
13
}
131
132 }
需要用到的jar包大家可以到百度地图api网站上下载。

主要依赖于下列包:
manifest.xml配置如下:
1
<?xml version="1.0" encoding="utf-8"?>
0 2 <manifest xmlns:android="/apk/res/androi d"
3
package="com.example.wenandroid"
4
android:versionCode="1"
5
android:versionName="1.0" >
6
7
<supports-screens android:largeScreens="true" 0
8
android:normalScreens="true"
0 android:smallScreens="true"
9
10
android:resizeable="true"
11
android:anyDensity="true"> 12
</supports-screens>
13
14
<uses-sdk
15
android:minSdkVersion="3" /> 16
17 <permissionandroid:name="android.permission.BAIDU_LOCATION_SER VICE"></permission>
18 <uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
19 <uses-permissionandroid:name="android.permission.ACCESS_FINE_LO CATION"></uses-permission>
20 <uses-permissionandroid:name="android.permission.ACCESS_WIFI_ST ATE"></uses-permission>
21 <uses-permissionandroid:name="android.permission.ACCESS_NETWORK _STATE"></uses-permission>
22 <uses-permissionandroid:name="android.permission.CHANGE_WIFI_ST ATE"></uses-permission>
23 <uses-permissionandroid:name="android.permission.READ_PHONE_ST ATE"></uses-permission>
24 <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL _STORAGE"></uses-permission>
2 <uses-permission android:name="android.permission.INTERNET" />
5
26 <uses-permissionandroid:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
27 <uses-permission android:name="android.permission.READ_LOGS"></uses-permission>
28 <uses-permission android:name="android.permission.VIBRATE"></us es-permission>
29 <uses-permissionandroid:name="android.permission.WRITE_SETTINGS "></uses-permission>
30 <uses-permissionandroid:name="android.permission.CALL_PHONE"></uses-permission>
31 <uses-permissionandroid:name="android.permission.BAIDU_LOCATION _SERVICE"></uses-permission>
32 <uses-permissionandroid:name="android.permission.ACCESS_MOCK_LO CATION"></uses-permission>
33 <uses-permissionandroid:name="android.permission.ACCESS_GPS"></uses-permission>
34
<!--授予设置手机屏幕权限 -->
35 <uses-permission android:name="android.permission.SET_WALLPAP ER"/>
36
37
<application 38
android:allowBackup="true"
39
android:icon="@drawable/first" 40
android:label="@string/app_name"
41
android:theme="@style/AppTheme" >
4
2
<activity
4 3 android:name="com.example.foreveross.office.MapActivit y"
4
4
android:label="@string/app_name" >
4
5
<intent-filter>
4 6 <action android:name="android.intent.action.MAIN" />
47
4 8 <category android:name=" UNCHER" />
4
9
</intent-filter> 5
</activity>
51
52
5 3 <service android:name="com.baidu.location.f" android:enab led="true"
54 android:process=":remote">
5
5
<!-- <intent-filter>
5 6 <action
android:name="com.baidu.location.service_v2.9"></action>
5
7
</intent-filter>--> 5
8
</service>
5 </application>
9
6
</manifest>
界面只有一个mapview地图控件,在此列出activity_main.xml:01 <?xml version="1.0" encoding="utf-8"?>
0 2 <LinearLayout xmlns:android="/apk/res/an droid"
3
android:layout_width="fill_parent"
4
android:layout_height="fill_parent"
5
android:orientation="vertical"
6
>
7
8
<com.baidu.mapapi.map.MapView android:id="@+id/bmapsView"
9
android:layout_width="fill_parent"
1
android:layout_height="fill_parent"
1
1
android:clickable="true" />
1
2
>
1
3
1
4
</LinearLayout>
来自:/zhu_hua_jie/article/details/10382433。

相关文档
最新文档