地理codeR并不总是返回值并不、返回值、地理、codeR

2023-09-13 00:56:23 作者:为幸福向前冲

我能够顺利拿到纬度/经度并将其传递到地理codeR得到一个地址。不过,我并不总是得到一个地址返回给用户。好像它需要一对夫妇的尝试?我不知道为什么。

有没有对我来说,在这一点上获得的地址?一个更好的办法

 公开名单<地址> getAddresses(){
          地理codeR地理codeR =新的地缘codeR(getApplicationContext(),Locale.getDefault());
          名单<地址>地址=新的ArrayList();
             尝试 {
       地址=地理coder.getFromLocation(纬度,经度,1);
      }赶上(IOException异常E){
       e.printStackTrace();
      }

      返回地址;
        }
 

我调用这个方法在这里:

  LocationListener的onLocationChange =新LocationListener的(){
        公共无效onLocationChanged(位置LOC){
            //设置和显示的经纬度/长时,所提供的位置
            字符串经纬度=纬度:+ loc.getLatitude()+长:+ loc.getLongitude();
            纬度= loc.getLatitude();
            经度= loc.getLongitude();

            名单<地址>地址= getAddresses();

            如果(addresses.size()大于0&安培;&安培;地址=空!)
             拉链code = addresses.get(0).getPostal code();

            Toast.makeText(getApplicationContext(),ZIP code,Toast.LENGTH_SHORT).show();
        }
 

解决方案 对领域的认知比会写代码更重要

在我的经验,Google的地理codeR总不工作,我有几个固定点在地图上,当我点击了覆盖它弹出一个敬酒的联系地址为纬度/长,这点不会改变,有时我点击一个相同点10次,但我只让他们的联系地址7。

这是奇怪的,但多数民众赞成事情是这样的,我只是修改我的应用程序,以解决该问题。

I am able to successfully get lat/long and pass it to the geocoder to get an Address. However, I don't always get an address back. Seems like it takes a couple of attempts? I'm not sure why.

Is there a better way for me to obtain the address at this point?

public List<Address> getAddresses(){
          Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
          List<Address> addresses = new ArrayList();
             try {
       addresses = geocoder.getFromLocation(latitude, longitude, 1);
      } catch (IOException e) {
       e.printStackTrace();
      }

      return addresses;
        }

I am calling this method here:

LocationListener onLocationChange=new LocationListener() {
        public void onLocationChanged(Location loc) {
            //sets and displays the lat/long when a location is provided
            String latlong = "Lat: " + loc.getLatitude() + " Long: " + loc.getLongitude();   
            latitude = loc.getLatitude();
            longitude = loc.getLongitude();

            List<Address> addresses = getAddresses();

            if(addresses.size() > 0 && addresses != null)
             zipcode = addresses.get(0).getPostalCode();

            Toast.makeText(getApplicationContext(), zipcode,Toast.LENGTH_SHORT).show();
        }

解决方案

In my experience Googles Geocoder doesnt always work, I have a couple of fixed points on the map, when I click on the overlay it pops a toast with the adress for that lat/long, these points do not change, sometimes I click on a same point 10 times, but I only get an adress 7 of them.

It's wierd, but thats the way it is, I just modified my app to work around the problem.