Android获取手机定位坐标
一、定位授权
在AndroidManifest.xml中添加以下代码
<!--网络权限-->
<uses-permission android:name="android.permission.INTERNET"/>
<!--模糊定位权限:一般用于网络定位-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<!--精确定位权限:一般用于gps定位-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
完整的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.itcast.g">
<!--网络权限-->
<uses-permission android:name="android.permission.INTERNET"/>
<!--模糊定位权限:一般用于网络定位-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<!--精确定位权限:一般用于gps定位-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
二、在MainActivity.java中编写方法getLocation()、updateLocation()
package cn.itcast.g;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.nfc.Tag;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private TextView text;
private Button btn;
// 定义LocationManager对象
private LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
}
private void initData() {
/*文本对象*/
text = (TextView) findViewById(R.id.text);
/*按钮对象*/
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn:
//开启定位
getLocation();
break;
}
}
/* 获取定位方法 */
public void getLocation() {
//1.获取系统LocationManager服务
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);//低精度,中精度高精度获取不到location。
criteria.setAltitudeRequired(false);//不要求海拔
criteria.setBearingRequired(false);//不要求方位
criteria.setCostAllowed(true);//允许有花费
criteria.setPowerRequirement(Criteria.POWER_LOW);//低功耗
String locationProvider = locationManager.getBestProvider(criteria, true);
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Log.d(null, "onCreate: 没有权限 ");
return;
}
//2.获取GPS最近的定位信息
Location location = locationManager.getLastKnownLocation(locationProvider);
//3.将location里面的位置信息展示在edittext中
updateLocation(location);
//4.设置没两秒获取一次GPS的定位信息
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0, new LocationListener() {
@Override
public void onLocationChanged(@NonNull Location location) {
//当GPS定位信息发生改变时,更新位置
updateLocation(location);
}
@Override
public void onProviderEnabled(@NonNull String provider) {
//当GPS LocationProvider可用时,更新位置
}
@Override
public void onProviderDisabled(@NonNull String provider) {
updateLocation(null);
}
});
}
private void updateLocation(Location location) {
if (location != null) {
StringBuffer sb = new StringBuffer();
sb.append("实时的位置信息:\n经度:");
sb.append(location.getLongitude());
sb.append("\n纬度:");
sb.append(location.getLatitude());
sb.append("\n高度:");
sb.append(location.getAltitude());
sb.append("\n速度:");
sb.append(location.getSpeed());
sb.append("\n方向:");
sb.append(location.getBearing());
sb.append("\n精度:");
sb.append(location.getAccuracy());
text.setText(sb.toString());
} else {
// 如果传入的Location对象为空则清空EditText
text.setText("");
}
}
}
三、XML页面代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="28dp"
android:layout_marginTop="0dp"
android:textColor="#000"
android:text="暂无定位"
app:layout_constraintLeft_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginBottom="600dp"
android:text="开始定位"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.096"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>