• 刚学会百度地图最新版的sdk,总结一个简单的demo


    <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote" 
        android:permission="android.permission.BAIDU_LOCATION_SERVICE">
        <intent-filter>
            <action android:name="com.baidu.location.service_v2.4"></action>
        </intent-filter>
    </service>

    以上是版本区别部分。

    <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>

    以上是权限部分

    然后就是demo的java部分

    package com.xmb.baiduloctest2;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.support.v4.app.NavUtils;
    import com.baidu.location.BDLocation;
    import com.baidu.location.BDLocationListener;
    import com.baidu.location.LocationClient;
    import com.baidu.location.LocationClientOption;
    public class MainActivity extends Activity {
        public LocationClient mLocationClient = null;
        public BDLocationListener myListener = new MyLocationListenner();
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mLocationClient = new LocationClient(this);     //声明LocationClient类
            
            LocationClientOption option = new LocationClientOption();
            option.setOpenGps(true);
            option.setAddrType("detail");
            option.setCoorType("bd09ll");
            option.setScanSpan(5000);
            option.disableCache(true);//禁止启用缓存定位
            option.setPoiNumber(5); //最多返回POI个数 
            option.setPoiDistance(1000); //poi查询距离  
            option.setPoiExtraInfo(true); //是否需要POI的电话和地址等详细信息  
            mLocationClient.setLocOption(option);
            
            mLocationClient.registerLocationListener( myListener );    //注册监听函数
            mLocationClient.start();
            
        }
        
        @Override
           public void onDestroy() {
            mLocationClient.stop();
               super.onDestroy();
           }
    
        /**
         * 监听函数,又新位置的时候,格式化成字符串,输出到屏幕中
         */
        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("\n省:");
                    sb.append(location.getProvince());
                    sb.append("\n市:");
                    sb.append(location.getCity());
                    sb.append("\n区/县:");
                    sb.append(location.getDistrict());
                    sb.append("\naddr : ");
                    sb.append(location.getAddrStr());
                }
                sb.append("\nsdk version : ");
                sb.append(mLocationClient.getVersion());
                sb.append("\nisCellChangeFlag : ");
                sb.append(location.isCellChangeFlag());
                Log.i("out","onReceiveLocation="+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");
                }
                Log.i("out","onReceivePoi="+sb.toString());
            }
        }
    }
  • 相关阅读:
    linux系统防火墙对访问服务器的影响
    Android 样式的开发(转)
    Android开发学习总结(三)——appcompat_v7项目说明
    Android开发:碎片Fragment完全解析fragment_main.xml/activity_main.xml
    BootStrap 常用控件总结
    mybatis自定义代码生成器(Generator)——自动生成model&dao代码
    MySql的下载和安装(解压版)
    jquery mobile 表单提交 图片/文件 上传
    java读取.properties配置文件的几种方法
    AngularJS------认识AngularJS
  • 原文地址:https://www.cnblogs.com/xmb7/p/2997839.html
Copyright © 2020-2023  润新知