LocationDemo(Android定位)

2017-08-18  本文已影响10人  冉桓彬

代码:

public class LocationUi extends BaseUi implements View.OnClickListener, LocationListener {

    private Button mBtnStartLocation;
    private Button mBtnStopLocation;
    private TextView mTvLocation;
    private LocationManager mLocationManager;

    @Override
    protected void initView() {
        setContentView(R.layout.ui_location);
        mBtnStartLocation = (Button) findViewById(R.id.location_btnStartLocation);
        mBtnStopLocation = (Button) findViewById(R.id.location_btnStopLocation);
        mTvLocation = (TextView) findViewById(R.id.location_tvLocation);
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initListener() {
        mBtnStartLocation.setOnClickListener(this);
        mBtnStopLocation.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.location_btnStartLocation:
                openGPSSettings();
                break;
            case R.id.location_btnStopLocation:
                stopGPS();
                break;
        }

    }

    private void openGPSSettings() {
        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (mLocationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
            Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT).show();
            doWork();
        } else {
            Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
            startActivityForResult(intent, 0);  // 此为设置完成后返回到获取界面
        }
    }

    private void doWork() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            mTvLocation.setText("权限异常1");
            return;
        }
        Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 8, this);
        updata(location);
    }

    private void updata(Location location) {
        if (location != null) {
            String latitude = formatWGS84ToDegree(location.getLatitude());
            String longitude = formatWGS84ToDegree(location.getLongitude());
            StringBuilder sb = new StringBuilder();
            sb.append("实时的位置信息:\n");
            sb.append("经度:");
            sb.append(longitude);
            sb.append("\n纬度:");
            sb.append(latitude);
            sb.append("\b高度:");
            sb.append(location.getAltitude());
            sb.append("\n速度:");
            sb.append(location.getSpeed());
            sb.append("\n方向:");
            sb.append(location.getBearing());
            mTvLocation.setText(sb.toString());
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        // 当GPS定位信息发生改变时,更新位置
        updata(location);
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {
        // 当GPS Location Provider可用时,更新位置
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            mTvLocation.setText("权限异常2");
            return;
        }
        updata(mLocationManager.getLastKnownLocation(provider));
    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    private void stopGPS() {
        if (mLocationManager != null && mLocationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
            mLocationManager.removeUpdates(this);
        }
    }

    /**
     * 将WGS84坐标由十进制转换为度分秒格式
     *
     * @param value
     * @return 格式为114°12′20.12″
     */
    public String formatWGS84ToDegree(double value) {
        String result = "";
        int degree, minute;
        double second;
        degree = (int) value;
        minute = (int) ((value - degree) * 60);
        second = ((value - degree) * 60 - minute) * 60;
        // “秒”部分小数位数为三位
        result = String.format(Locale.CHINA, "%s°%s′%.3f″", degree, minute, second);
        return result;
    }
}

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <Button
        android:id="@+id/location_btnStartLocation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="开启定位"
        android:textAllCaps="false"/>
    <Button
        android:id="@+id/location_btnStopLocation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="关闭定位"
        android:textAllCaps="false"/>
    <TextView
        android:id="@+id/location_tvLocation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textColor="#000000"
        android:textSize="20sp"/>
</LinearLayout>

权限:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
上一篇下一篇

猜你喜欢

热点阅读