如何使用Android中的Google商家信息Api操作存储附近地址和地址?

时间:2014-07-20 10:15:07

标签: android google-maps google-places-api google-places

我尝试使用Google Places Api Actions在Android上保存新地点。为了做到这一点,我已经下载了这个工作正常的简单库。

https://github.com/windy1/google-places-api-java#add-place

如果我使用标准方法创建一个新位置,则可以使用范围APP按预期显示该位置。

            //In my AsyncTask Method: 
             GooglePlaces client = new GooglePlaces("MY API KEY");


            double lat = 45.231745;
            double lng = 10.248884;

            try{

                se.walkercrou.places.Place place = client.addPlace("Test Location", "it", lat, lng, 50, "bar");

            }catch(Exception e){

                e.printStackTrace();
            }

但是我希望能够存储地址字段和邻近字段。阅读文档和库的代码可以通过向方法addPlace添加两个额外的参数来完成,如下所示:

             GooglePlaces client = new GooglePlaces("MY API KEY");

            double lat = 45.231745;
            double lng = 10.248884;

            try{

                Param p = Param.name("vicinity");
                p.value("My example address 6, CR Italy");

                Param p2 = Param.name("address");
                p2.value("My example address 6, CR Italy");


                se.walkercrou.places.Place place = client.addPlace("Test Location2", "it", lat, lng, 50, "bar",p,p2);



            }catch(Exception e){

                e.printStackTrace();
            }

该方法不会出现任何错误。但是,存储的地方不包含邻近参数。这是我在调用方法列出地点时得到的响应:

{
     "geometry" : {
        "location" : {
           "lat" : 45.231745,
           "lng" : 10.248884
        }
     },
     "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/bar-71.png",
     "id" : "344d2ffcb408cfefcb6ff137e0c8dcbb0cea901d",
     "name" : "Test Location2",
     "place_id" : "qgYvCi0wMDAwMDA5Y2UwYThjNGM5OjQ3ODFhZWJlZGM3OjBiNWZmNGIxZTNmNDFlYzU",
     "reference" : "CnRqAAAAjSMpi9N2keFELpIekQ1-b2_RJKeCVLhC9lpxvqoZZqgtgftpVJMeGiAk4F_uHLXu9UUeuIWYLJk2jhVMyq3-P09PwqLelQRvJy3bCeEqnNJSdf_OGHUFRaXV7mKjoyUBSiCWY4C9dOwxsv6IYRbf8xIQjtIH5FP5NVE7toep-PJUOBoUsSeWqsTD6PZuEF8aE3PX0sJmYIA",
     "scope" : "APP",
     "types" : [ "bar", "establishment" ]
  }

这个地方在那里,但没有地址或附近。我究竟做错了什么?

我还尝试使用简单的http post请求来实现我自己的方法版本:

public String addPlaceMap() {

HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout
String result=null;                                                                     // Limit
HttpResponse response;

JSONObject placeJson =  new JSONObject();
JSONObject location = new JSONObject();
try {
location.put("lat", 45.231745);
location.put("lng", 10.248884);
placeJson.put("name", "Test 1");
placeJson.put("location", location);
placeJson.put("accuracy", 50);

JSONArray jarr = new JSONArray();
jarr.put("bar");
placeJson.put("types", jarr);
placeJson.put("vicinity", "My test address 1, Cremona Italy");
placeJson.put("address", "My test address 1, Cremona Italy");
placeJson.put("language", "it");


    HttpPost post = new HttpPost("https://maps.googleapis.com/maps/api/place/add/json?key=MY API KEY");
    post.setHeader("Content-Type", "application/json");
    StringEntity se = new StringEntity(placeJson.toString());
    post.setEntity(se);
    response = client.execute(post);

    if (response == null) {


        return null;
    }

    if (response != null) {
        InputStream in = response.getEntity().getContent(); 
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(in, "iso-8859-1"));

        StringBuilder sb = new StringBuilder();
        sb.append(reader.readLine() + "\n");

        String line = "0";
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        in.close();
        result = sb.toString();

        Log.i("RESULT", result);

    return result;  

    }
} catch (Exception e) {
    e.printStackTrace();
        return null;
}
return null;

 }

在这种情况下,我得到了Google的成功回复。但是,附近仍然没有可见。我究竟做错了什么?在Google验证之前,此字段是否可见?

1 个答案:

答案 0 :(得分:1)

也许您需要为这些字段指定特定格式。看看this是否有帮助。