关闭

定位

41人阅读 评论(0) 收藏 举报

MainActivity

package com.example.getcoord_text;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;

public class MainActivity extends Activity{
    private LocationClient mLocationClient;//定位SDK的核心类
    private TextView LocationResult;
    private Button startLocation;
    @Override
    protected void 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(new OnClickListener() {

            public void onClick(View v) {
                InitLocation();//初始化
                if(startLocation.getText().equals("开启定位")){
                    mLocationClient.start();
                    startLocation.setText("停止定位");
                }else{
                    mLocationClient.stop();
                    startLocation.setText("开启定位");
                }
            }
        });

    }
    @Override
    protected void onStop() {
        mLocationClient.stop();
        super.onStop();
    }

    private void InitLocation(){
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationMode.Hight_Accuracy);//设置高精度定位定位模式
        option.setCoorType("bd09ll");//设置百度经纬度坐标系格式
        option.setScanSpan(1000);//设置发起定位请求的间隔时间为1000ms
        option.setIsNeedAddress(true);//反编译获得具体位置,只有网络定位才可以
        mLocationClient.setLocOption(option);
    }
}

LocationApplication类

package com.example.getcoord_text;

import android.app.Application;
import android.util.Log;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;

public class LocationApplication extends Application {
    public LocationClient mLocationClient;//定位SDK的核心类
    public MyLocationListener mMyLocationListener;//定义监听类
    public TextView mLocationResult,logMsg;

    @Override
    public void onCreate() {
        super.onCreate();
        mLocationClient = new LocationClient(this.getApplicationContext());
        mMyLocationListener = new MyLocationListener();
        mLocationClient.registerLocationListener(mMyLocationListener);
    }

    /**
     * 实现实位回调监听
     */
    public class MyLocationListener implements BDLocationListener {

        public void onReceiveLocation(BDLocation location) {
            //Receive Location
            StringBuffer sb = new StringBuffer(256);
            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());//获得方位
            } else if (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
     */
    public void logMsg(String str) {
        try {
            if (mLocationResult != null)
                mLocationResult.setText(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

AndroidManifest.xml:

<!--?xml version="1.0" encoding="utf-8"?-->
<manifest android:versioncode="1" android:versionname="1.0" package="com.example.getcoord_text" xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-sdk android:minsdkversion="8" android:targetsdkversion="18">

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


        <service android:enabled="true" android:name="com.baidu.location.f" android:process=":remote">
            <intent-filter>

                </action>
            </intent-filter>
        </service>

        <!-- meta-data需要写在application中 -->
        <meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="sGFSyZlrvnHGr9GipH70G6Nd">


            <intent-filter>


                <category android:name="android.intent.category.LAUNCHER">
            </category></action></intent-filter>
        </activity>
    </meta-data></application>

</uses-permission></uses-permission></uses-permission></uses-permission></uses-sdk></manifest>
0
0

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