https://github.com/westlifeChen/GPS-Demo
https://blog.csdn.net/tdstds/article/details/50517515
https://www.kancloud.cn/alex_wsc/android-wifi-nfc-gps/414153
https://www.cnblogs.com/ljf181275034/articles/3238087.html
<!-- 网络权限和GPS权限 -->
<!-- 连接互联网Internet权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GPS定位权限 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private TextView mLocationone;
private TextView mLocationtwo;
private Context context;
String latLongString;
String placename="";
List<Address> places=null;
private double longitude;
private double latitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
/*if (Build.VERSION.SDK_INT > 22) {
checkPermission();
initView();
}*/
initView();
}
private void checkPermission() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 200);
}
return;
}
private void initView() {
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.
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_PHONE_STATE}, 200);
Log.e("11111111", "已经获取到权限");
return;
}
mLocationone = findViewById(R.id.location_one);
mLocationtwo = findViewById(R.id.location_two);
//获取LocationManager
LocationManager lManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Log.e("11111111", "lManager" + lManager);
Criteria criteria = new Criteria();
Log.e("11111111", "criteria:" + criteria);
criteria.setAccuracy(Criteria.ACCURACY_FINE);//高精度
criteria.setAltitudeRequired(false);//不要求海拔
criteria.setBearingRequired(false);//不要求方位
criteria.setCostAllowed(false);//允许有花费
criteria.setPowerRequirement(Criteria.POWER_LOW);//低功耗
lManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 5, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
//经度
longitude = location.getLongitude();
//纬度
latitude = location.getLatitude();
//海拔
double altitude = location.getAltitude();
Log.e("11111111", longitude + " " + latitude);
mLocationone.setText("经度:==>" + longitude + "\n纬度:==>" + latitude);
Log.e("有GPS信息", "经度:==>" + longitude + " \n 纬度==>" + latitude);
Log.e("获取的位置11111111",location.toString()+" 1111");
Geocoder geocoder = new Geocoder(MainActivity.this,Locale.getDefault());
try {
places = geocoder.getFromLocation(latitude, longitude, 5);
Toast.makeText(MainActivity.this, places.size() + "", Toast.LENGTH_LONG).show();
Log.e("11111111"," "+places.toString());
} catch (IOException e) {
e.printStackTrace();
Log.e("11111111"," "+places.toString());
}
if (places != null && places.size() > 0) {
// placename=((Address)places.get(0)).getLocality();
Log.e("11111111",places.get(0).getLocality()+"");
//一下的信息将会具体到某条街
//其中getAddressLine(0)表示国家,getAddressLine(1)表示精确到某个区,getAddressLine(2)表示精确到具体的街
for (int i = 0; i < places.size(); i++) {
placename = places.get(0).getAddressLine(0) + ", " + System.getProperty("line.separator")
+ places.get(0).getAddressLine(1) + ", "
+ places.get(0).getAddressLine(2);
}
}
latLongString = "纬度:" + latitude + "/n经度:" + longitude;
Toast.makeText(MainActivity.this, placename, Toast.LENGTH_LONG).show();
mLocationtwo.setText("当前位置"+placename);
} else {
Log.e("没有GPS信息", "没有GPS信息");
}
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
switch (i) {
case LocationProvider.AVAILABLE:
Toast.makeText(context, "当前GPS为可用状态!", Toast.LENGTH_SHORT).show();
break;
case LocationProvider.OUT_OF_SERVICE:
Toast.makeText(context, "当前GPS不在服务内", Toast.LENGTH_SHORT).show();
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Toast.makeText(context, "当前GPS为暂停服务状态", Toast.LENGTH_SHORT).show();
break;
}
}
@Override
public void onProviderEnabled(String s) {
Toast.makeText(context, "GPS开启了", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String s) {
Toast.makeText(context, "GPS关闭了", Toast.LENGTH_SHORT).show();
}
});
}
}
<TextView
android:id="@+id/location_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"/>
<TextView
android:id="@+id/location_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"/>
网友评论