安卓开发入门gps获取定位经纬度海拔速度

layout xml 文件





    

    

    



MainActivity类代码

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.provider.Settings;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;
import android.content.pm.PackageManager;

public class MainActivity extends AppCompatActivity {
    private String[] perms = {Manifest.permission.ACCESS_FINE_LOCATION};
    private final int PERMS_REQUEST_CODE = 200;
    private TextView tvLag,tvAlt,tvSpeed;
    private LocationManager lm = null;
    private Location mLocation;
    private MyLocationListner mLocationListner;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvLag = (TextView) findViewById(R.id.tvLag);
        tvAlt = (TextView) findViewById(R.id.tvAlt);
        tvSpeed = (TextView) findViewById(R.id.tvSpeed);

        lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        //Android 6.0以上版本需要临时获取权限
        if(Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP_MR1&&
                PackageManager.PERMISSION_GRANTED!=checkSelfPermission(perms[0])) {
            requestPermissions(perms,PERMS_REQUEST_CODE);
        }else{
            initLocation();
        }
    }
    private void initLocation(){
        //判断GPS是否正常启动
        if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            Toast.makeText(MainActivity.this, "请开启GPS...",Toast.LENGTH_SHORT);
            //返回开启GPS导航设置界面
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivityForResult(intent,0);
            return;
        }

        if (mLocationListner == null)
        {
            mLocationListner = new MyLocationListner();
        }

        try{
            mLocation = lm.getLastKnownLocation(lm.GPS_PROVIDER);
            updateView(mLocation);
        }catch (SecurityException se){
        }

        try{
             /**
         * 开启定位监听变化
         * 参数1,定位方式:主要有GPS_PROVIDER和NETWORK_PROVIDER,前者是GPS,后者是GPRS以及WIFI定位
         * 参数2,位置信息更新周期.单位是毫秒
         * 参数3,位置变化最小距离:当位置距离变化超过此值时,将更新位置信息
         * 参数4,监听
         * 备注:参数2和3,如果参数3不为0,则以参数3为准;参数3为0,则通过时间来定时更新;两者为0,则随时刷新
         */
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 1, mLocationListner);
        }catch (SecurityException se){
        }

    }

    private class MyLocationListner implements LocationListener
    {
        @Override
        public void onLocationChanged(Location location)
        {
            updateView(location);
        }
        @Override
        public void onProviderDisabled(String provider)
        {
            updateView(null);
        }
        @Override
        public void onProviderEnabled(String provider)
        {
            try{
                updateView(lm.getLastKnownLocation(provider));
            }catch (SecurityException e){}
        }
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras)
        {

        }
    }

    private void updateView(Location location)
    {
        if (location!=null) {
            tvLag.setText("经度:"+location.getLongitude()+"\n"+"纬度:"+location.getLatitude());
            tvAlt.setText("当前海拔:" + location.getAltitude() + "m");
            tvSpeed.setText("当前速度:" + location.getSpeed() + "m/s");
        }else{
            tvLag.setText("经度:"+"纬度:");
            tvAlt.setText("当前海拔:" );
            tvSpeed.setText("当前速度:");
        }
    }

    @Override
    public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults){
        switch(permsRequestCode){
            case PERMS_REQUEST_CODE:
                boolean storageAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
                if(storageAccepted){
                    initLocation();
                }
                break;

        }
    }


}

你可能感兴趣的:(android)