Android手机通过百度定位

来源:互联网 发布:数据库开放给别人 编辑:程序博客网 时间:2024/04/30 03:10

步骤1:申请秘钥     http://lbsyun.baidu.com/index.php?title=android-locsdk/guide/key(这是百度地址)

步骤2:配置环境,这个步骤一定要做http://lbsyun.baidu.com/index.php?title=android-locsdk/guide/buildprojec(按照这里面的一步一步的做,之前我就被配置文件坑了。)

sourceSets {        main {    jniLibs.srcDirs = ['libs']        }}
android {
        packagingOptions {
            exclude 'META-INF/DEPENDENCIES.txt'
            exclude 'META-INF/LICENSE.txt'
            exclude 'META-INF/NOTICE.txt'
            exclude 'META-INF/NOTICE'
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/DEPENDENCIES'
            exclude 'META-INF/notice.txt'
            exclude 'META-INF/license.txt'
            exclude 'META-INF/dependencies.txt'
            exclude 'META-INF/LGPL2.1'
        }
    }

(这个也是需要的,不然会找不到相应的 Jar)

******如果是项目已经集成了百度地图,那么你的项目一定是有jar包了,所有就不需要再导入百度定位的jar包。Androidstudio再编译时会因为包冲突而运行不起来。即使jar包名不一样也不要怕,只要等会需要的类能找到就可以了。

项目中要导入的东西很少,这是百度API里能找到的。


在<Application>与<activity>标签中加<meta-data>标签,key是我们申请的key.(你的项目包名也要和申请时提供的包名一样)。


关键代码:1,初始化:

一般可以在Android的application里初始化:

public LocationService locationService;
public Vibrator mVibrator;

locationService = new LocationService(getApplicationContext());
mVibrator =(Vibrator)getApplicationContext().getSystemService(Service.VIBRATOR_SERVICE);
SDKInitializer.initialize(getApplicationContext());


一般可以通过点击按钮触发定位功能,也可以自动调用定位功能。

这个项目是需要自动调用定位功能

方法如下:

locationService.registerListener(mListener);//注册监听事件(一定要取消注册,不然会一直获取定位,消耗内存)
locationService.start();//启动

下面是监听的对象

private BDLocationListener mListener = new BDLocationListener() {


@Override
public void onReceiveLocation(BDLocation location) {
// TODO Auto-generated method stub
if (null != location && location.getLocType() != BDLocation.TypeServerError) {
StringBuffer sb = new StringBuffer(256);
sb.append("time : ");
/**
* 时间也可以使用systemClock.elapsedRealtime()方法 获取的是自从开机以来,每次回调的时间;
* location.getTime() 是指服务端出本次结果的时间,如果位置不发生变化,则时间不变
*/
sb.append(location.getTime());
sb.append("\nerror code : ");
sb.append(location.getLocType());
sb.append("\nlatitude : ");
sb.append(location.getLatitude());
sb.append("\nlontitude : ");
sb.append(location.getLongitude());
sb.append("\nradius : ");
sb.append(location.getRadius());
sb.append("\nCountryCode : ");
sb.append(location.getCountryCode());
sb.append("\nCountry : ");
sb.append(location.getCountry());
sb.append("\ncitycode : ");
sb.append(location.getCityCode());
sb.append("\ncity : ");
sb.append(location.getCity());
sb.append("\nDistrict : ");
sb.append(location.getDistrict());
sb.append("\nStreet : ");
sb.append(location.getStreet());
sb.append("\naddr : ");
sb.append(location.getAddrStr());
sb.append("\nDescribe: ");
sb.append(location.getLocationDescribe());
sb.append("\nDirection(not all devices have value): ");
sb.append(location.getDirection());
sb.append("\nPoi: ");
if (location.getPoiList() != null && !location.getPoiList().isEmpty()) {
for (int i = 0; i < location.getPoiList().size(); i++) {
Poi poi = (Poi) location.getPoiList().get(i);
sb.append(poi.getName() + ";");
}
}
if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位结果
sb.append("\nspeed : ");
sb.append(location.getSpeed());// 单位:km/h
sb.append("\nsatellite : ");
sb.append(location.getSatelliteNumber());
sb.append("\nheight : ");
sb.append(location.getAltitude());// 单位:米
sb.append("\ndescribe : ");
sb.append("gps定位成功");
locationService.unregisterListener(mListener); //注销掉监听
locationService.stop();
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 网络定位结果
// 运营商信息
sb.append("\noperationers : ");
sb.append(location.getOperators());
sb.append("\ndescribe : ");
sb.append("网络定位成功");
locationService.unregisterListener(mListener); //注销掉监听
locationService.stop();
} else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果
sb.append("\ndescribe : ");
sb.append("离线定位成功,离线定位结果也是有效的");
locationService.unregisterListener(mListener); //注销掉监听
locationService.stop();
} else if (location.getLocType() == BDLocation.TypeServerError) {
sb.append("\ndescribe : ");
sb.append("服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因");
} else if (location.getLocType() == BDLocation.TypeNetWorkException) {
sb.append("\ndescribe : ");
sb.append("网络不同导致定位失败,请检查网络是否通畅");
} else if (location.getLocType() == BDLocation.TypeCriteriaException) {
sb.append("\ndescribe : ");
sb.append("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机");
}
System.out.println("经度:app----"+location.getLatitude());
System.out.println("维度:app----"+location.getLongitude());
Constants.LATITUDE=location.getLatitude()+"";
Constants.LONGITUDE=location.getLongitude()+"";
if(!Tools.isEmpty(Constants.LATITUDE)){
locationService.unregisterListener(mListener); //注销掉监听
locationService.stop();
}
}
}


};


我的做法是得到经纬度就放在全局变量里。

在监听到获取了定位就取消监听。

locationService.unregisterListener(mListener); //注销掉监听
locationService.stop();

0 0
原创粉丝点击