百度定位获取当前信息

1.获取百度密钥
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.TextView;
 
importcom.baidu.location.LocationClient;
importcom.baidu.location.LocationClientOption;
importcom.baidu.location.LocationClientOption.LocationMode;
 
publicclass MainActivity extendsActivity{
    privateLocationClient mLocationClient;//定位SDK的核心类
    privateTextView LocationResult;
    privateButton startLocation;
    @Override
    protectedvoid onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mLocationClient = ((LocationApplication)getApplication()).mLocationClient;
         
        LocationResult = (TextView)findViewById(R.id.textView1);
         ((LocationApplication)getApplication()).mLocationResult = LocationResult;//调用LocationApplication,获得需要的信息
        startLocation = (Button)findViewById(R.id.addfence);
        startLocation.setOnClickListener(newOnClickListener() {
             
            publicvoid onClick(View v) {
                InitLocation();//初始化
                if(startLocation.getText().equals("开启定位")){
                    mLocationClient.start();
                    startLocation.setText("停止定位");
                }else{
                    mLocationClient.stop();
                    startLocation.setText("开启定位");
                }
            }
        });
         
    }
    @Override
    protectedvoid onStop() {
        mLocationClient.stop();
        super.onStop();
    }
 
    privatevoid InitLocation(){
        LocationClientOption option = newLocationClientOption();
        option.setLocationMode(LocationMode.Hight_Accuracy);//设置高精度定位定位模式
        option.setCoorType("bd09ll");//设置百度经纬度坐标系格式
        option.setScanSpan(1000);//设置发起定位请求的间隔时间为1000ms
        option.setIsNeedAddress(true);//反编译获得具体位置,只有网络定位才可以
        mLocationClient.setLocOption(option);
    }
}
2定义LocationApplication类:目的是获取信息,定位额关键就靠它了
packagecom.example.getcoord_text;
 
importandroid.app.Application;
importandroid.util.Log;
importandroid.widget.TextView;
 
importcom.baidu.location.BDLocation;
importcom.baidu.location.BDLocationListener;
importcom.baidu.location.LocationClient;
 
publicclass LocationApplication extendsApplication {
    publicLocationClient mLocationClient;//定位SDK的核心类
    publicMyLocationListener mMyLocationListener;//定义监听类
    publicTextView mLocationResult,logMsg;
     
    @Override
    publicvoid onCreate() {
        super.onCreate();
        mLocationClient = newLocationClient(this.getApplicationContext());
        mMyLocationListener = newMyLocationListener();
        mLocationClient.registerLocationListener(mMyLocationListener);
    }
 
    /**
     * 实现实位回调监听
     */
    publicclass MyLocationListener implementsBDLocationListener {
 
        publicvoid onReceiveLocation(BDLocation location) {
            //Receive Location 
            StringBuffer sb = newStringBuffer(256);
String city=location.getCity();
            sb.append("time : ");
            sb.append(location.getTime());//获得当前时间
            sb.append("\nerror code : ");
            sb.append(location.getLocType());//获得erro code得知定位现状
            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){//通过GPS定位
                sb.append("\nspeed : ");
                sb.append(location.getSpeed());//获得速度
                sb.append("\nsatellite : ");
                sb.append(location.getSatelliteNumber());
                sb.append("\ndirection : ");
                sb.append("\naddr : ");
                sb.append(location.getAddrStr());//获得当前地址
                sb.append(location.getDirection());//获得方位
            }elseif (location.getLocType() == BDLocation.TypeNetWorkLocation){//通过网络连接定位
                sb.append("\naddr : ");
                sb.append(location.getAddrStr());//获得当前地址
                //运营商信息
                sb.append("\noperationers : ");
                sb.append(location.getOperators());//获得经营商?
            }
            logMsg(sb.toString());
            Log.i("BaiduLocationApiDem", sb.toString());
        }
    }
     
    /**
     * 显示请求字符串
     * @param str
     */
    publicvoid logMsg(String str) {
        try{
            if(mLocationResult != null)
                mLocationResult.setText(str); //所有的信息
mLocationResult.setText(city); //只有城市
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
     
}
3.Mainfast.xml
 1.在application中加上一个属性name
 android:name="com.king.salebaby.Location.LocationApplication" //LocationApplication所在的包名
 2.注册service
<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.6"></action>
             </intent-filter>
</service>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值