【问题标题】:How to access nested Hashmap如何访问嵌套的 Hashmap
【发布时间】:2019-09-28 06:52:33
【问题描述】:

我想访问 Inner Hashmap,这样我就可以建立一个地理围栏,每个地理围栏都有一个自定义半径。

我已经在整个互联网上寻找这个问题的答案,但我似乎没有找到任何看起来相似的代码,而另一个关于如何访问嵌套 Hashmap 的帖子对我的问题没有帮助。

如有任何帮助或建议,我们将不胜感激。

我已经尝试了我在相关帖子上阅读的所有内容,但似乎没有任何效果。


private void setmGeofenceList(HashMap<String, Map.Entry<LatLng,Integer>> mhashMap)
    {
        for (Map.Entry<String, Map.Entry<LatLng,Integer>> entry : mhashMap.entrySet()) {

            mGeofenceList.add(new Geofence.Builder()
                    .setLoiteringDelay(1000)
                    .setNotificationResponsiveness(1000)
                    // Set the request ID of the geofence. This is a string to identify this
                    // geofence.
                    .setRequestId(entry.getKey())
                    // Set the circular region of this geofence.
                    .setCircularRegion(

                            //THIS is the problem, this wont access the LatLng Value that is inside the inner hashmap
                            entry.getValue().latitude, //error: cant access sysmbol Latitude

                            entry.getValue().longitude, //error: cant access sysmbol Longitude


                    )
                    // Set the expiration duration of the geofence. This geofence gets automatically
                    // removed after this period of time.
                    .setExpirationDuration(Geofence.NEVER_EXPIRE)

                    // Set the transition types of interest. Alerts are only generated for these
                    // transition. We track entry and exit transitions in this sample.
                    .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
                            Geofence.GEOFENCE_TRANSITION_EXIT)

                    // Create the geofence.
                    .build());

        }
    }

// 这是 Hashmap 存储值的地方

    static final HashMap<String, Map.Entry<LatLng,Integer>>RED_LINE_GEOFENCES = new HashMap<>();
    static {
        RED_LINE_GEOFENCES.put("PlatformA", new AbstractMap.SimpleEntry<>(new LatLng(-33.905679, 19.112890), 300));
        RED_LINE_GEOFENCES.put("Ticket Office", new AbstractMap.SimpleEntry<>(new LatLng(-33.911024, 19.119688), 300));
        RED_LINE_GEOFENCES.put("Maison", new AbstractMap.SimpleEntry<>(new LatLng(-33.886851, 19.076072), 300));
        RED_LINE_GEOFENCES.put("Mont Rochelle", new AbstractMap.SimpleEntry<>(new LatLng(-33.914458, 19.106716), 300));
        RED_LINE_GEOFENCES.put("Holden Manz", new AbstractMap.SimpleEntry<>(new LatLng(-33.934360, 19.113939), 300));
        RED_LINE_GEOFENCES.put("Charmonix", new AbstractMap.SimpleEntry<>(new LatLng(-33.899535, 19.127489), 300));
        RED_LINE_GEOFENCES.put("DieuDonne", new AbstractMap.SimpleEntry<>(new LatLng(-33.891131, 19.133831), 300));
        RED_LINE_GEOFENCES.put("Grande Provance", new AbstractMap.SimpleEntry<>(new LatLng(-33.899443, 19.103134), 300));
        RED_LINE_GEOFENCES.put("Rickety Bridge", new AbstractMap.SimpleEntry<>(new LatLng(-33.894788, 19.097545), 300));
    }

【问题讨论】:

    标签: java android dictionary hashmap android-geofence


    【解决方案1】:

    entry.getValue() 返回一个以 LatLng 为键的映射条目。因此,要访问它们,您需要 entry.getValue().getKey().latitudeentry.getValue().getKey().longitude

    【讨论】:

      【解决方案2】:

      您缺少 getKey() 调用。你需要这个;

       entry.getValue().getKey().latitude
      

      【讨论】:

        【解决方案3】:

        代替

        entry.getValue().latitude, //error: cant access symbol Latitude
        entry.getValue().longitude, //error: cant access symbol Longitude
        

        使用这些语句:

        entry.getValue().get("latitude");
        entry.getValue().get("longitude");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-02-16
          • 2011-12-27
          • 1970-01-01
          • 2021-07-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多