Android 中的反向地理编码
在下面的代码中,我收到以下异常
没有可用的服务
public class ds extends Activity {
LocationManager locationManager;
double lati,longi;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String location_context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(location_context);
testProviders();
}
public void testProviders() {
TextView tv = (TextView)findViewById(R.id.myTextView);
StringBuilder sb = new StringBuilder("Enabled Providers:");
List<String> providers = locationManager.getProviders(true);
for (String provider : providers)
{
locationManager.requestLocationUpdates(provider, 1000, 0,new LocationListener()
{
public void onLocationChanged(Location location) {}
public void onProviderDisabled(String provider){}
public void onProviderEnabled(String provider){}
public void onStatusChanged(String provider, int status,
Bundle extras){}
});
sb.append("\n").append(provider).append(":");
Location location = locationManager.getLastKnownLocation(provider);
if (location != null)
{
double lat = location.getLatitude();
double lng = location.getLongitude();
sb.append(lat).append(",").append(lng);
lati=lat;
longi=lng;
Geocoder gcd = new Geocoder(ds.this, Locale.getDefault());
List<Address> addresses;
try {
addresses = gcd.getFromLocation(lati, longi, 1);
if (addresses.size() > 0)
} catch (IOException e) {
Toast.makeText(ds.this, "hi exception", 5000).show();
}
}
else {
sb.append("No Location");
}
}
tv.setText(sb);
}
}
In the code below, I am getting the following exception
NO SERVICE AVAIALBLE
public class ds extends Activity {
LocationManager locationManager;
double lati,longi;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String location_context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(location_context);
testProviders();
}
public void testProviders() {
TextView tv = (TextView)findViewById(R.id.myTextView);
StringBuilder sb = new StringBuilder("Enabled Providers:");
List<String> providers = locationManager.getProviders(true);
for (String provider : providers)
{
locationManager.requestLocationUpdates(provider, 1000, 0,new LocationListener()
{
public void onLocationChanged(Location location) {}
public void onProviderDisabled(String provider){}
public void onProviderEnabled(String provider){}
public void onStatusChanged(String provider, int status,
Bundle extras){}
});
sb.append("\n").append(provider).append(":");
Location location = locationManager.getLastKnownLocation(provider);
if (location != null)
{
double lat = location.getLatitude();
double lng = location.getLongitude();
sb.append(lat).append(",").append(lng);
lati=lat;
longi=lng;
Geocoder gcd = new Geocoder(ds.this, Locale.getDefault());
List<Address> addresses;
try {
addresses = gcd.getFromLocation(lati, longi, 1);
if (addresses.size() > 0)
} catch (IOException e) {
Toast.makeText(ds.this, "hi exception", 5000).show();
}
}
else {
sb.append("No Location");
}
}
tv.setText(sb);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
反向地理编码代码,您可以根据您的需求传递经纬度......
Code for Reverse Geocoding , you can pass the lattitude and longitude according to your requirement......