• 百度定位


    百度定位DEMO 

    Location.java类

    package com.example.baidudemo;
    
    import android.app.Application; 
    import android.os.Process;
    import android.os.Vibrator;
    import android.util.Log;
    import android.widget.TextView;
    
    import com.baidu.location.BDLocation;
    import com.baidu.location.BDLocationListener;
    import com.baidu.location.BDNotifyListener;
    import com.baidu.location.GeofenceClient;
    import com.baidu.location.LocationClient;
    
    public class Location extends Application {
    
        public LocationClient mLocationClient = null;
        public GeofenceClient mGeofenceClient;
        private String mData;  
        public MyLocationListenner myListener = new MyLocationListenner();
        public TextView mTv;
        public NotifyLister mNotifyer=null;
        public Vibrator mVibrator01;
        public static String TAG = "LocTestDemo";
        
        @Override
        public void onCreate() {
        
            mLocationClient = new LocationClient( this );
            /**——————————————————————————————————————————————————————————————————
             * 这里的AK和应用签名包名绑定,如果使用在自己的工程中需要替换为自己申请的Key
             * ——————————————————————————————————————————————————————————————————
             */
            mLocationClient.setAK("0Fa49070e3d9a18fb1df084293c5a335");
            mLocationClient.registerLocationListener( myListener );
            mGeofenceClient = new GeofenceClient(this);
            //位置提醒相关代码
    //        mNotifyer = new NotifyLister();
    //        mNotifyer.SetNotifyLocation(40.047883,116.312564,3000,"gps");//4个参数代表要位置提醒的点的坐标,具体含义依次为:纬度,经度,距离范围,坐标系类型(gcj02,gps,bd09,bd09ll)
    //        mLocationClient.registerNotify(mNotifyer);
            
            super.onCreate(); 
            Log.d(TAG, "... Application onCreate... pid=" + Process.myPid());
        }
        
        /**
         * 显示请求字符串
         * @param str
         */
        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("
    error code : ");
                sb.append(location.getLocType());
                sb.append("
    latitude : ");
                sb.append(location.getLatitude());
                sb.append("
    lontitude : ");
                sb.append(location.getLongitude());
                sb.append("
    radius : ");
                sb.append(location.getRadius());
    //            if (location.getLocType() == BDLocation.TypeGpsLocation){
    //                sb.append("
    speed : ");
    //                sb.append(location.getSpeed());
    //                sb.append("
    satellite : ");
    //                sb.append(location.getSatelliteNumber());
    //            } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
                    /**
                     * 格式化显示地址信息
                     */
                    sb.append("
    省:");
                    sb.append(location.getProvince());
                    sb.append("
    市:");
                    sb.append(location.getCity());
                    sb.append("
    区/县:");
                    sb.append(location.getDistrict());
                    sb.append("
    addr : ");
                    sb.append(location.getAddrStr());
    //            }
                sb.append("
    sdk version : ");
                sb.append(mLocationClient.getVersion());
                sb.append("
    isCellChangeFlag : ");
                sb.append(location.isCellChangeFlag());
                logMsg(sb.toString());
                Log.i(TAG, 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("
    error code : "); 
                sb.append(poiLocation.getLocType());
                sb.append("
    latitude : ");
                sb.append(poiLocation.getLatitude());
                sb.append("
    lontitude : ");
                sb.append(poiLocation.getLongitude());
                sb.append("
    radius : ");
                sb.append(poiLocation.getRadius());
                if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation){
                    sb.append("
    addr : ");
                    sb.append(poiLocation.getAddrStr());
                } 
                if(poiLocation.hasPoi()){
                    sb.append("
    Poi:");
                    sb.append(poiLocation.getPoi());
                }else{                
                    sb.append("noPoi information");
                }
                logMsg(sb.toString());
            }
        }
        
        public class NotifyLister extends BDNotifyListener{
            public void onNotify(BDLocation mlocation, float distance){
                mVibrator01.vibrate(1000);
            }
        }
    }

    MainActivity 测试类

    package com.example.baidudemo;
    
    import com.baidu.location.LocationClient;
    import com.baidu.location.LocationClientOption;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    
        TextView textView1;
        private LocationClient mLocClient;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mLocClient = ((Location)getApplication()).mLocationClient;
            textView1 = (TextView)findViewById(R.id.textView2);
            ((Location)getApplication()).mTv = textView1;
            setLocationOption();
            mLocClient.start();
            mLocClient.requestLocation();
    
        }
    
        // ������ز���
        private void setLocationOption() {
            LocationClientOption option = new LocationClientOption();
    //        option.setOpenGps(true); //gps
            option.setCoorType("bd009ll"); 
            option.setServiceName("com.baidu.location.service_v2.9");//设置使用的service, 注意 该service需要在AndroidMenifest.xml中配置
            option.setPoiExtraInfo(true);
    
            option.setAddrType("all");//设置获取的地址类型
    
            option.setScanSpan(3000);//设置扫描间隔
    
            option.setPriority(LocationClientOption.NetWorkFirst); // 设置定位优先级;网络定位优先
    
            option.setPoiNumber(10);
            option.disableCache(true);
            mLocClient.setLocOption(option);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
    }

    AndroidManifests.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.baidudemo"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="18" />
    
        <application
            android:name="com.example.baidudemo.Location"
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.baidudemo.MainActivity"
                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的定义很重要 --> <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote" > <intent-filter> <action android:name="com.baidu.location.service_v2.2" > </action> </intent-filter> </service> </application> <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> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> </manifest>

    在做上面的工作之前需要导入百度定位的jar和相应的so文件。

    具体可以参考http://i.cnblogs.com/Files.aspx里面的android_baidudingweiDEMO.rar
  • 相关阅读:
    [转]JS自执行函数,匿名函数
    [转]前端实现token刷新
    [转]maven引入本地jar包的方法
    钉钉小程序后台报错"errcode":88,"sub_code":"60011","sub_msg":"没有调用该接口的权限
    中国台湾圣贤堂《天堂游记》等下载
    pt-table-checksum对比数据测试(dsns方式)
    参透世间所有困惑的终极智慧
    KQL笔记
    elasticsearch should实现or功能,设置minimum_should_match
    《王阳明心学及其当代意义》观后总结自用
  • 原文地址:https://www.cnblogs.com/wangzehuaw/p/4344004.html
Copyright © 2020-2023  润新知