android 百度地图定位功能实现
jopen
11年前
package com.example.foreveross.office; 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.MKMapViewListener; import com.baidu.mapapi.map.MapController; import com.baidu.mapapi.map.MapPoi; import com.baidu.mapapi.map.MapView; import com.baidu.mapapi.map.MyLocationOverlay; import com.baidu.platform.comapi.basestruct.GeoPoint; import com.example.wenandroid.R; import android.app.Activity; import android.content.res.Configuration; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.view.Menu; import android.widget.FrameLayout; import android.widget.Toast; public class MapActivity extends Activity { BMapManager mBMapMan = null; MapView mMapView = null; MapController mMapController; MyLocationOverlay mLocationOverlay; LocationData locData = null; LocationClient mLocClient; public MyLocationListenner myListener = new MyLocationListenner(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mBMapMan = new BMapManager(getApplication()); mBMapMan.init("BB3299207a600b8a035b76982adde60f", null); setContentView(R.layout.activity_main); mMapView = (MapView) findViewById(R.id.bmapsView); mMapView.setBuiltInZoomControls(true); // 设置启用内置的缩放控件 mMapController = mMapView.getController(); mMapController.setZoom(12);// 设置地图zoom级别 // 定位初始化 mLocClient = new LocationClient(this); locData = new LocationData(); mLocClient.registerLocationListener(myListener); LocationClientOption option = new LocationClientOption(); option.setOpenGps(true);// 打开gps option.setCoorType("bd09ll"); // 设置坐标类型 option.setScanSpan(5000); mLocClient.setLocOption(option); mLocClient.start(); // 定位图层初始化 mLocationOverlay = new MyLocationOverlay(mMapView); // 设置定位数据 mLocationOverlay.setData(locData); // 添加定位图层 mMapView.getOverlays().add(mLocationOverlay); mLocationOverlay.enableCompass(); // 修改定位数据后刷新图层生效 mMapView.refresh(); } //实现监听器 public class MyLocationListenner implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { if (location == null) return ; locData.latitude = location.getLatitude(); locData.longitude = location.getLongitude(); //如果不显示定位精度圈,将accuracy赋值为0即可 locData.accuracy = location.getRadius(); locData.direction = location.getDerect(); //更新定位数据 mLocationOverlay.setData(locData); //更新图层数据执行刷新后生效 mMapView.refresh(); //是手动触发请求或首次定位时,移动到定位点 mMapController.animateTo(new GeoPoint((int)(locData.latitude* 1e6), (int)(locData.longitude * 1e6))); } public void onReceivePoi(BDLocation poiLocation) { if (poiLocation == null){ return ; } } } @Override protected void onDestroy() { // TODO Auto-generated method stub mMapView.destroy(); if (mBMapMan != null) { mBMapMan.destroy(); mBMapMan = null; } super.onDestroy(); } @Override protected void onPause() { // TODO Auto-generated method stub mMapView.onPause(); if (mBMapMan != null) { mBMapMan.stop(); } super.onPause(); } @Override protected void onResume() { // 获取位置管理者,视图根据位置监听更新位置 mLocationOverlay.getMyLocation(); mLocationOverlay.enableCompass(); mMapView.onResume(); if (mBMapMan != null) { mBMapMan.start(); } super.onResume(); } }
需要用到的jar包大家可以到百度地图api网站上下载。主要依赖于下列包:
manifest.xml配置如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.wenandroid" android:versionCode="1" android:versionName="1.0" > <supports-screens android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" android:resizeable="true" android:anyDensity="true"> </supports-screens> <uses-sdk android:minSdkVersion="3" /> <permission android:name="android.permission.BAIDU_LOCATION_SERVICE"></permission> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission> <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission> <uses-permission android:name="android.permission.READ_LOGS"></uses-permission> <uses-permission android:name="android.permission.VIBRATE"></uses-permission> <uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission> <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission> <uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE"></uses-permission> <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission> <uses-permission android:name="android.permission.ACCESS_GPS"></uses-permission> <!--授予设置手机屏幕权限 --> <uses-permission android:name="android.permission.SET_WALLPAPER"/> <application android:allowBackup="true" android:icon="@drawable/first" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.foreveross.office.MapActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"> <!-- <intent-filter> <action android:name="com.baidu.location.service_v2.9"></action> </intent-filter>--> </service> </application> </manifest>
界面只有一个mapview地图控件,在此列出activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.baidu.mapapi.map.MapView android:id="@+id/bmapsView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" /> > </LinearLayout>来自:http://blog.csdn.net/zhu_hua_jie/article/details/10382433