【问题标题】:Trouble Getting LatLng from Google places json array object从 Google 获取 LatLng 时遇到问题会放置 json 数组对象
【发布时间】:2019-04-23 13:01:47
【问题描述】:

我似乎遇到的问题是从 json 文件中访问“更深层次”的 lat lng 值。

正在使用这种类型的链接从 google 实时访问 Json 文件: https://maps.googleapis.com/maps/api/place/nearbysearch/json?

这是 json 中单个对象/数组项的外观,包括“顶级”“结果”。

"results" : [
      {
         "geometry" : {
            "location" : {
               "lat" : 55.4628609,
               "lng" : -4.6299348
            },
            "viewport" : {
               "northeast" : {
                  "lat" : 55.46420472989273,
                  "lng" : -4.628674020107278
               },
               "southwest" : {
                  "lat" : 55.46150507010728,
                  "lng" : -4.631373679892723
               }
            }
         },
         "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png",
         "id" : "0655ceeeddd83f1a901bcef361b22cbfa951ae73",
         "name" : "GAME",
         "opening_hours" : {
            "open_now" : false
         },
         "place_id" : "ChIJM8hBpZ3WiUgRuWc3KhfMJys",
         "plus_code" : {
            "compound_code" : "F97C+42 Ayr, UK",
            "global_code" : "9C7QF97C+42"
         },
         "rating" : 4.2,
         "reference" : "ChIJM8hBpZ3WiUgRuWc3KhfMJys",
         "scope" : "GOOGLE",
         "types" : [ "electronics_store", "store", "point_of_interest", "establishment" ],
         "vicinity" : "120 High St, Ayr"
      },

这是我在将json文件数据解析为字符串后访问它的方法。

  void createMarkersFromJson(String json) throws JSONException {
        JSONObject object = new JSONObject(json);
        JSONArray jsonArray = object.getJSONArray("results");
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObj = jsonArray.getJSONObject(i);

            map.addMarker(new MarkerOptions()
                    .title(jsonObj.getString("name"))
                    .position(new LatLng(
                            jsonObj.getJSONArray("geometry").getDouble(0),
                            jsonObj.getJSONArray("geometry").getDouble(1)
                    ))
            );
        }
    }

【问题讨论】:

    标签: android json google-maps android-studio


    【解决方案1】:

    几何不是数组。以 CURLY BRACE {} 开头的 JSON 是一个对象,而 BRACKET [ ] 表示一个数组。试试这个。

        JSONArray jsonArray= object.getJSONArray("results");
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObj = jsonArray.getJSONObject(i);
            JSONObject locationObj = jsonObj .getJSONObject("geometry")
                                           .getJSONObject("location");
    
            map.addMarker(new MarkerOptions()
                    .title(jsonObj.getString("name"))
                    .position(new LatLng(
                            locationObj.getDouble("lat"),
                            locationObj.getDouble("lng")
                    ))
            );
        }
    

    希望这可以帮助您阐明 JSON 数组和对象之间的区别,以及如何访问它们。干杯

    【讨论】:

    • 它的工作原理是没有错误但标记不显示在地图上。感谢您的描述。
    • @PyStraw45 那是一个不同的问题伙伴。这篇文章并不能帮助我们找出问题所在
    • 很公平,我会确保 latlng 正确解析并看看我能从那里做些什么,但还是谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-25
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    相关资源
    最近更新 更多