Android之
点击按钮获取当前位置的经纬度
过几秒更新当前位置的经纬度
权限配置
<!-- 地址定位权限 -->
<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("正在获取位置信息");
}
}
}
注意:
手机定位和网络权限可能要手动打开
。。。