本程序首先获得当前位置的经纬度,然后根据经纬度获得当前所在的城市名,经检测可以获得本人所在的汉中市。
java代码:
public class MainActivity extends AppCompatActivity {
private TextView showInfo = null,tv=null;
private LocationManager locationManager = null;
private String provider = null;
private LocationListener locationListener = null;
private List<String> list = null;
private Location loca = null;
private StringBuilder strbl = null;
private double jing, wei;
private String country="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showInfo = (TextView) findViewById(R.id.showInfo);
tv=(TextView)findViewById(R.id.tv);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
provider = LocationManager.GPS_PROVIDER;
loca=locationManager.getLastKnownLocation(provider);
if (loca != null) {
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;
}
update(loca);
}else{
Toast.makeText(MainActivity.this, "GPS定位不可用,即将尝试网络定位", Toast.LENGTH_SHORT).show();
provider=LocationManager.NETWORK_PROVIDER;
loca=locationManager.getLastKnownLocation(provider);
if(loca!=null){
update(loca);
}else{
Toast.makeText(MainActivity.this, "网络定位不可用,即将尝试下一个定位方式", Toast.LENGTH_SHORT).show();
provider=LocationManager.PASSIVE_PROVIDER;
loca=locationManager.getLastKnownLocation(provider);
if(loca!=null){
update(loca);
}else{
Toast.makeText(MainActivity.this, "当前没有可以使用的定位方式", Toast.LENGTH_SHORT).show();
}
}
}
}
private void update(Location location) {
if(location!=null) {
strbl = new StringBuilder();
strbl.append("实时位置信息:\n");
strbl.append("经度: ");
strbl.append(location.getLongitude());
strbl.append("\n纬度: ");
strbl.append(location.getLatitude());
strbl.append("\n高度: ");
strbl.append(location.getAltitude());
strbl.append("\n速度: ");
strbl.append(location.getSpeed());
strbl.append("\n方向: ");
strbl.append(location.getBearing());
showInfo.setText(strbl.toString());
//获得经纬度
jing = location.getLongitude();
wei = location.getLatitude();
Geocoder geocoder=new Geocoder(this);
List<Address> list=null;
try {
list= geocoder.getFromLocation(wei,jing,1);
} catch (IOException e) {
e.printStackTrace();
}
if(list!=null && list.size()>0){
for (int i=0;i<list.size();i++) {
if (list.get(i) != null) {
Address address = list.get(i);
country += address.getCountryName() + ";" + address.getLocality();
}
}
}
System.out.println("获得的城市名"+country);
tv.setText(country);
}else{
showInfo.setText("传递的值为空,无法获取位置信息");
}
}
}
布局代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="zyf.com.locationinfo.MainActivity">
<TextView
android:id="@+id/showInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="经纬度"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv"
android:hint="城市名"
/>
</LinearLayout>
问题探讨:
在获取当前的经纬度的时候,使用了昨天的代码,发现程序并不能根据设置的时间间隔来打印所在的经纬度!尚且不明原因!如有网友知道,求教!