以前的应用使用了百度的定位和百度地图,Android5.0 不能安装,出现了
Installation error: INSTALL_FAILED_DUPLICATE_PERMISSION perm=android.permission.BAIDU_LOCATION_SERVICE pkg=com.palmtrends.yl的错误。原来是android 5.0无法使用以前的百度定位SDK,所以决定重新写一个使用百度地图最新SDK的Demo。
2014年11月07日 百度地图Android SDKv3.2.0发布,2014年12月11日 Android定位SDK:v5.0产品上线,
新增:新增国外(全球)定位能力开放,直接替换开发包无需修改代码;
优化:Android L 适配优化;
修复:修复GPS定位方式未返回地址的问题。
本demo使用的是最新的百度地图AndroidSDK v3.2.0和Android定位SDK:v5.0。最后效果图为Android5.0.1上测试。
1.功能介绍
a 定位功能;
b 添加覆盖物marker,弹出自定义布局的InfoWindow及其点击事件处理;
c 屏幕中心自定义View和PopupWindow,随地图移动显示当前位置信息;
d 百度地图POI范围内检索参数PoiBoundSearchOption和附近检索参数PoiNearbySearchOption;
2.申请密钥和安全码
这里就不说了,http://lbsyun.baidu.com/apiconsole/key;
3.第一个百度地图
我这里是直接把BaiduMapsApiDemo中的LocationDemo拷贝过来的,没做任何修改,所以引用的是locSDK_3.1.jar和libBaiduMapSDK_v3_1_0.so,后面会换成最新的。
BaseMapActivity.java
public class BaseMapActivity extends Activity {
private static final String TAG = BaseMapActivity.class.getSimpleName();;
// 定位相关
LocationClient mLocClient;
public MyLocationListenner myListener = new MyLocationListenner();
private LocationMode mCurrentMode;
BitmapDescriptor mCurrentMarker;
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_location);
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;
}
}
};
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));
}
}
};
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());
MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
mBaiduMap.animateMapStatus(u);
}
}
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();
}
}
public class DemoApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
SDKInitializer.initialize(this);
}
}
说明和注意事项:
a DemoApplication中的SDKInitializer.initialize(this)注意:在SDK各功能组件使用之前都需要调用,因此我们建议该方法放在Application的初始化方法中
在应用程序创建时初始化SDK引用Context 全局变量:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//在使用SDK各组件之前初始化context信息,传入ApplicationContext
//注意该方法要再setContentView方法之前实现
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
}
}
b 在application中添加开发密钥和添加所需权限
<application>
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="开发者 key" />
</application>
<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.READ_PHONE_STATE" />
<application
android:name="com.example.baiduMap.application.DemoApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
>
如果遇到
Couldn't load locSDK3 from loaderdalvik.system.PathClassLoader...的错误,可以在libs下再建一个armeabi-v7a的文件夹,把armeabi中的文件拷贝过去。如下图:
其他就不做说明了,
http://developer.baidu.com/map/index.php?title=androidsdk/guide/hellobaidumap讲得很详细。
最后效果:
Demo下载地址: