(1)BaiDuMapActivity.java
- package intvehapp.intvehapp;
- import com.baidu.location.BDLocation;
- import com.baidu.location.BDLocationListener;
- import com.baidu.location.LocationClient;
- import com.baidu.location.LocationClientOption;
- import com.baidu.mapapi.SDKInitializer;
- import com.baidu.mapapi.map.BaiduMap;
- import com.baidu.mapapi.map.MapStatus;
- import com.baidu.mapapi.map.MapStatusUpdateFactory;
- import com.baidu.mapapi.map.MapView;
- import com.baidu.mapapi.map.MyLocationData;
- import com.baidu.mapapi.model.LatLng;
- import android.app.Activity;
- import android.os.Bundle;
- public class BaiDuMapActivity extends Activity{
- /**
- * 定位SDK核心类
- */
- private LocationClient locationClient;
- /**
- * 定位监听
- */
- public MyLocationListenner myListener = new MyLocationListenner();
- /**
- * 百度地图控件
- */
- private MapView mapView;
- /**
- * 百度地图对象
- */
- private BaiduMap baiduMap;
- boolean isFirstLoc = true; // 是否首次定位
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- SDKInitializer.initialize(getApplicationContext());
- setContentView(R.layout.activity_bai_du_map);
- /**
- * 地图初始化
- */
- //获取百度地图控件
- mapView = (MapView) findViewById(R.id.bmapView);
- //获取百度地图对象
- baiduMap = mapView.getMap();
- // 开启定位图层
- baiduMap.setMyLocationEnabled(true);
- /**
- * 定位初始化
- */
- //声明定位SDK核心类
- locationClient = new LocationClient(this);
- //注册监听
- locationClient.registerLocationListener(myListener);
- //定位配置信息
- LocationClientOption option = new LocationClientOption();
- option.setOpenGps(true); // 打开gps
- option.setCoorType("bd09ll"); // 设置坐标类型
- option.setScanSpan(1000);//定位请求时间间隔
- locationClient.setLocOption(option);
- //开启定位
- locationClient.start();
- }
- /**
- * 定位SDK监听函数
- */
- public class MyLocationListenner implements BDLocationListener {
- @Override
- public void onReceiveLocation(BDLocation location) {
- // map view 销毁后不在处理新接收的位置
- if (location == null || mapView == null) {
- return;
- }
- MyLocationData locData = new MyLocationData.Builder()
- .accuracy(location.getRadius())
- // 此处设置开发者获取到的方向信息,顺时针0-360
- .direction(100).latitude(location.getLatitude())
- .longitude(location.getLongitude()).build();
- baiduMap.setMyLocationData(locData);
- if (isFirstLoc) {
- isFirstLoc = false;
- LatLng ll = new LatLng(location.getLatitude(),
- location.getLongitude());
- MapStatus.Builder builder = new MapStatus.Builder();
- builder.target(ll).zoom(18.0f);
- baiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
- }
- }
- public void onReceivePoi(BDLocation poiLocation) {
- }
- }
- @Override
- protected void onPause() {
- mapView.onPause();
- super.onPause();
- }
- @Override
- protected void onResume() {
- mapView.onResume();
- super.onResume();
- }
- @Override
- protected void onDestroy() {
- // 退出时销毁定位
- locationClient.stop();
- // 关闭定位图层
- baiduMap.setMyLocationEnabled(false);
- mapView.onDestroy();
- mapView = null;
- super.onDestroy();
- }
- }
(2)activity_bai_du_map.xml:增加百度地图控件
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
- android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin"
- tools:context="intvehapp.intvehapp.BaiDuMapActivity">
- <com.baidu.mapapi.map.MapView
- android:id="@+id/bmapView"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:clickable="true" />
- </RelativeLayout>
(3)AndroidManifest.xml:增加百度地图SDK需要的权限、API_KEY和定位服务
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="intvehapp.intvehapp" >
- <!-- 百度地图API所需权限 -->
- <uses-permission android:name="android.permission.GET_ACCOUNTS" />
- <uses-permission android:name="android.permission.USE_CREDENTIALS" />
- <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
- <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
- <uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
- <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
- <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
- <uses-permission android:name="android.permission.READ_PHONE_STATE" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- <uses-permission android:name="android.permission.BROADCAST_STICKY" />
- <uses-permission android:name="android.permission.WRITE_SETTINGS" />
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
- <!-- 访问精确位置的权限 -->
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:supportsRtl="true"
- android:theme="@style/AppTheme" >
- <activity android:name=".BaiDuMapActivity" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <!--百度API_KEY-->
- <meta-data
- android:name="com.baidu.lbsapi.API_KEY"
- android:value="iXDGdZtFiPifnNm6dUEFwDRXYQVeZ37V" />
- <!--百度定位服务-->
- <service
- android:name="com.baidu.location.f"
- android:enabled="true"
- android:process=":remote" >
- </service>
- </application>
- </manifest>