• 经度,纬度,获取


    权限:<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION">

    布局

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
        android:layout_width="match_parent" android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.my_duomeiti.MainActivity">

        <TextView
            android:id="@+id/tv1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            />
        <TextView
            android:id="@+id/tv2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv1"
            />
    </RelativeLayout>

    package com.example.my_duomeiti;

    import android.Manifest;
    import android.content.Context;
    import android.content.pm.PackageManager;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.support.v4.app.ActivityCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.TextView;

    public class MainActivity extends AppCompatActivity {
        private LocationManager locationManager;
        private TextView tv1;
        private TextView tv2;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            initView();
            initLinter();


        }

        private void initLinter() {
            LocationListener k = new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    //纬度
                    double latitude = location.getLatitude();
                    //经度
                    double longitude = location.getLongitude();
                    tv1.setText(String.valueOf(latitude));
                    tv2.setText(String.valueOf(longitude));
                }

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

                }

                @Override
                public void onProviderEnabled(String provider) {

                }

                @Override
                public void onProviderDisabled(String provider) {

                }
            };
            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, 0, 0, k);
        }

        private void initView() {
            tv1 = (TextView) findViewById(R.id.tv1);
            tv2 = (TextView) findViewById(R.id.tv2);
        }
    }

  • 相关阅读:
    linux基础操作
    提交form表单---修改密码 ajax、jQuery
    jQuery ajax() 方法
    response.getWriter()和jsp中out对象的区别
    get与post请求的区别
    Idea导入eclipse web项目404问题(webcontent)
    Mysql 获取当月和上个月第一天和最后一天的解决方案及其它日期
    Debian10环境基本配置
    修改sudoers文件为用户增加sudo权限
    Debian10 添加清华 软件源
  • 原文地址:https://www.cnblogs.com/98k98k/p/7822494.html
Copyright © 2020-2023  润新知