前段时间在弄GPRS定位的问题,使用google的地图定位,大家也都知道,google现在在中国境内有很多限制,而且国外刷机严重,难免将google的各种服务给刷掉,所以最终采用百度的定位系统,完美实现。现在有时间了,给大家讲一讲,代码并不多。
我还是先说说google的定位吧,说不定有些仁兄需要的呢!
首先判断机器的GPRS模块是否正常,如果不正常,那没办法了,哪家的定位系统都不能用。
[html] 
LocationManager alm = (LocationManager) this  
.getSystemService(Context.LOCATION_SERVICE);  
if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {  
Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT).show();  
return;  
}  
LocationManager alm = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT).show();
return;
}设置开启GPRS页面 
[html] 
Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();  
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);  
startActivityForResult(intent, 0); // 此为设置完成后返回到获取界面  
Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivityForResult(intent, 0); // 此为设置完成后返回到获取界面
设置省电模式,获得最好的定位方式 
[html] 
ocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
gprs_view = (TextView) findViewById(R.id.gprs_view);  
Criteria criteria = new Criteria();  
// 获得最好的定位效果  
criteria.setAccuracy(Criteria.ACCURACY_COARSE);  
criteria.setAltitudeRequired(false);  
criteria.setBearingRequired(false);  
criteria.setCostAllowed(false);  
// 使用省电模式  
criteria.setPowerRequirement(Criteria.POWER_LOW);  
// 获得当前的位置提供者  
provider = locationManager.getBestProvider(criteria, false);  
ser.append(provider);  
locationManager.requestLocationUpdates(provider, 2000, 10, this);  
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
gprs_view = (TextView) findViewById(R.id.gprs_view);
Criteria criteria = new Criteria();
// 获得最好的定位效果
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(false);
// 使用省电模式
criteria.setPowerRequirement(Criteria.POWER_LOW);
// 获得当前的位置提供者
provider = locationManager.getBestProvider(criteria, false);
ser.append(provider);
locationManager.requestLocationUpdates(provider, 2000, 10, this);
获得上次location对象 
[html] 
// 使用网络定位,获得上次定位的location对象  
if (location == null) {  
location = locationManager  
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);  
provider = LocationManager.NETWORK_PROVIDER;  
}  
// 使用网络定位,获得上次定位的location对象
if (location == null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
provider = LocationManager.NETWORK_PROVIDER;
}
然后定位 
[html] 
String latLongString;  
if (location != null) {  
double lat = location.getLatitude();  
double lng = location.getLongitude();  
latLongString = "纬度:" + lat + "\n经度:" + lng;  
Geocoder gc = new Geocoder(context);  
List<Address> addresses = null;  
try {  
addresses = gc.getFromLocation(location.getLatitude(),  
location.getLongitude(), 1);  
} catch (IOException e) {  
// TODO Auto-generated catch block  
e.printStackTrace();  
}  
ser.append("\n" + addresses.get(0).getCountryName());  
} else {  
latLongString = "无法获取地理信息";  
}  
ser.append("\n" + "您当前的位置是:\n" + latLongString);  
String latLongString;
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "纬度:" + lat + "\n经度:" + lng;
Geocoder gc = new Geocoder(context);
List<Address> addresses = null;
try {
addresses = gc.getFromLocation(location.getLatitude(),
location.getLongitude(), 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ser.append("\n" + addresses.get(0).getCountryName());
} else {
latLongString = "无法获取地理信息";
}
ser.append("\n" + "您当前的位置是:\n" + latLongString);
实现LocationListener接口,并在onLocationChanged和onProviderDisabled方法中实现updateWithNewLocation方法 
以期待在未获得location对象时,不断获取直到取到为止
[html] 
private void updateWithNewLocation(Location location) {  
// TODO Auto-generated method stub  
if (location == null) {  
locationManager.requestLocationUpdates(provider, 2000, (float) 0.1,  
this);  
}  
}  
private void updateWithNewLocation(Location location) {
// TODO Auto-generated method stub
if (location == null) {
locationManager.requestLocationUpdates(provider, 2000, (float) 0.1,
this);www.2cto.com
}
}
以上是我弄到的关于用google开发服务的资料,实际上次定位的位置很难得到,实现定位,比较困难,也许是笔者使用的是水货,刷过机的原因吧。 
下面是百度的定位,可以说都能实现吧
首先请大家看效果图,是实现了的!PS:朝鲜金胖子,看到我的经纬度乱来啊!
百度的定位相对来说要简单的多,为什么呢,因为它只有两三个方法,一般国内的手机GPS功能有被“阉割”的可能,所以一般GPS定位取不到位置,通用的还是GPRS网络定位功能。
如图,导入项目所需包
然后在manifest.xml中加入权限,以及定义Service
[html] 
<SPAN style="FONT-SIZE: 18px">    <application  
android:name="com.baidu.locSDK.test.Location"  
android:icon="@drawable/icon"  
android:label="@string/app_name" >  
<activity  
android:name="mainActivity"  
android:configChanges="orientation|keyboardHidden"  
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>  
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
<uses-permission android:name="android.permission.CHANGE_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.INTERNET" />  
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />  
<uses-permission android:name="android.permission.READ_LOGS" />  
<uses-permission android:name="android.permission.VIBRATE" /></SPAN>  
<application
android:name="com.baidu.locSDK.test.Location"
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity
android:name="mainActivity"
android:configChanges="orientation|keyboardHidden"
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>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_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.INTERNET" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.VIBRATE" />
主要代码如下,但要先打开网络 
[html] 
mStartBtn.setOnClickListener(new OnClickListener() {  
@Override  
public void onClick(View v) {  
if (!mIsStart) {  
setLocationOption();  
mLocClient.start();  
mStartBtn.setText("开始");  
mIsStart = true;  
} else {  
mLocClient.stop();  
mIsStart = false;  
mStartBtn.setText("结束");  
}  
Log.d("locSDK_Demo1",  
"... mStartBtn onClick... pid=" + Process.myPid()  
+ " count=" + count++);  
}  
});  
mStartBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!mIsStart) {
setLocationOption();
mLocClient.start();
mStartBtn.setText("开始");
mIsStart = true;
} else {
mLocClient.stop();
mIsStart = false;
mStartBtn.setText("结束");
}
Log.d("locSDK_Demo1",
"... mStartBtn onClick... pid=" + Process.myPid()
+ " count=" + count++);
}
});
[html] 
private void setLocationOption() {  
LocationClientOption option = new LocationClientOption();  
option.setOpenGps(mGpsCheck.isChecked()); // gps  
option.setCoorType(mCoorEdit.getText().toString());  
option.setAddrType(mAddrEdit.getText().toString());  
option.setScanSpan(Integer.parseInt(mSpanEdit.getText().toString()));  
mLocClient.setLocOption(option);  
}  
private void setLocationOption() {
LocationClientOption option = new LocationClientOption();
option.setOpenGps(mGpsCheck.isChecked()); // gps
option.setCoorType(mCoorEdit.getText().toString());
option.setAddrType(mAddrEdit.getText().toString());
option.setScanSpan(Integer.parseInt(mSpanEdit.getText().toString()));
mLocClient.setLocOption(option);
}
最终展示出来 
[html] 
public void logMsg(String str) {  
try {  
mData = str;  
if ( mTv != null )  
mTv.setText(mData);  
} catch (Exception e) {  
e.printStackTrace();  
}  
}  
public class MyLocationListenner implements BDLocationListener {  
@Override  
public 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());  
}  
}  
public void logMsg(String str) {
try {
mData = str;
if ( mTv != null )
mTv.setText(mData);
} catch (Exception e) {
e.printStackTrace();
}
}
public class MyLocationListenner implements BDLocationListener {
@Override
public 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());
}
}

Android中级第五讲--GPRS定位的实现相关推荐

  1. Android中级篇之百度地图SDK v3.5.0-一步一步带你仿各大主流APP地图定位移动选址功能

    from: http://blog.csdn.net/y1scp/article/details/49095729 定位+移动选址 百学须先立志-学前须知: 我们经常在各大主流APP上要求被写上地址, ...

  2. Android中GPRS定位的实现

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

  3. [android] 百度地图开发 (三).定位当前位置及getLastKnownLocation获取location总为空问题

           前一篇百度地图开发讲述"(二).定位城市位置和城市POI搜索",主要通过监听对象MKSearchListener类实现城市兴趣点POI(Point of Intere ...

  4. (五)GPRS定位的实现

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

  5. android室内定位+3d,基于Android平台的手机室内定位及导航的设计与实现

    摘要: 随着无线通信网络技术的发展,智能手机逐渐融入到人们的日常生活中.它不仅满足当今人们对于通话短信等基本功能的需求,而且还满足人们对于娱乐.上网.社交等功能的需求.导航功能是现在人们常用的一个功能 ...

  6. Android NDK开发Crash错误定位

    在Android开发中,程序Crash分三种情况:未捕获的异常.ANR(Application Not Responding)和闪退(NDK引发错误).其中未捕获的异常根据logcat打印的堆栈信息很 ...

  7. Android百度地图之位置定位和附近查找代码简单实现 (上)

    很长时间没有做Android相关知识了,闲暇之余再弄了弄最新的百度地图API,主要是进行百度地图附近餐馆查找功能来练练手,同时熟悉下最新的API教程.文章比较基础,也希望对你有所帮助~参考前文:   ...

  8. Android开发中虚拟位置定位、应用双开、IP代理检测

    Android开发中虚拟位置定位.应用双开.IP代理检测 1.虚拟位置定位.应用双开原理 目前市面上的多开App的原理类似,都是以新进程运行被多开的App,并hook各类系统函数,使被多开的App认为 ...

  9. android native 代码内存泄露 定位方案

    android native 代码内存泄露 定位方案 java代码的内存定位,暂时我们先不关注.此篇文章,主要围绕c c++代码的内存泄露. ** *欢迎留言,交流您所使用的内存泄露定位方案.*c   ...

最新文章

  1. 物联网云平台-贝壳物联入门详细使用方法
  2. SAP里面的ATP的定义
  3. 导出SAP表结构到EXCEl
  4. 使用tensoflow serving来部署模型推理节点
  5. 【IT资讯】TIOBE 7月编程语言排行榜显示,这些编程语言更吃香
  6. 毫米波雷达与激光雷达的初探
  7. openfeign 负载均衡调用服务
  8. linux下u盘病毒msdos,浅谈U盘病毒——MS-DOS.com 以及做最便民的杀毒软件
  9. cannot be cast to org.springframework.web.method.HandlerMethod 统一异常处理发生异常。
  10. 数据库的字段属性(重点)
  11. python与excel-python3与Excel的完美结合
  12. Android APK代码混淆与资源混淆详解,你确定不看?
  13. 计算机组成原理(第3版)唐朔飞著 知识点总结 第四章存储器
  14. 网络安全等级保护的过程
  15. DAY02 pat乙级刷题(c++代码)
  16. 万向区块链“汽车供应链物流服务平台”获评“2018金融区块链创新应用优秀案例”...
  17. java的逻辑常量_在Java语言中,逻辑常量只有true和(__)两个值;
  18. 电子作业票如何实现特殊作业全过程信息化管理
  19. 自考路之大渡考场路远寒
  20. 亲,你心念念的Axure9.0来了,请查收~ 内附Axure安装包+汉化包+授权码获取方式

热门文章

  1. 2022-2028年中国肛肠医疗器械(吻合器与套扎器)行业市场调研报告
  2. JAVA遍历list四种方法及其效率比较
  3. 视频删除怎么恢复找回,刚删除的视频怎么找回来
  4. IBM 报修型号/序列号 8408/2111E1V;更换备件 FRU号 00E7689 描述 VRM
  5. 整理你房间的Top10项资源和灵感。。。
  6. Python每日一练——数据存储第六关:操作MySQL数据库
  7. 如何使linux终端还原,大师应对win10系统14316开启Linux Bash命令行的还原办法
  8. 深入分析——Linux DMA Fence
  9. OBS预览窗口的设置
  10. 2021年场(厂)内专用机动车辆安全管理新版试题及场(厂)内专用机动车辆安全管理模拟考试题