@Yano
2016-04-01T16:48:43.000000Z
字数 7062
阅读 2738
Android
今天研究了百度地图 Android SDK,照着例程实现了定位功能。在实现的过程中遇到了一些麻烦,现在记录一下。
Android 定位 SDK 的官方网址是:http://lbsyun.baidu.com/index.php?title=android-locsdk ,里面有详细的参考教程(吐槽一下,写得不够好)。
网页中的“开发指南”中,有详细说明,不重复造轮子。
参考链接:http://blog.csdn.net/u010940300/article/details/43488207
DEMO 下载:链接: http://pan.baidu.com/s/1pK7GIcb 密码: mh8f
DemoApplication.java
package com.example.mymap;
import android.app.Application;
import com.baidu.mapapi.SDKInitializer;
public class DemoApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// 在使用 SDK 各组间之前初始化 context 信息,传入 ApplicationContext
SDKInitializer.initialize(this);
}
}
MainActivity.java
package com.example.mymap;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
public class MainActivity extends Activity {
// 定位相关
LocationClient mLocClient;
public MyLocationListenner myListener = new MyLocationListenner();
private LocationMode mCurrentMode;
BitmapDescriptor mCurrentMarker;
private static final int accuracyCircleFillColor = 0xAAFFFF88;
private static final int accuracyCircleStrokeColor = 0xAA00FF00;
MapView mMapView;
BaiduMap mBaiduMap;
// UI相关
OnCheckedChangeListener radioButtonListener;
Button requestLocButton;
boolean isFirstLoc = true; // 是否首次定位
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestLocButton = (Button) findViewById(R.id.button1);
mCurrentMode = LocationMode.NORMAL;
requestLocButton.setText("普通");
OnClickListener btnClickListener = new OnClickListener() {
public void onClick(View v) {
switch (mCurrentMode) {
case NORMAL:
requestLocButton.setText("跟随");
mCurrentMode = LocationMode.FOLLOWING;
mBaiduMap
.setMyLocationConfigeration(new MyLocationConfiguration(
mCurrentMode, true, mCurrentMarker));
break;
case COMPASS:
requestLocButton.setText("普通");
mCurrentMode = LocationMode.NORMAL;
mBaiduMap
.setMyLocationConfigeration(new MyLocationConfiguration(
mCurrentMode, true, mCurrentMarker));
break;
case FOLLOWING:
requestLocButton.setText("罗盘");
mCurrentMode = LocationMode.COMPASS;
mBaiduMap
.setMyLocationConfigeration(new MyLocationConfiguration(
mCurrentMode, true, mCurrentMarker));
break;
default:
break;
}
}
};
requestLocButton.setOnClickListener(btnClickListener);
RadioGroup group = (RadioGroup) this.findViewById(R.id.radioGroup);
radioButtonListener = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.defaulticon) {
// 传入null则,恢复默认图标
mCurrentMarker = null;
mBaiduMap
.setMyLocationConfigeration(new MyLocationConfiguration(
mCurrentMode, true, null));
}
if (checkedId == R.id.customicon) {
// 修改为自定义marker
mCurrentMarker = BitmapDescriptorFactory
.fromResource(R.drawable.icon_geo);
mBaiduMap
.setMyLocationConfigeration(new MyLocationConfiguration(
mCurrentMode, true, mCurrentMarker,
accuracyCircleFillColor,
accuracyCircleStrokeColor));
}
}
};
group.setOnCheckedChangeListener(radioButtonListener);
// 地图初始化
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
// 开启定位图层
mBaiduMap.setMyLocationEnabled(true);
// 定位初始化
mLocClient = new LocationClient(this);
mLocClient.registerLocationListener(myListener);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true); // 打开gps
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(1000);
mLocClient.setLocOption(option);
mLocClient.start();
}
/**
* 定位SDK监听函数
*/
public class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
// map view 销毁后不在处理新接收的位置
if (location == null || mMapView == null) {
return;
}
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(100).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
mBaiduMap.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);
mBaiduMap.animateMapStatus(MapStatusUpdateFactory
.newMapStatus(builder.build()));
}
}
public void onReceivePoi(BDLocation poiLocation) {
}
}
@Override
protected void onPause() {
mMapView.onPause();
super.onPause();
}
@Override
protected void onResume() {
mMapView.onResume();
super.onResume();
}
@Override
protected void onDestroy() {
// 退出时销毁定位
mLocClient.stop();
// 关闭定位图层
mBaiduMap.setMyLocationEnabled(false);
mMapView.onDestroy();
mMapView = null;
super.onDestroy();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mymap"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="false"
android:resizeable="true"
android:smallScreens="true" />
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<!-- 这个权限用于进行网络定位 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- 这个权限用于访问GPS定位 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位 -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- 获取运营商信息,用于支持提供运营商信息相关的接口 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_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.MOUNT_UNMOUNT_FILESYSTEMS" />
<!-- 访问网络,网络定位需要上网 -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_TASKS" />
<application
android:name="com.example.mymap.DemoApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="LSBdCjaNswoilnnX9me7w7EC5oH46QbV" />
<activity
android:name=".MainActivity"
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" >
</service>
</application>
</manifest>