Android的地理codeR也不回城 [英] Android Geocoder not returning City

查看:134
本文介绍了Android的地理codeR也不回城的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助。即时通讯卡在使用地理codeR API让城市名。 Anthing我做错了这里。

这是工作的一件事是,如果我用这个在onLocationChanged(位置LOC)。

不过,需要我更新通过移动坐标。我只想使用从网络坐标。

任何帮助非常多AP preciated。

 公共无效DisCityName(位置LOC){
    / * ----------从坐标获得市名称------------- * /
    字符串的cityName = NULL;
    地理codeR GCD =新的地缘codeR(getBaseContext(),Locale.getDefault());
    清单<地址>地址;
    尝试{
    地址= gcd.getFromLocation(loc.getLatitude(),loc.getLongitude(),1);
    如果(addresses.size()大于0)
    的System.out.println(addresses.get(0).getLocality());
    的cityName = addresses.get(0).getLocality();
    }赶上(IOException异常五){
    e.printStackTrace();
    }    Toast.makeText(getBaseContext(),
        \\ n \\ n我Currrent城市是:+的cityName
        +\\ nLatitude:+ loc.getLatitude()
        +\\ nLongitude:+ loc.getLongitude(),
        Toast.LENGTH_SHORT).show();}


解决方案

试试这个,它会给你的位置的地址。

 公共类AndroidFromLocation延伸活动{双纬度= 37.42233;
双经度= -122.083;/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
   super.onCreate(savedInstanceState);
   的setContentView(R.layout.main);
   TextView的myLatitude =(的TextView)findViewById(R.id.mylatitude);
   TextView的myLongitude =(的TextView)findViewById(R.id.mylongitude);
   TextView的myAddress =(的TextView)findViewById(R.id.myaddress);   myLatitude.setText(纬度:+将String.valueOf(纬度));
   myLongitude.setText(经度:+将String.valueOf(经度));   地理codeR地理codeR =新的地缘codeR(这一点,Locale.ENGLISH);   尝试{
   清单<地址>地址=地理coder.getFromLocation(纬度,经度,1);   如果(地址!= NULL){
   地址returnedAddress = addresses.get(0);
   StringBuilder的strReturnedAddress =新的StringBuilder(地址:\\ n);
   的for(int i = 0; I< returnedAddress.getMaxAddressLineIndex();我++){
   strReturnedAddress.append(returnedAddress.getAddressLine(I))追加(\\ n);
  }
 myAddress.setText(strReturnedAddress.toString());
}
其他{
myAddress.setText(无地址回来了!);
}
}赶上(IOException异常五){
 // TODO自动生成catch块
  e.printStackTrace();
 myAddress.setText(Canont得到地址!);
 } }
 }

I need help. Im stuck in getting cityname using Geocoder api. Anthing that I did wrong here.

One thing that work is if I used this in the onLocationChanged(Location loc).

However that require me to update my coordinate by moving around. I only want to use coordinate from network.

Any help is very much appreciated.

public void DisCityName(Location loc){
    /*----------to get City-Name from coordinates ------------- */
    String cityName=null;                 
    Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());                  
    List<Address>  addresses;  
    try {  
    addresses = gcd.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);  
    if (addresses.size() > 0)  
    System.out.println(addresses.get(0).getLocality());  
    cityName=addresses.get(0).getLocality();  
    } catch (IOException e) {                 
    e.printStackTrace();  
    } 

    Toast.makeText(getBaseContext(),
        "\n\nMy Currrent City is: "+cityName
        +"\nLatitude: "+loc.getLatitude()
        +"\nLongitude: "+loc.getLongitude(),
        Toast.LENGTH_SHORT).show();}

解决方案

Try this it will give you address from the Location.

public class AndroidFromLocation extends Activity {

double LATITUDE = 37.42233;
double LONGITUDE = -122.083;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
   TextView myLatitude = (TextView)findViewById(R.id.mylatitude);
   TextView myLongitude = (TextView)findViewById(R.id.mylongitude);
   TextView myAddress = (TextView)findViewById(R.id.myaddress);

   myLatitude.setText("Latitude: " + String.valueOf(LATITUDE));
   myLongitude.setText("Longitude: " + String.valueOf(LONGITUDE));

   Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);

   try {
   List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);

   if(addresses != null) {
   Address returnedAddress = addresses.get(0);
   StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
   for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
   strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
  }
 myAddress.setText(strReturnedAddress.toString());
}
else{
myAddress.setText("No Address returned!");
}
} catch (IOException e) {
 // TODO Auto-generated catch block
  e.printStackTrace();
 myAddress.setText("Canont get Address!");
 }

 }
 }

这篇关于Android的地理codeR也不回城的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆