使用Android自带的LocationManager和Location获取位置的时候,经常会有获取的location为null的情况,并且操作起来也不是很方便,在这个Demo里我使用了百度地图API中的定位SDK,可以一次性获取当前位置经纬度以及详细地址信息,还可以获取周边POI信息,同时可以设定位置通知点,当到达某一位置时,发出通知信息等方式来告知用户。jar包下载以及官方文档请参照:百度定位SDK,前提是需要注册百度开发者账号。
下面来看看定位的基本原理,目前,定位SDK可以通过GPS、基站、Wifi信号进行定位。基本定位流程如下图所示,当应用程序向定位SDK发起定位请求时,定位SDK会根据当前的GPS、基站、Wifi信息生成相对应的定位依据。然后定位SDK会根据定位依据来进行定位。如果需要,定位SDK会向定位服务器发送网络请求。定位服务器会根据请求的定位依据推算出对应的坐标位置,然后根据用户的定制信息,生成定位结果返回给定位SDK。
      

到官方下载jar文件后添加到工程,工程目录截图如下:

注意要把locSDK_2.4.jar添加到当天工程,右键jar文件-Build path-Add to。。。

上代码
布局文件:
[html
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
 
    <Button 
        android:id="@+id/btn_start" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"  
        android:layout_marginTop="20dp" 
        android:text="Start"/> 
 
    <TextView 
        android:id="@+id/tv_loc_info" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:textSize="18sp" /> 
 
</LinearLayout>

配置文件:
[html]
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.ericssonlabs" 
    android:versionCode="1" 
    android:versionName="1.0" > 
 
    <uses-sdk android:minSdkVersion="8" /> 
 
    <permission android:name="android.permission.BAIDU_LOCATION_SERVICE" > 
    </permission> 
 
    <uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE" > 
    </uses-permission> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" > 
    </uses-permission> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" > 
    </uses-permission> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" > 
    </uses-permission> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" > 
    </uses-permission> 
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" > 
    </uses-permission> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" > 
    </uses-permission> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" > 
    </uses-permission> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" > 
    </uses-permission> 
    <uses-permission android:name="android.permission.READ_LOGS" > 
    </uses-permission> 
 
    <application 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name" > 
        <activity 
            android:name=".LocationDemoActivity" 
            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:permission="android.permission.BAIDU_LOCATION_SERVICE" 
            android:process=":remote" > 
            <intent-filter> 
                <action android:name="com.baidu.location.service_v2.4" /> 
            </intent-filter> 
        </service> 
    </application> 
 
</manifest>

实现代码:
[java]
public class LocationDemoActivity extends Activity { 
    private TextView locationInfoTextView = null; 
    private Button startButton = null; 
    private LocationClient locationClient = null; 
    private static final int UPDATE_TIME = 5000; 
    private static int LOCATION_COUTNS = 0; 
     
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        locationInfoTextView = (TextView) this.findViewById(R.id.tv_loc_info); 
        startButton = (Button) this.findViewById(R.id.btn_start); 
         
         
        locationClient = new LocationClient(this); 
        //设置定位条件 
        LocationClientOption option = new LocationClientOption(); 
        option.setOpenGps(true);                                //是否打开GPS 
        option.setCoorType("bd09ll");                           //设置返回值的坐标类型。 
        option.setPriority(LocationClientOption.NetWorkFirst);  //设置定位优先级 
        option.setProdName("LocationDemo");                     //设置产品线名称。强烈建议您使用自定义的产品线名称,方便我们以后为您提供更高效准确的定位服务。 
        option.setScanSpan(UPDATE_TIME);                        //设置定时定位的时间间隔。单位毫秒 
        locationClient.setLocOption(option); 
         
        //注册位置监听器 
        locationClient.registerLocationListener(new BDLocationListener() { 
             
            @Override 
            public void onReceiveLocation(BDLocation location) { 
                // TODO Auto-generated method stub 
                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("\nAddress : "); 
                    sb.append(location.getAddrStr()); 
                } 
                LOCATION_COUTNS ++; 
                sb.append("\n检查位置更新次数:"); 
                sb.append(String.valueOf(LOCATION_COUTNS)); 
                locationInfoTextView.setText(sb.toString()); 
            } 
             
            @Override 
            public void onReceivePoi(BDLocation location) { 
            } 
             
        }); 
         
        startButton.setOnClickListener(new OnClickListener() { 
             
            @Override 
            public void onClick(View v) { 
                if (locationClient == null) { 
                    return; 
                } 
                if (locationClient.isStarted()) { 
                    startButton.setText("Start"); 
                    locationClient.stop(); 
                }else { 
                    startButton.setText("Stop"); 
                    locationClient.start(); 
                    /*
                     *当所设的整数值大于等于1000(ms)时,定位SDK内部使用定时定位模式。
                     *调用requestLocation( )后,每隔设定的时间,定位SDK就会进行一次定位。
                     *如果定位SDK根据定位依据发现位置没有发生变化,就不会发起网络请求,
                     *返回上一次定位的结果;如果发现位置改变,就进行网络请求进行定位,得到新的定位结果。
                     *定时定位时,调用一次requestLocation,会定时监听到定位结果。
                     */ 
                    locationClient.requestLocation(); 
                } 
            } 
        }); 
         
    } 
     
    @Override 
    protected void onDestroy() { 
        super.onDestroy(); 
        if (locationClient != null && locationClient.isStarted()) { 
            locationClient.stop(); 
            locationClient = null; 
        } 
    } 
     
     

来看看最后实现效果,点击Start后进入位置监听状态,根据设置的监听时间间隔进行定位,如果位置有变化则进行位置更新,同时显示了检测位置更新的次数,如果开启了GPS,则获取到卫星后,进行GPS定位:

设置位置提醒的功能我这里就没实现了,感兴趣的可以参考开发指南

百度定位SDK实现获取当前经纬度及位置的更多相关文章

  1. Android使用百度定位SDK 方法及错误处理

    之前我的项目中的位置定位使用的是基站方法,使用的Google提供的API,但是前天中午突然就不返回数据了,到网上搜了一下才知道,Google的接 口不提供服务了,基于时间紧迫用了百度现有的SDK,但是 ...

  2. Android使用百度定位SDK方法及错误处理

    下面事例是使用Android平台的部分代码.对于这个平台百度的开放人员已经写了完整的demo,把工程导入到eclipse中之后一般没有错误,如果报错的话,eclipse也会给出提示.一般可以通过将pr ...

  3. android中使用百度定位sdk实时的计算移动距离

    ;   //5秒刷新一次 private Handler refreshHandler = new Handler(){ //刷新界面的Handler public void handleMessag ...

  4. Android 百度定位SDK

    原文:Android 百度定位SDK 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/shui1025701856/article/details/7 ...

  5. 百度定位SDK:弥补Android基站WIFI定位缺失

    http://tech.qq.com/a/20120524/000347.htm 如今,基于位置信息的移动应用越来越多,从餐饮.购物等本地生活服务,到定向广告的匹配.移动社交网络的构建,LBS类应用的 ...

  6. 百度定位SDK 返回error code : 162 latitude : 4.9E-324 lontitude : 4.9E-324

    Android应用使用百度定位SDK 返回error code : 162 latitude : 4.9E-324 lontitude : 4.9E-324 在使用百度定位SDK时遇到一个非常郁闷的问 ...

  7. 基于百度定位SDK的定位服务的实现

    转载请标明出处:http://blog.csdn.net/android_ls/article/details/10179013 一.定位模块的需求:我们想知道使用我们应用的用户的大概位置,每隔五分钟 ...

  8. 百度定位SDK

    按照官网要求配置SHA1和包名生成ak秘钥 生成秘钥命令: keytool -list -v -keystore debug.keystore 密码:原始密码为android 添加libs文件夹并在g ...

  9. 基于百度定位及天气获取的DEMO

    demo基于百度定位APIv4.0版.新浪天气(不用查询城市代码). 需求: 1.button实现触发定位监听和天气捕获 2.两个textview 分别显示详细地址.天气. 界面很简陋,侧重功能实现. ...

随机推荐

  1. 操作系统的启动与引导问题 BIOS、UEFI、MBR、GPT

    关于ISO.WIM.GHO三者的正确理解. ISO(Isolation)文件一般以ISO为扩展名,是复制光盘上全部信息而形成的镜像文件. WIM是英文Microsoft Windows Imaging ...

  2. 【BZOJ】【3170】【TJOI2103】松鼠聚会

    切比雪夫距离+曼哈顿距离 题解:http://www.cnblogs.com/zyfzyf/p/4105456.html 其实应该先做这题再做[BZOJ][3210]花神的浇花集会的吧…… 我们发现d ...

  3. Java网络编程技术1

    1. Java网络编程常用API 1.1 InetAddress类使用示例 1.1.1根据域名查找IP地址 获取用户通过命令行方式指定的域名,然后通过InetAddress对象来获取该域名对应的IP地 ...

  4. 使用DBCA工具创建自己的数据库

    ylbtech-Oracle:使用DBCA工具创建自己的数据库  DBCA创建数据库 默认安装的Oracle数据库一般不能满足实际应用的需求,例如数据库名称.数据库块的大小等都需要修改,那么我们应该自 ...

  5. Length of Last Word leetocde java

    题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...

  6. bind原理图释

    (原文:http://blog.think-async.com/2010/04/bind-illustrated.html) 本文解释了bind 是如何工作的.为了清晰,我对图中的语法作了一些简化(例 ...

  7. 比较windows phone 的回退事件与android的回退事件

    public void onBackPressed() { finish(); } 如果要做一个页面导航的功能的话,就我而言,认为,windows phone开发比android更加人性化,更加傻瓜化 ...

  8. hdu 5326

    Work Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  9. go语言string、int、int64互相转换

    #string到int int,err:=strconv.Atoi(string) #string到int64 int64, err := strconv.ParseInt(string, 10, 6 ...

  10. Java高并发syncronized深入理解

    1.Synchronized的作用: 能够保证在同一时刻最多只有一个线程执行该段代码,以达到保证并发安全的效果. 2.地位: 1)Synchronized是java的关键字,并java的怨言原生支持: ...