关闭

简易百度地图定位地址

47人阅读 评论(0) 收藏 举报
<code class="hljs xml has-numbering">1>导入文件夹和jar包

2>配置
appli下面 <span class="hljs-tag"><<span class="hljs-title">meta-data
</span>            <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"com.baidu.lbsapi.API_KEY"</span>
            <span class="hljs-attribute">android:value</span>=<span class="hljs-value">"BEMeihwMKbX9T5RMB54eOGaflCiPILhU申请的key"</span> /></span>

        <span class="hljs-tag"><<span class="hljs-title">service
</span>            <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"com.baidu.location.f"</span>
            <span class="hljs-attribute">android:enabled</span>=<span class="hljs-value">"true"</span>
            <span class="hljs-attribute">android:process</span>=<span class="hljs-value">":remote"</span> ></span>
        <span class="hljs-tag"></<span class="hljs-title">service</span>>

</span> 3>//权限
 <span class="hljs-tag"><<span class="hljs-title">uses-permission</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"android.permission.ACCESS_COARSE_LOCATION"</span> ></span>
    <span class="hljs-tag"></<span class="hljs-title">uses-permission</span>></span>
    <span class="hljs-comment"><!-- 这个权限用于访问GPS定位 --></span>
    <span class="hljs-tag"><<span class="hljs-title">uses-permission</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"android.permission.ACCESS_FINE_LOCATION"</span> ></span>
    <span class="hljs-tag"></<span class="hljs-title">uses-permission</span>></span>
    <span class="hljs-comment"><!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位 --></span>
    <span class="hljs-tag"><<span class="hljs-title">uses-permission</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"android.permission.ACCESS_WIFI_STATE"</span> ></span>
    <span class="hljs-tag"></<span class="hljs-title">uses-permission</span>></span>
    <span class="hljs-comment"><!-- 获取运营商信息,用于支持提供运营商信息相关的接口 --></span>
    <span class="hljs-tag"><<span class="hljs-title">uses-permission</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"android.permission.ACCESS_NETWORK_STATE"</span> ></span>
    <span class="hljs-tag"></<span class="hljs-title">uses-permission</span>></span>
    <span class="hljs-comment"><!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位 --></span>
    <span class="hljs-tag"><<span class="hljs-title">uses-permission</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"android.permission.CHANGE_WIFI_STATE"</span> ></span>
    <span class="hljs-tag"></<span class="hljs-title">uses-permission</span>></span>
    <span class="hljs-comment"><!-- 用于读取手机当前的状态 --></span>
    <span class="hljs-tag"><<span class="hljs-title">uses-permission</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"android.permission.READ_PHONE_STATE"</span> ></span>
    <span class="hljs-tag"></<span class="hljs-title">uses-permission</span>></span>
    <span class="hljs-comment"><!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据 --></span>
    <span class="hljs-tag"><<span class="hljs-title">uses-permission</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"android.permission.WRITE_EXTERNAL_STORAGE"</span> ></span>
    <span class="hljs-tag"></<span class="hljs-title">uses-permission</span>></span>
    <span class="hljs-comment"><!-- 访问网络,网络定位需要上网 --></span>
    <span class="hljs-tag"><<span class="hljs-title">uses-permission</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"android.permission.INTERNET"</span> /></span>
    <span class="hljs-comment"><!-- SD卡读取权限,用户写入离线定位数据 --></span>
    <span class="hljs-tag"><<span class="hljs-title">uses-permission</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"android.permission.MOUNT_UNMOUNT_FILESYSTEMS"</span> ></span>
    <span class="hljs-tag"></<span class="hljs-title">uses-permission</span>></span>
//代码块
4>public class MainActivity extends Activity implements BDLocationListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


         //实例化定位类
        LocationClient client=new LocationClient(this);
        //实例化定位参数类并设置参数
        LocationClientOption clientOption=new LocationClientOption();
        clientOption.setIsNeedAddress(true);
        client.setLocOption(clientOption);
        //设置监听
        client.registerLocationListener(this);
        
        client.start();</code><pre name="code" class="prettyprint"><code class="hljs xml has-numbering">//开始定位</code>
<code class="hljs xml has-numbering">
</code>
} @Override public void onReceiveLocation(BDLocation arg0) { // TODO Auto-generated method stub TextView tv=(TextView) findViewById(R.id.main_tv); String addr=arg0.getAddrStr(); tv.setText(addr); }}

   
1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:484次
    • 积分:31
    • 等级:
    • 排名:千里之外
    • 原创:2篇
    • 转载:2篇
    • 译文:0篇
    • 评论:1条
    文章存档
    最新评论