new Geocoder(this).getFromLocationName(“taj mahal”,1)没有返回结果既没有给出任何异常

时间:2016-03-17 02:42:44

标签: android

未获得列表大小> 0,我正在使用地理编码器。 使用googleMap for android的高级计划使用地理编码器是否必要?

 @Override
    public void onMapReady(GoogleMap googleMap) {
      /* mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(sydney, 15);
        mMap.moveCamera(update);
*/
        try {
            pinaddress();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("errorrrrrrrrrrrrrrrrrrrr");
        }



    }

    private void gotoLocation(double lat, double lon, float zoom) {
        LatLng l1 = new LatLng(lat, lon);
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(l1, zoom);
        mMap.moveCamera(update);
    }

    private void pinaddress() throws IOException {
        Geocoder geocoder = new Geocoder(this);
        List<Address> list;

        list = geocoder.getFromLocationName("taj mahal", 1);

        if (list.size() > 0) {
            Address address = list.get(0);
            String locality = address.getLocality();
            System.out.println("locality" + locality);
            Toast.makeText(this, locality, Toast.LENGTH_LONG).show();
            double lat = address.getLatitude();
            double lon = address.getLongitude();
            gotoLocation(lat, lon, 15);
        } else
            Toast.makeText(this, "checkout connection", Toast.LENGTH_LONG).show();

    }

onmapready中的注释部分在带有标记的地图上显示正确  但使用地理编码器,其列表大小= 0和 在屏幕上只显示地图 并输出

03-16 13:03:42.522 24772-24843/com.jagdiv.android.myapplicationmap D/Volley: [370] a.a: HTTP response for request=<[ ] https://clients4.google.com/glm/mmap/api 0x99e6744e NORMAL 1> [lifetime=5290], [size=47], [rc=200], [retryCount=0]
03-16 13:03:42.522 24772-24772/com.jagdiv.android.myapplicationmap D/Volley: [1] l.b: 5290 ms: [ ] https://clients4.google.com/glm/mmap/api 0x99e6744e NORMAL 1

1 个答案:

答案 0 :(得分:0)

文件说 -

  

如果未找到匹配项或没有匹配项,则返回null或空列表   提供后端服务。

许多人提出了一个民意调查概念,它适用于我们中的许多人(我同意消除我们的api限制,但有一些方法可以在某种程度上克服这一点,在你身边实施一些缓存技术) -

try {
    int attempt=10;
    List<Address> list = geocoder.getFromLocationName("taj mahal", 1);
    while (list.size()==0 && attempt>0) {
        attempt--;
        list = geocoder.getFromLocationName("taj mahal", 1);
    }
    if (list.size()>0) {
        Address address = list.get(0);
            String locality = address.getLocality();
            System.out.println("locality" + locality);
            Toast.makeText(this, locality, Toast.LENGTH_LONG).show();
            double lat = address.getLatitude();
            double lon = address.getLongitude();
            gotoLocation(lat, lon, 15);
    }
} catch (Exception e) {
    System.out.print(e.getMessage());
}