#####1、获取经度纬度高度速度等
#####2、xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout"
tools:context="com.example.albert.myapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginTop="80dp"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/text_id"
android:gravity="center"
android:text="维度"
android:textSize="20dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/bt_setting"
android:layout_marginTop="250dp"
android:layout_gravity="center"
android:orientation="vertical">
<Button
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:text="定位设置"
android:textSize="20dp"
android:onClick="setup"/>
</LinearLayout>
</RelativeLayout>
#####3、权限添加
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.albert.myapplication">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<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>
#####4、功能代码
package com.example.albert.myapplication;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements LocationListener {
static final int MIN_TIME = 5000;//位置更新条件:5000毫秒
static final float MIN_DIST = 5;//位置更新条件
LocationManager mgr;//定位管理器
TextView txv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txv = (TextView) findViewById(R.id.text_id);
//获取系统服务的LocationManager对象
mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
}
protected void onResume() {
super.onResume();
//获取最佳的定位提供者,true参数是找出已经开启的提供者
String best = mgr.getBestProvider(new Criteria(), true);
if (best != null) {
txv.setText("获取定位信息中```");
//
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;
}
mgr.requestLocationUpdates(best, MIN_TIME, MIN_DIST, this);
} else {
txv.setText("请确认已经开启定位功能");
}
}
protected void onPause() {
super.onPause();
mgr.removeUpdates(this); //取消注册更新事件
}
//位置更新事件
public void onLocationChanged(Location location) {
String str = "定位提供者:" + location.getProvider();
str += String.format("\n纬度:%.5f\n经度:%.5f\n高度:%.2f米\n速度:%.2f m/s",
location.getLatitude(),//纬度
location.getLongitude(), //经度
location.getAltitude(),//高度
location.getSpeed());//速度
txv.setText(str);
}
//locationlistener 接口方法,定位提供者被停用
public void onProviderDisabled(String provider) {
//不处理
}
//locationlistener 接口方法,定位提供者被启用
public void onProviderEnabled(String provider) {
//不处理
}
//locationlistener 接口方法,定位提供者状态改变
public void onStatusChanged(String provider,int status, Bundle extras) {
//不处理
}
//显示手机定位设置界面
public void setup(View v){
Intent it = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(it);
}
}
这个APK好像没有作用,运行起来获取不到经纬度的值。只做参考使用。
文献参考:
android app开发入门 施威铭 编著
本人郑重声明,本博客所著文章、图片版权归权利人持有,本博只做学习交流分享所用,不做任何商业用途。访问者可將本博提供的內容或服务用于个人学习、研究或欣赏,不得用于商业使用。同時,访问者应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人的合法权利;如果用于商业用途,须征得相关权利人的书面授权。若以上文章、图片的原作者不愿意在此展示內容,请及时通知在下,將及时予以刪除。