@Override
protected void onDestroy() {
super.onDestroy();
mLocationClient.stop();
map.onDestroy();
}
首先要知道定位有两种方式,一种是GPS定位,一种是根据附近基站的网络定位。GPS精度高,但是在室内信号不好一般无法使用,网络定位使用的范围更大,但是精度堪忧。安卓对于两种定位方式都有原生的解决方案,但是网络定位因为特殊原因不能使用,GPS使用起来也不方便。现在的第三方SDK已经很成熟了,如百度和高德,今天我简单学习了百度SDK,对LBS有了简单的了解。这里做一个小总结,其实这主要是API的学习了,方便以后的使用吧。
1.集成百度地图SDK:首先你要有一个百度账号,然后在http://developer.baidu.com/user/info填写信息成为一名百度开发者。然后在http://lbsyun.baidu.com/apiconsole/key#就可以创建应用了,应用类型填Android SDK。其中注意要填写的一个值SHA1,这是是签名文件的SHA1指纹。要找到这个值,我们要打开一个以前的工程(打包过的),打开右侧gradle工具,:app---Tasks---android----signingReport,打开这个文件,就可以找到这个值。接下来下载SDK,根据相关提示配置Manifest文件和SDK就可以了。
2.得到自己的位置:首先new一个LocationClient,为它注册一个自定义监听器,这个监听器中有个方法onReceiveLocation在每次得到位置信息时调用,通过这个方法的参数可以得到各种想要的位置信息,如经纬度,国家省份市区街道。client调用start后获得一次位置。部分代码:
mLocationClient=new LocationClient(getApplicationContext());
mLocationClient.registerLocationListener(new MyLocationListener());
mLocationClient.start();
public class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation bdLocation) {
StringBuilder builder=new StringBuilder();
builder.append("纬度:").append(bdLocation.getLatitude()).append("\n");
builder.append("经度:").append(bdLocation.getLongitude()).append("\n");
builder.append("定位方式:");
if (bdLocation.getLocType()==BDLocation.GPS_ACCURACY_BAD){
builder.append("GPS").append("\n");
} else {
if (bdLocation.getLocType()==BDLocation.TypeNetWorkLocation){
builder.append("网络").append("\n");
}
}
builder.append("国家:").append(bdLocation.getCountry()).append("\n");
builder.append("省份:").append(bdLocation.getProvince()).append("\n");
builder.append("市:").append(bdLocation.getCity()).append("\n");
builder.append("区:").append(bdLocation.getDistrict()).append("\n");
builder.append("街道:").append(bdLocation.getStreet()).append("\n");
tv.setText(builder);
}
}
3.定时进行定位:借助一个为client设置参数的工具类LocationClientOption:
LocationClientOption option=new LocationClientOption();
option.setScanSpan(5000);
option.setIsNeedAddress(true);
mLocationClient.setLocOption(option);
这里每五秒会重新进行一次定位。option还有很多作用,比如设置定位方式等。要注意及时停止client,否则会在后台大量消耗:
4.显示地图:SDK提供了一个炫酷的自定义组件MapView,放在布局文件里就可以用了,还需要一句初始化代码:
SDKInitializer.initialize(getApplicationContext());
这一句必须在为活动设置视图也就是setContentView前执行,否则组件不能使用报错。而且参数必须是APP的context,亲测activity的不行。
在布局里放入:
<?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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="example.com.mylbs.MainActivity">
<TextView
android:visibility="gone"
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<com.baidu.mapapi.map.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.baidu.mapapi.map.MapView>
</RelativeLayout>
最好让这个控件的生命周期与活动同步,节约资源:
@Override
protected void onDestroy() {
super.onDestroy();
mLocationClient.stop();
map.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
map.onResume();
}
@Override
protected void onPause() {
super.onPause();
map.onPause();
}