反向地理编码不起作用

发布于 2025-01-07 04:22:25 字数 3270 浏览 1 评论 0原文

我编写了以下代码来获取当前位置。尽管我正在具有不同纬度和经度的模拟器中测试它。但它无法转换真实位置的纬度和经度。

          public class LocationFindingActivity extends Activity {
/** Called when the activity is first created. */

EditText et;
LocationManager  locationManager;
String provider;
LocationListener locationListener;
Location currentLocation;
String addressString="";
String longlattString="";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    et=(EditText)findViewById(R.id.locationTXT);

    locationManager =(LocationManager) getSystemService(Context.LOCATION_SERVICE);

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    provider = locationManager.getBestProvider(criteria, true);
    currentLocation=locationManager.getLastKnownLocation(provider);


    et.setText(addressString);
    locationListener =new LocationListener() {

        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderDisabled(String arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onLocationChanged(Location arg0) {
            // TODO Auto-generated method stub
            updateWithNewLocation(currentLocation);

        }
    };
    updateWithNewLocation(currentLocation);
     locationManager.requestLocationUpdates(provider, 2000, 10,locationListener)

}



private void updateWithNewLocation(Location location) {




    if (location != null) 
    {
      double lat = location.getLatitude();
      double lng = location.getLongitude();
      longlattstring="Lattitude :"+lat+"Longitude :"+lng;


     Geocoder gc = new Geocoder(this, Locale.getDefault());

     try 
     {
            List<Address> addresses = gc.getFromLocation(lat, lng, 1);
            StringBuilder sb = new StringBuilder();
            //Toast.makeText(this, "Problem1", 2000).show();
            if (addresses.size() > 0)
            {
                    Address address = addresses.get(0);
                    for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
                        sb.append(address.getAddressLine(i)).append("\n");
                    sb.append(address.getLocality()).append("\n");
                    Toast.makeText(this, "Problem2", 2000).show();
                    sb.append(address.getPostalCode()).append("\n");
                    sb.append(address.getCountryName());
            }
            addressString = sb.toString();
     } 
     catch (IOException e) 
     {
         Toast.makeText(this, "Problem Catch", 2000).show();
     }
}


    else 
    {
        addressString = "No location found";    
    }

    et.setText(addressString);


}

List

我在这行中遇到问题

地址 = gc.getFromLocation(lat, lng, 1);

该声明不返回任何内容。

i written following code to get current location. although I m testing it in emulator with different latitude and longitude. But it cant convert the lattitude and longitude in real location.

          public class LocationFindingActivity extends Activity {
/** Called when the activity is first created. */

EditText et;
LocationManager  locationManager;
String provider;
LocationListener locationListener;
Location currentLocation;
String addressString="";
String longlattString="";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    et=(EditText)findViewById(R.id.locationTXT);

    locationManager =(LocationManager) getSystemService(Context.LOCATION_SERVICE);

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    provider = locationManager.getBestProvider(criteria, true);
    currentLocation=locationManager.getLastKnownLocation(provider);


    et.setText(addressString);
    locationListener =new LocationListener() {

        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderDisabled(String arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onLocationChanged(Location arg0) {
            // TODO Auto-generated method stub
            updateWithNewLocation(currentLocation);

        }
    };
    updateWithNewLocation(currentLocation);
     locationManager.requestLocationUpdates(provider, 2000, 10,locationListener)

}



private void updateWithNewLocation(Location location) {




    if (location != null) 
    {
      double lat = location.getLatitude();
      double lng = location.getLongitude();
      longlattstring="Lattitude :"+lat+"Longitude :"+lng;


     Geocoder gc = new Geocoder(this, Locale.getDefault());

     try 
     {
            List<Address> addresses = gc.getFromLocation(lat, lng, 1);
            StringBuilder sb = new StringBuilder();
            //Toast.makeText(this, "Problem1", 2000).show();
            if (addresses.size() > 0)
            {
                    Address address = addresses.get(0);
                    for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
                        sb.append(address.getAddressLine(i)).append("\n");
                    sb.append(address.getLocality()).append("\n");
                    Toast.makeText(this, "Problem2", 2000).show();
                    sb.append(address.getPostalCode()).append("\n");
                    sb.append(address.getCountryName());
            }
            addressString = sb.toString();
     } 
     catch (IOException e) 
     {
         Toast.makeText(this, "Problem Catch", 2000).show();
     }
}


    else 
    {
        addressString = "No location found";    
    }

    et.setText(addressString);


}

}

I am getting problem in this line

List addresses = gc.getFromLocation(lat, lng, 1);

the statement doesn't return anything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的痛♀有谁懂 2025-01-14 04:22:25

这是一个已知错误,他们从未修复过,请参阅服务不可用。我想您会发现它可以在 API 级别 7 模拟器中运行。

It's a known bug which they never fixed see service not avavilable.I think you will find that it works in the the API level 7 emulator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文