手机定位:获取我的位置经纬度
public class MainActivity extends AppCompatActivity {
private Button getLocation;
private TextView myLocation;
private double latitudeGps;
private double longitudeGps;
private double latitudeNet;
private double longitudeNet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(.activity_main);
initView();
setListener();
}
private void setListener() {
getLocation.setOnClickListener(new () {
@Override
public void onClick(View view) {
myLocation.setText("您现在的位置经度是:"+longitudeGps+"维度是:"+latitudeGps);
}
});
}
private void initView() {
getLocation = (Button) findViewById(.getLocation_btn);
myLocation = (TextView) findViewById(.myLocation_tv);
getMyLocation();
}
public void getMyLocation() {
//要加动态权限,清单文件也要加权限
//被拒绝情况
if (ContextCompat.checkSelfPermission(MainActivity.this, .ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_DENIED) {
//申请
ActivityCompat.requestPermissions(MainActivity.this, new String[]{.ACCESS_COARSE_LOCATION, .ACCESS_FINE_LOCATION}, 0);
initLocation();
} else {
initLocation();
}
}
//加载本地位置
private void initLocation() {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(false);
criteria.setPowerRequirement(Criteria.POWER_LOW);
//
if (ActivityCompat.checkSelfPermission(this, .ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, .ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Location locationGps = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location locationNet = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (locationGps!=null){
latitudeGps = locationGps.getLatitude();
longitudeGps = locationGps.getLongitude();
choseLocationType();
}else if (locationNet!=null){
latitudeNet = locationNet.getLatitude();
longitudeNet = locationGps.getLongitude();
choseLocationType();
}else {
Toast.makeText(MainActivity.this,"获取位置失败,请检查网络",Toast.LENGTH_SHORT).show();
}
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {}
@Override
public void onProviderEnabled(String s) {}
@Override
public void onProviderDisabled(String s) {}
};
//更新(间隔自己定义)
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,10,1000,locationListener);
}
private void choseLocationType() {
if (latitudeNet>0&&latitudeGps<=0){
latitudeGps=latitudeNet;
}else if (longitudeNet>0&&longitudeGps<=0){
longitudeGps=longitudeNet;
}
}
}