android中的反向地理编码
在下面的代码中,我得到以下异常 没有可用的服务android中的反向地理编码,android,geocoding,reverse-geocoding,Android,Geocoding,Reverse Geocoding,在下面的代码中,我得到以下异常 没有可用的服务 公共类ds扩展活动{ 地点经理地点经理; 双纬、长纬; @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); 字符串location\u context=context.location\u服务; locationManager=(locationManager)getSystemSe
公共类ds扩展活动{
地点经理地点经理;
双纬、长纬;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
字符串location\u context=context.location\u服务;
locationManager=(locationManager)getSystemService(位置上下文);
testProviders();
}
公共void testProviders(){
TextView tv=(TextView)findViewById(R.id.myTextView);
StringBuilder sb=新的StringBuilder(“已启用的提供程序:”);
List providers=locationManager.getProviders(true);
for(字符串提供程序:提供程序)
{
locationManager.RequestLocationUpdate(提供程序,1000,0,新LocationListener()
{
public void onLocationChanged(位置){}
公共无效onProviderDisabled(字符串提供程序){}
公共无效onProviderEnabled(字符串提供程序){}
public void onStatusChanged(字符串提供程序,int状态,
Bundle extras){}
});
sb.append(“\n”)。append(提供者)。append(“:”);
Location Location=locationManager.getLastKnownLocation(提供者);
如果(位置!=null)
{
双纬度=location.getLatitude();
double lng=location.getLongitude();
sb.追加(lat).追加(“,”).追加(lng);
纬度=纬度;
longi=液化天然气;
Geocoder gcd=新的地理编码器(ds.this,Locale.getDefault());
列出地址;
试一试{
地址=gcd.getFromLocation(纬度、经度、1);
如果(地址.size()>0)
}捕获(IOE异常){
Toast.makeText(ds.this,“hi exception”,5000.show();
}
}
否则{
某人附加(“无位置”);
}
}
tv.setText(sb);
}
}
请检查我对以下问题的回答
这是Android emulator API 8级代码的一个问题,用于反向地理编码,您可以根据需要传递纬度和经度
public class MainActivity extends FragmentActivity {
static final LatLng DELHI = new LatLng(39.6985207, -104.8954315);
GoogleMap map;
Button btn_geo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activitymain);
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
btn_geo=(Button)findViewById(R.id.btn_getAddress);
map.addMarker(new MarkerOptions().position(DELHI).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(DELHI, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
btn_geo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
if(myLocation.isPresent())
{
List<Address> addresses=null ;
addresses = myLocation.getFromLocation(39.6985207, -104.8954315, 1);
System.out.println(".................."+addresses);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0)
{
Address address = addresses.get(0);
sb.append(address.getAddressLine(0)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
Toast.makeText(getApplicationContext(), sb,Toast.LENGTH_LONG).show();
}
}
else
Toast.makeText(getApplicationContext(), "Not present",Toast.LENGTH_SHORT).show();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
public类MainActivity扩展了FragmentActivity{
静态最终车床德里=新车床(39.6985207,-104.8954315);
谷歌地图;
按钮btn_geo;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(窗口。功能\u无\u标题);
setContentView(R.layout.activitymain);
map=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
btn_geo=(按钮)findviewbyd(R.id.btn_getAddress);
addMarker(newmarkeroptions().position(德里).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher));
//将相机立即移动到汉堡,变焦为15。
moveCamera地图(CameraUpdateFactory.newLatLngZoom(德里,15));
//放大,设置摄影机动画。
animateCamera(CameraUpdateFactory.zoomTo(10),2000年,空);
btn_geo.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Geocoder myLocation=新的地理编码器(getApplicationContext(),Locale.getDefault());
试一试{
if(myLocation.isPresent())
{
列表地址=空;
addresses=myLocation.getFromLocation(39.6985207,-104.8954315,1);
系统输出打印项次(……“+地址);
StringBuilder sb=新的StringBuilder();
如果(地址.size()>0)
{
地址=地址。获取(0);
sb.append(address.getAddressLine(0)).append(“\n”);
sb.append(address.getLocality()).append(“\n”);
sb.append(address.getPostalCode()).append(“\n”);
sb.append(address.getCountryName());
Toast.makeText(getApplicationContext(),sb,Toast.LENGTH_LONG.show();
}
}
其他的
Toast.makeText(getApplicationContext(),“不存在”,Toast.LENGTH\u SHORT.show();
}
捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
});
}
}
要从Geocoder类获取地址,应在特定位置提供服务。只有这样它才能给你地址。要知道服务是否可用,请使用isPresent()方法。其余我张贴完整的代码…………希望它会有所帮助
public class MainActivity extends FragmentActivity {
static final LatLng DELHI = new LatLng(39.6985207, -104.8954315);
GoogleMap map;
Button btn_geo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activitymain);
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
btn_geo=(Button)findViewById(R.id.btn_getAddress);
map.addMarker(new MarkerOptions().position(DELHI).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(DELHI, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
btn_geo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
if(myLocation.isPresent())
{
List<Address> addresses=null ;
addresses = myLocation.getFromLocation(39.6985207, -104.8954315, 1);
System.out.println(".................."+addresses);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0)
{
Address address = addresses.get(0);
sb.append(address.getAddressLine(0)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
Toast.makeText(getApplicationContext(), sb,Toast.LENGTH_LONG).show();
}
}
else
Toast.makeText(getApplicationContext(), "Not present",Toast.LENGTH_SHORT).show();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}