2016-08-21 77 views
2

我想要使用 获取特定地址的经度和纬度addressList = geoCoder.getFromLocationName(locationName,1);Android地理编码getFromLocationName失败,有效地址

对于大多数地址来说,这工作得很好,但有一些有效的地址,如“Lentoasemantie 1,Vantaa”,它返回空数组。奇怪的是,有效地址在4天前曾经工作过,但大部分地址仍然可以继续工作。

因此,这看起来像谷歌后端问题,我想知道我应该向谷歌(哪里/如何?)报告这个或者离开使用地理编码器,因为它本质上不可靠?

回答

1

地理编码器Android API不如Google地图地理编码API那么高效,所以我建议您使用此地址代替Geocoder。 您可以在下面找到通过Volley队列获取地址的功能(对于Lentoasemantie 1,Vantaa,它的工作原理):

public void getLocationFromAddress(String address) { 
    String url = "https://maps.googleapis.com/maps/api/geocode/json?address=" 
      + Uri.encode(address) + "&sensor=true&key=API_KEY"; 
    RequestQueue queue = Volley.newRequestQueue(this); 
    JsonObjectRequest stateReq = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() { 
     @Override 
     public void onResponse(JSONObject response) { 
      JSONObject location; 
      try { 
       // Get JSON Array called "results" and then get the 0th 
       // complete object as JSON 
       location = response.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location"); 
       // Get the value of the attribute whose name is 
       // "formatted_string" 
       if (location.getDouble("lat") != 0 && location.getDouble("lng") != 0) { 
        LatLng latLng = new LatLng(location.getDouble("lat"), location.getDouble("lng")); 

        //Do what you want 
       } 
      } catch (JSONException e1) { 
       e1.printStackTrace(); 

      } 
     } 

    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      Log.d("Error.Response", error.toString()); 
     } 
    }); 
    // add it to the queue 
    queue.add(stateReq); 

}