• BaiduMap


    1、注册密钥:地址http://developer.baidu.com/map/

    2、下载定位SDK,并导入SDK如图所示:

     map

    3.编写MyApplication类

    编写MyApplication类,为了使用方便我们可以将实现定位的方法封装的Application组件中

    封装下列方法

    1、  获取定位信息——requestLocationInfo()

    2、  通过广播发送位置信息——sendBroadCast()

    3、  停止定位——stopLocationClient()

    public class MyApplication extends Application{
        public LocationClient mLocationClient = null;
        public GeofenceClient mGeofenceClient;
        public MyLocationListenner myListener = new MyLocationListenner();
        public static String TAG = "MyApplication";
        private static MyApplication mInstance = null;
        @Override
        public void onCreate(){
            mInstance = this;
            mLocationClient = new LocationClient(this);
            /**
             * 项目的key,自己到官网申请 http://lbsyun.baidu.com/apiconsole/key
             */
            mLocationClient.setAK("你的应用Key");
            mLocationClient.registerLocationListener(myListener);
            mGeofenceClient = new GeofenceClient(this);
            super.onCreate();
            Log.d(TAG, "... Application onCreate... pid=" + Process.myPid());
        }
        
        public static  MyApplication  getInstance(){
            return mInstance;
        }
    
        /**
         * 停止定位
         */
        public void stopLocationClient(){
            if (mLocationClient != null && mLocationClient.isStarted()){
                mLocationClient.stop();
            } 
        }
    
        /**
         * 发起定位
         */
        public void requestLocationInfo(){
            setLocationOption();
            if (mLocationClient != null && !mLocationClient.isStarted()){
                mLocationClient.start();
            }
            if (mLocationClient != null && mLocationClient.isStarted()){
                mLocationClient.requestLocation();
            } 
        }
    
        /**
         *  设置百度地图的相关参数
         */
        private void setLocationOption(){
            LocationClientOption option = new LocationClientOption();
            option.setOpenGps(true); // 打开gps
            option.setCoorType("bd09ll"); // 设置坐标类型
            option.setServiceName("com.baidu.location.service_v2.9");
            option.setPoiExtraInfo(true);
            option.setAddrType("all");
            option.setPoiNumber(10);
            option.disableCache(true);
            mLocationClient.setLocOption(option);
        }
    
        /**
         * 监听函数,有更新位置的时候,格式化成字符串,输出到屏幕中
         */
        public class MyLocationListenner implements BDLocationListener
        {
            @Override
            public void onReceiveLocation(BDLocation location){
                if (location == null){
                    sendBroadCast("定位失败!");
                    return;
                }
                sendBroadCast(location.getAddrStr());
            }
            public void onReceivePoi(BDLocation poiLocation){
                if (poiLocation == null){
                    sendBroadCast("定位失败!");
                    return;
                }
                sendBroadCast(poiLocation.getAddrStr());
            }
        }
        
        /**
         * 得到发送广播
         * @param address
         */
        public void sendBroadCast(String address){
                    stopLocationClient();
            Intent intent = new Intent(MainActivity.LOCATION_BCR);
            intent.putExtra("address", address);
            sendBroadcast(intent);
        }
    }

    4.主程序MainActivity
    public class MainActivity extends Activity{
        public static String TAG = "LocTestDemo";
        private BroadcastReceiver broadcastReceiver;
        public static String LOCATION_BCR = "location_bcr";
        private Button locBtn;
        private TextView locInfo;
        private MyApplication application;
        @Override
        protected void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            application=(MyApplication)super.getApplication();
            initialize();
            initializeViews();
            initializeListeners();
        }
    
        private void initialize(){
            registerBroadCastReceiver();//注册广播
        }
        
        private void initializeViews()
        {
            locBtn = (Button) findViewById(R.id.location);
            locInfo = (TextView) findViewById(R.id.location_info);
        }
        
        private void initializeListeners()
        {
            locBtn.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v){
                    locInfo.setText("定位中...");
                //调用请求定位信息
                application.requestLocationInfo();
                }
            });
        }
    
        /**
         * 注册一个广播,监听定位结果,接受广播获得地址信息
         */
        private void registerBroadCastReceiver()
        {
            broadcastReceiver = new BroadcastReceiver(){
            public void onReceive(Context context, Intent intent){
                String address = intent.getStringExtra("address");
                    locInfo.setText(address);
                }
            };
            IntentFilter intentToReceiveFilter = new IntentFilter();
            intentToReceiveFilter.addAction(LOCATION_BCR);
            registerReceiver(broadcastReceiver, intentToReceiveFilter);
        }
        @Override
        protected void onDestroy(){
            super.onDestroy();
            unregisterReceiver(broadcastReceiver);
        }
    }

    五。 AndroidManifest.xml配置信息
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.jereh.baidulocation"
        android:versionCode="1"
        android:versionName="1.0" >
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="17" />
        <application
            android:name="com.jereh.baidulocation.MyApplication"
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.jereh.baidulocation.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
                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>
    
    



  • 相关阅读:
    Frameworks.Entity.Core 5 EntityValidation
    Frameworks.Entity.Core 4
    大叔 Frameworks.Entity.Core 3 Predicate
    大叔 Frameworks.Entity.Core 2 PageList
    Frameworks.Entity.Core 1
    大叔 EF 来分析 EntityFrameworks.Data.Core 2
    大叔 EF 来分析 EntityFrameworks.Data.Core 1
    基于 Lind.DDD 的 权限管理系统
    HttpApplication IHttpAsyncHandler, IHttpHandler, IComponent, IDisposable ps url System.Web.dll
    MVC 记录
  • 原文地址:https://www.cnblogs.com/yiweiaimeng/p/4968361.html
Copyright © 2020-2023  润新知