Android之获取当前位置的经纬度

Android之
点击按钮获取当前位置的经纬度
过几秒更新当前位置的经纬度

https://upload-images.jianshu.io/upload_images/13225108-85f1bc5986906856.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240

权限配置

    <!-- 地址定位权限 -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

主页面布局:

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

    <Button
        android:id="@+id/loctian_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击按钮获取当前经纬度"
        tools:ignore="MissingConstraints" />
    <TextView
        android:id="@+id/loctian_btn_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击按钮获取当前经纬度"
        tools:ignore="MissingConstraints" />

    <TextView
        android:id="@+id/loctian_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="过几秒更新当前位置的经纬度"
        tools:ignore="MissingConstraints" />

</LinearLayout>

主页面:

package com.ksxy.myapp;

import androidx.appcompat.app.AppCompatActivity;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


/**
 * Android之获取当前位置的经纬度
 */
public class LoctianActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loctian);

        // 地址定位权限
        // <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 访问地址
        // <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 访问线路位置

        LocationManager mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE); // 位置
        Location mlocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); // 网络

        Log.d("111111LoctianActivity>>", "mLocationManager>>:" + mLocationManager);
        Log.d("111111LoctianActivity>>", "mlocation>>:" + mlocation);

        // 点击按钮获取 经纬度
        Button loctianBtn = (Button) findViewById(R.id.loctian_btn);
        TextView loctianBtnTv = (TextView) findViewById(R.id.loctian_btn_tv);
        loctianBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 每隔5秒 , 100米 的距离
                Log.d("111111LoctianActivity>>", "点击按钮获取当前经纬度" + "经度:" + mlocation.getLongitude() + "纬度:" + mlocation.getLatitude());
                loctianBtnTv.setText("点击按钮获取当前经纬度:" + "经度:" + mlocation.getLongitude() + "纬度:" + mlocation.getLatitude());

            }
        });

        // 求俩个经纬度的距离
        // float[] results=new float[3];
        // Location.distanceBetween(100, 200, 200, 400, results);
        // loctianTv.setText(results[0]+"米");

        // 每隔5秒  100 米的距离,更新当前位置
        mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 100, new LocationListener() {

            //在用户禁用具有定位功能的硬件时被调用
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                // TODO Auto-generated method stub

            }

            // 位置服务可用
            // 在用户启动具有定位功能的硬件是被调用
            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub
                updateLocation(mLocationManager.getLastKnownLocation(provider));
            }

            //在提供定位功能的硬件状态改变是被调用
            @Override
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub
            }

            // 位置改变
            // 在设备的位置改变时被调用
            @Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
                updateLocation(location);
            }
        });
    }


    private void updateLocation(Location mlocation) {
        TextView loctianTv = (TextView) findViewById(R.id.loctian_tv);
        // TODO Auto-generated method stub
        if (mlocation != null) {
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("经度:" + mlocation.getLongitude());
            stringBuffer.append("纬度:" + mlocation.getLatitude());
            stringBuffer.append("海拔:" + mlocation.getAltitude());
            stringBuffer.append("速度:" + mlocation.getSpeed());
            stringBuffer.append("方向:" + mlocation.getBearing());
            loctianTv.setText("过几秒更新当前位置的经纬度:" + stringBuffer.toString());

            Log.d("111111LoctianActivity>>", "stringBuffer:" + stringBuffer.toString());
        } else {
            loctianTv.setText("正在获取位置信息");
        }
    }
}

注意:

手机定位和网络权限可能要手动打开

。。。

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Android Studio中,我们可以使用LocationManager和LocationListener来获取当前经纬度。 首先,我们需要在AndroidManifest.xml文件中添加相应的权限: <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 然后,在你的Activity中,在onCreate方法中获取LocationManager的实例,并检查是否获得了权限: LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); boolean isCoarseLocationEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); boolean isFineLocationEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 然后,我们需要创建一个LocationListener来监听位置变化事件: LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { // 当位置变化时触发该方法 double latitude = location.getLatitude(); //获取纬度 double longitude = location.getLongitude(); //获取经度 // 将经纬度保存到变量中,或者进行其他需要的处理 } public void onStatusChanged(String provider, int status, Bundle extras) {} public void onProviderEnabled(String provider) {} public void onProviderDisabled(String provider) {} }; 接下来,我们需要请求位置更新: if (isCoarseLocationEnabled) { locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); } else if (isFineLocationEnabled) { locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); } 最后,当不再需要获取位置信息时,要记得取消位置更新: locationManager.removeUpdates(locationListener); 这样,当我们点击按钮时,就能实时获得当前经纬度了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值