Android中级第五讲-GPRS定位的兑现

Android中级第五讲--GPRS定位的实现博客出自:http://blog.csdn.net/liuxian13183,转载注明出处! All Right

Android中级第五讲--GPRS定位的实现

博客出自:http://blog.csdn.net/liuxian13183,转载注明出处! All Rights Reserved !  


前段时间在弄GPRS定位的问题,使用google的地图定位,大家也都知道,google现在在中国境内有很多限制,而且国外刷机严重,难免将google的各种服务给刷掉,所以最终采用百度的定位系统,完美实现。现在有时间了,给大家讲一讲,代码并不多。

我还是先说说google的定位吧,说不定有些仁兄需要的呢!

首先判断机器的GPRS模块是否正常,如果不正常,那没办法了,哪家的定位系统都不能用。


百度的定位相对来说要简单的多,为什么呢,因为它只有两三个方法,一般国内的手机GPS功能有被“阉割”的可能,所以一般GPS定位取不到位置,通用的还是GPRS网络定位功能。

如图,导入项目所需包

Android中级第五讲-GPRS定位的兑现

然后在manifest.xml中加入权限,以及定义Service

public void logMsg(String str) {try {mData = str;if ( mTv != null )mTv.setText(mData);} catch (Exception e) {e.printStackTrace();}}public class MyLocationListenner implements BDLocationListener {@Overridepublic void onReceiveLocation(BDLocation location) {if (location == null)return ;StringBuffer sb = new StringBuffer(256);sb.append("time : ");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());if (location.getLocType() == BDLocation.TypeGpsLocation){sb.append("\nspeed : ");sb.append(location.getSpeed());sb.append("\nsatellite : ");sb.append(location.getSatelliteNumber());} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){sb.append("\nprovince:");sb.append(location.getProvince());sb.append("\ncity");sb.append(location.getCity());sb.append("\nstreet");sb.append(location.getDistrict());sb.append("\naddr : ");sb.append(location.getAddrStr());}sb.append("\nsdk version : ");sb.append(mLocationClient.getVersion());logMsg(sb.toString());}public void onReceivePoi(BDLocation poiLocation) {if (poiLocation == null){return ;}StringBuffer sb = new StringBuffer(256);sb.append("Poi time : ");sb.append(poiLocation.getTime());sb.append("\nerror code : ");sb.append(poiLocation.getLocType());sb.append("\nlatitude : ");sb.append(poiLocation.getLatitude());sb.append("\nlontitude : ");sb.append(poiLocation.getLongitude());sb.append("\nradius : ");sb.append(poiLocation.getRadius());if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation){sb.append("\naddr : ");sb.append(poiLocation.getAddrStr());} if(poiLocation.hasPoi()){sb.append("\nPoi:");sb.append(poiLocation.getPoi());}else{sb.append("noPoi information");}logMsg(sb.toString());}}

就是这样,一个麻烦至极的定位功能完成了!源码下载地址:http://download.csdn.net/detail/liuxian13183/5088512