无法将Firestore文档字段值添加到Map <string,latlng =“”>

时间:2019-02-19 17:44:23

标签: java android google-cloud-firestore

我已将几家咖啡店的名称和地址信息存储到我的Firestore数据库中。单个集合数组如下所示:

“ LocalityList / Del City / Shops / 6a9ch072kxun28dn /”

位置列表包含城市列表的地方,每个城市都有商店的列表,每个商店都包含商店信息字段(名称,地址,邮政编码,所有者等)。现在,我要做的是将地址转换为地理位置(LatLng),添加商店名称并将其LatLng添加到哈希图中,然后返回哈希图。但是,该函数始终返回一个空的哈希图。

这是我的代码:

enter code hereprivate FirebaseFirestore database = FirebaseFirestore.getInstance();

public Map<String, LatLng> getLocalShops(String userLocality, Geocoder gc){
    final Map<String, LatLng> addressList = new HashMap<String, LatLng>();
    final Geocoder g = gc;

    // Grab Names and Addresses of each store within the user's locality
    // return the list of names and addresses
    database.collection("LocalityList").document(userLocality).collection("Shops")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task){
                    if (task.isSuccessful()) {
                        for (QueryDocumentSnapshot doc : task.getResult()) {
                            String id = doc.getId();
                            String name = doc.getString("name");
                            String address = doc.getString("address");
                            String city = doc.getString("city");
                            String state = doc.getString("state");
                            String zip = doc.get("zip").toString();
                            String geoSearch = address+" "+city+", "+state;
                            LatLng pos = null;
                            List<Address> testL = null;
                            try {
                                testL = g.getFromLocationName(geoSearch, 1);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            if (testL.size() > 0){
                                pos = new LatLng(testL.get(0).getLatitude(), testL.get(0).getLongitude());
                            }

                            if (name.length() > 0 && pos != null){
                                addressList.put(name, pos);
                                Log.v(TAG, Double.toString(pos.latitude)+", "+Double.toString(pos.longitude));
                            }
                        }
                    } else {
                        Log.w(TAG, "Error getting documents.", task.getException());
                    }
                }
            });

    return addressList;
}

0 个答案:

没有答案