即使setTag()不是,Google Maps getTag()始终为null

时间:2019-06-26 12:46:38

标签: android google-maps infowindow

我正在尝试通过下面的addMarkers方法中的marker.setTag(Object object)添加标签。我已进行调试,以确保在标记中设置的值中没有null。但是,当我尝试通过getTag()检索标签数据时,我得到的是null,并且不知道为什么以及出了什么问题。

private void addMarkers() {
        resetMap();
        CollectionReference restaurantCollection = FirebaseFirestore.getInstance().collection("Restaurant_Data");
        restaurantCollection.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
                    GeoPoint restaurantPoints = documentSnapshot.getGeoPoint("geoPoint");
                    String restaurantName = documentSnapshot.getString("restaurantName");
                    String snippet = documentSnapshot.getString("restaurantDescription");
                    double lat = restaurantPoints.getLatitude();
                    double lng = restaurantPoints.getLongitude();
                    LatLng latLng = new LatLng(lat, lng);

                    if (googleMap != null) {
                        MarkerOptions markerOptions = new MarkerOptions().position(latLng).title(restaurantName).icon(BitmapDescriptorFactory.defaultMarker(162)).snippet(snippet);
                        googleMap.addMarker(markerOptions);
                        Marker marker = googleMap.addMarker(markerOptions);
                        RestaurantMarker restaurantMarker = new RestaurantMarker(documentSnapshot.getId(), marker);
                        markerRestaurantIdArrayList.add(restaurantMarker);
                        marker.setTag(restaurantMarker);
                    }
                }
            }
        });
    }

这是我检索标签的方法。我知道标记不仅是null,而且是正确的标记,因为我在地图上获得正确的latlng / placement以及标题和摘要:

@Override
    public void onInfoWindowClick(final Marker marker) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(mapviewContext);
        builder.setMessage(marker.getSnippet()).setCancelable(true).setPositiveButton("See Menu", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                RestaurantMarker restaurantMarker = (RestaurantMarker)marker.getTag();
                if (restaurantMarker != null && marker.getTag() instanceof RestaurantMarker) {
                    Log.d(TAG, "restaurant ID:"+ restaurantMarker.getRestaurantId());
                    bundle.putString("restaurantId", restaurantMarker.getRestaurantId());
                    bundle.putString("restaurantName", marker.getTitle());
                    bundle.putBoolean("direction", true);
                    restaurantMapListener.onRestaurantMap(bundle);
                } else {
                    Log.d(TAG, "restaurant marker is null");
                }
                dialogInterface.cancel();
            }
        }).setNegativeButton("Calculate Distance", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                resetSelectedMarker();
                selectedMarker = marker;
                calculateDirections(marker);
                dialogInterface.dismiss();
                markerRestaurantName = marker.getTitle();
            }
        });
        final AlertDialog alert = builder.create();
        alert.show();
    }

最后是RestaurantMaker POJO。这里没有问题,因为我正在其他地方使用它并且没有任何问题:

public class RestaurantMarker {
    public String restaurantId;
    public Marker restaurantMarker;

    public RestaurantMarker(String restaurantId, Marker restaurantMarker) {
        this.restaurantId = restaurantId;
        this.restaurantMarker = restaurantMarker;
    }

    public String getRestaurantId() {
        return restaurantId;
    }

    public void setRestaurantId(String restaurantId) {
        this.restaurantId = restaurantId;
    }

    public Marker getRestaurantMarker() {
        return restaurantMarker;
    }

    public void setRestaurantMarker(Marker restaurantMarker) {
        this.restaurantMarker = restaurantMarker;
    }
}

1 个答案:

答案 0 :(得分:0)

根据您的addMarkers()代码:

...
MarkerOptions markerOptions = new MarkerOptions().position(latLng).title(restaurantName).icon(BitmapDescriptorFactory.defaultMarker(162)).snippet(snippet);
googleMap.addMarker(markerOptions);
Marker marker = googleMap.addMarker(markerOptions);
RestaurantMarker restaurantMarker = new RestaurantMarker(documentSnapshot.getId(), marker);
markerRestaurantIdArrayList.add(restaurantMarker);
marker.setTag(restaurantMarker);
...

您将标记添加到googleMap两次(在相同的LatLng位置):一次在googleMap.addMarker(markerOptions);行,然后在Marker marker = googleMap.addMarker(markerOptions);行,但是仅将标记设置为“第二次已添加”标记。可能是当您尝试通过getTag()来为单击的标记检索标记数据时,它在未设置标记的情况下调用了“首次添加”标记(因为两个标记都具有相同的LatLng坐标)。因此,尝试从googleMap.addMarker(markerOptions);方法中删除addMarkers()行:

...
MarkerOptions markerOptions = new MarkerOptions().position(latLng).title(restaurantName).icon(BitmapDescriptorFactory.defaultMarker(162)).snippet(snippet);
//googleMap.addMarker(markerOptions);  <-- delete this line
Marker marker = googleMap.addMarker(markerOptions);
RestaurantMarker restaurantMarker = new RestaurantMarker(documentSnapshot.getId(), marker);
markerRestaurantIdArrayList.add(restaurantMarker);
marker.setTag(restaurantMarker);
...