【问题标题】:Differentiate markers Android MapActivity区分标记Android MapActivity
【发布时间】:2018-01-17 18:29:37
【问题描述】:

我正在尝试在 Android 上使用 GoogleMaps 区分标记。我尝试了 StackOverflow 上提出的几种解决方案。其中一个是为标记设置标签,在点击标记上我会检索该标签(取决于我点击的那个标记),问题是它没有区分并为我目前的两个标记显示相同的信息有。

下面是部分代码:

private void markUserLocationMap(final Double currentUserLatitude, final Double currentUserLongitude) {
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    db.collection("users").get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (DocumentSnapshot document : task.getResult()) {
                            Log.d(TAG, document.getId() + " => " + document.getData());
                            Map<String, Object> data = document.getData();

                            String s = data.get("location").toString();
                            List<String> userInfo = new ArrayList<String>();
                            final String username = data.get("username").toString();
                            userInfo.add(username);
                            final String teamName = data.get("teamName").toString();
                            userInfo.add(teamName);
                            String latitude = s.substring(s.indexOf("latitude") + 9, s.indexOf(","));
                            String longitude = s.substring(s.indexOf("longitude") + 10,s.indexOf("}"));
                            double latitudeValue = Double.parseDouble(latitude);
                            double longitudeValue = Double.parseDouble(longitude);

                            if(( (currentUserLatitude != latitudeValue)&& (currentUserLongitude != longitudeValue) ) && ((latitudeValue+1<=currentUserLatitude) ||(latitudeValue-100<=currentUserLatitude))  && ((longitudeValue+100<=currentUserLongitude) ||(longitudeValue-100<=currentUserLongitude)) ){ //alterar os valores, para alterar o radius
                                LatLng userInteracting = new LatLng(latitudeValue, longitudeValue); //the user that its currently being accessed
                                mCurrLocationMarker = mMap.addMarker(new MarkerOptions()
                                        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
                                        .position(userInteracting)
                                        .title(username)); //para mostrar o seu nome ao clicar no marker
                                mCurrLocationMarker.setTag(userInfo);

                                mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {      //mMap is a GoogleMap object
                                    @Override
                                    public boolean onMarkerClick(Marker marker) {
                                        Intent i = new Intent(MapActivity.this, Pop.class);
                                        String userInfo = (mCurrLocationMarker.getTag()).toString();
                                        i.putExtra("userInfo",userInfo);

                                        startActivity(i);
                                        return false;
                                    }
                                });


                            }

                        }
                    } else {
                        Log.d(TAG, "Error getting documents: ", task.getException());
                    }
                }
            });

}

所以在这里我试图将进入标签的 userInfo 存储起来,然后每当我单击标记时,我都应该检索该特定的 userInfo,正如我之前所说,它为两个标记返回相同的 userInfo我有,标记之间的标签不应该不同吗?我通过 FireStore 访问 userInfo,并遍历每个用户,并为每个用户显示一个标记。

【问题讨论】:

    标签: android google-maps google-maps-markers


    【解决方案1】:

    我认为问题出在你的听众身上

    @Override
    public boolean onMarkerClick(Marker marker) {
        Intent i = new Intent(MapActivity.this, Pop.class);
        // Don't use the field mCurrLocationMarker, use the passed in marker object
        String userInfo = (marker.getTag()).toString();
        i.putExtra("userInfo",userInfo);
        startActivity(i);
        return false;
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      • 1970-01-01
      • 1970-01-01
      • 2011-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多