android位置服务GPS经纬度获取

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/myFrameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.ccyumo.demo.MainActivity">

    <TextView
        android:id="@+id/gps_text"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="match_parent" />
</FrameLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
	TextView gps_text;//GPS显示控件
    LocationManager locationManager;//位置服务管理器
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gps_text = (TextView) findViewById(R.id.gps_text);
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
       
		 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER,//指定GPS为内容提供者
                1000,//间隔时间为1秒
                1,//距离为1米
                new LocationListener() {
                    @Override
                    public void onLocationChanged(Location location) {

                    }

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

                    }

                    @Override
                    public void onProviderEnabled(String provider) {

                    }

                    @Override
                    public void onProviderDisabled(String provider) {

                    }
                }
        );
        Location location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        locationUpdate(location);
    }
    //Location更新方法
    private void locationUpdate(Location location){
        //获取最新位置信息
        StringBuffer stringBuffer=new StringBuffer();//存储GPS信息
        stringBuffer.append("您的当前位置:"+"\n"+"经度:");
        stringBuffer.append(location.getLongitude());//精度信息
        stringBuffer.append("\n"+"纬度:");
        stringBuffer.append(location.getLatitude());//纬度信息
        gps_text.setText(stringBuffer.toString());

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值