布局界面
<?xml version="1.0" encoding="utf-8"?>
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
android:textColor="#ff00ff"
android:textSize="40sp"
android:id="@+id/tv0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="您的定位信息:"
/>
android:textColor="#ff00ff"
android:textSize="40sp"
android:id="@+id/tv1"
android:text="xinxi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
MainActivity.java界面:
package com.example.gps3;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView;
LocationManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.tv1);
manager = (LocationManager) getSystemService(LOCATION_SERVICE);
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;
}
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 8, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
updateLocation(location);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
});
}
public void updateLocation(Location location){
textView.setText("");
textView.append("经度是:"+location.getLongitude());
textView.append("\n纬度是:"+location.getLatitude());
textView.append("\n高度:"+location.getAltitude());
textView.append("\n方向:"+location.getBearing());
textView.append("\n速度:"+location.getSpeed());
}
}
为什么我的第二个TextView不显示定位信息?
谢谢,第一次提问,排版不好,还请见谅!
// 获取所有可用的位置提供器
List<String> providerList = locationManager.getProviders(true);
再根据集合返回的内容判断当前手机可用的提供器 有WiFi,GPS这些,如果这些都没有手机是没法定位的https://blog.csdn.net/jiuyuefenglove/article/details/80082076
用真机获取经纬度
【1】6.0以上要获取动态获取定位权限
【2】一些手机是不能自身定位的,比如华为手机
最后把工具类列出来,方便参考
/**
* 获取位置信息的Utils
*注意:6.0 7.0 需要动态获取权限
*/
public class LocationUtils {
// 纬度
public static double latitude = 0.0;
// 经度
public static double longitude = 0.0;
/**
* 初始化位置信息
*
* @param context
*/
public static void initLocation(Context context) {
LocationManager locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Location location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
} else {
LocationListener locationListener = new LocationListener() {
// Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
// Provider被enable时触发此函数,比如GPS被打开
@Override
public void onProviderEnabled(String provider) {
}
// Provider被disable时触发此函数,比如GPS被关闭
@Override
public void onProviderDisabled(String provider) {
}
// 当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
@Override
public void onLocationChanged(Location location) {
if (location != null) {
}
}
};
locationManager
.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
1000, 0, locationListener);
Location location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude(); // 经度
longitude = location.getLongitude(); // 纬度
}
}
}
}