【发布时间】:2014-06-26 14:02:10
【问题描述】:
我想使用 GPS 提供程序获取当前地址。我在使用 GPS 时获得零纬度 lng 值。我想要当前位置而不使用 Wifi。当我使用 GeoCoder 转换为 alt lng 值以寻址时出现异常( “无法解析来自服务器的响应”)我无法做到这一点,请帮助我
我的代码
public class MainActivity extends Activity implements LocationListener, OnClickListener{
LocationManager locationManager ;
String provider;
Button btn;
private String strAdd;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(this);
// Getting LocationManager object
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
// Creating an empty criteria object
Criteria criteria = new Criteria();
// Getting the name of the provider that meets the criteria
provider = locationManager.getBestProvider(criteria, false);
if(provider!=null && !provider.equals("")){
// Get the location from the given provider
Location location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 10, 30, this);
if(location!=null)
onLocationChanged(location);
else
Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getBaseContext(), "No Provider Found", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onLocationChanged(Location location) {
// Getting reference to TextView tv_longitude
TextView tvLongitude = (TextView)findViewById(R.id.textView1);
TextView tvAdd = (TextView)findViewById(R.id.textView2);
// Getting reference to TextView tv_latitude
//TextView tvLatitude = (TextView)findViewById(R.id.tv_latitude);
// Setting Current Longitude
double lat = location.getLatitude();
double lng = location.getLongitude();
Log.e("LATLNG", ""+lat+lng);
Geocoder geoCoder = new Geocoder(MainActivity.this, Locale.getDefault());
try{
List<Address> addresses = geoCoder.getFromLocation(lat,lng, 1);
if (addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("");
for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
strAdd = strReturnedAddress.toString();
}
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
tvLongitude.setText("LAT LNG:" + lat+lng);
tvAdd.setText("ADDRESS:" + strAdd);
// Setting Current Latitude
// tvLatitude.setText("Latitude:" + location.getLatitude() );
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
权限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COURSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
【问题讨论】:
-
你在哪里测试你的代码?在模拟器中还是在真实设备中?
-
真机测试