代码之家  ›  专栏  ›  技术社区  ›  Sheehan Alam

Geocoder并不总是返回值

  •  4
  • Sheehan Alam  · 技术社区  · 14 年前

    我能够成功获取lat/long并将其传递给地理编码器以获取地址。然而,我并不总是能得到一个地址。好像需要几次尝试?我不知道为什么。

    现在有没有更好的方法让我得到地址?

    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;
            }
    

    我在这里调用此方法:

    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();
            }
    
    5 回复  |  直到 14 年前
        1
  •  4
  •   Alex Bravo Maurizio FIORINO    6 年前

    根据我的经验,谷歌地理编码器并不总是工作,我在地图上有几个固定的点,当我点击覆盖图时,它会弹出一个吐司,上面写着那条经文的地址,这些点不会改变,有时我会点击同一个点10次,但我只得到其中的7个地址。

    这很奇怪,但就是这样,我只是修改了我的应用程序来解决这个问题。

        2
  •  3
  •   Naveen Kumar    9 年前

    上述方法以及internet上的许多其他示例的问题是,它们只尝试从第一个返回的提供程序检索数据 addresses.get(0).getPostalCode() . 执行代码时 List<Address> addresses = getAddresses() ,它检索提供者列表,每个提供者提供自己的数据。并非所有提供程序都包含相同的数据。

    我收到邮政编码也有同样的问题。在我最初的测试中,我发现我收到了来自6家供应商的数据。这6个提供者中只有2个返回邮政编码。我试图访问的第一个提供者 address.get(0).getPostalCode() ,不是其中之一。一种更合适的方法是遍历所有返回的提供程序,看看是谁返回了我正在寻找的数据。

    像下面这样的方法应该行得通。很可能,其中一个提供商会返回邮政编码。这可以对提供者返回的任何地理编码数据执行。

    List<Address> address = gc.getFromLocation(myLatitude, myLongitude, 10);
    
    if (address.size() > 0) {
       strZipcode = address.get(0).getPostalCode();
    
       //if 1st provider does not have data, loop through other providers to find it.
       count = 0;
       while (strZipcode == null && count < address.size()) {
          strZipcode = address.get(count).getPostalCode();
          count++;
       }
    }
    
        3
  •  0
  •   Joshua Pardee - Engineer    10 年前

    如果有人还在找解决办法,我就这么做了: 首先,我确保所有的点都是好的(将加载),但可能并不总是被地理编码(获取数据是命中或未命中的) 第二,我们强迫代码做我们想做的事情,换句话说,如果我们知道它可以找到一个lat和long,让它继续尝试直到它找到!这似乎也没有影响我的装载时间!下面是代码:

    do {
    $geocode=file_get_contents('https://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&key='.$key.'sensor=false');
    
    $output= json_decode($geocode);
    $lat = $output->results[0]->geometry->location->lat;
    $long = $output->results[0]->geometry->location->lng;
    } while($lat == '' and $long == '');
    

    这将继续循环,直到lat和long都不是空字符串!

    快乐编码!:P页

        4
  •  0
  •   Nanda Gopal    9 年前

    地理编码器在某些区域不工作。您可以使用地理编码Api,而不是使用地理编码器。按照链接获取有关api的详细信息,

    https://developers.google.com/s/results/?q=geocoding+api

        5
  •  0
  •   lm2a    8 年前

    如果使用“自动完成”,则可以从place对象获取位置:

    private ResultCallback<PlaceBuffer> mUpdatePlaceDetailsCallback
            = new ResultCallback<PlaceBuffer>() {
        @Override
        public void onResult(PlaceBuffer places) {
            if (!places.getStatus().isSuccess()) {
                // Request did not complete successfully
                AALog.e("Geocoder Place query did not complete. Error: " + places.getStatus().toString());
                return;
            }
            // Get the Place object from the buffer.
            final Place place = places.get(0);
            //--------------here you can recover the place location---------------------
            ((DrawerActivity)getActivity()).lastPlace = place.getLatLng();
            //----------------------------------------------------------------------           
            places.release();
            getActivity().getFragmentManager().beginTransaction().remove(AutocompleteFragment.this).commit();
        }
    };
    

    此回调应在AdapterView.OnItemClickListener中注册如下:

            PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi
                    .getPlaceById(mGoogleApiClient, placeId);
            placeResult.setResultCallback(mUpdatePlaceDetailsCallback);