在谷歌地图 V2 android studio 中单击 InfoWindowClick 时,如何设置唯一键?

Posted

技术标签:

【中文标题】在谷歌地图 V2 android studio 中单击 InfoWindowClick 时,如何设置唯一键?【英文标题】:How can I intent a unique key when InfoWindowClick was click in google maps V2 android studio? 【发布时间】:2020-01-30 07:47:44 【问题描述】:

我想设置其他活动成功运行所需的两个唯一键,但是当涉及到标记中的 infowindowclick 时,我不知道如何设置这两个唯一键。有没有办法可以正确传递数据而不是将这些键添加到 sn-p?请看我下面的代码。

// I am getting the values of the store here including the storeID
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(latLng);
for (DocumentSnapshot storeDS: queryDocumentSnapshots)
    StoreDetailsModel detailsModel = storeDS.toObject(StoreDetailsModel.class);
    GeoPoint storeAddress = detailsModel.getStoreLocation();
    double LatitudeData = storeAddress.getLatitude();
    double LongitudeData = storeAddress.getLongitude();
    final LatLng SlatLng = new LatLng(LatitudeData, LongitudeData);

    if (SphericalUtil.computeDistanceBetween(latLng, SlatLng) < 35000)
        String storeID = detailsModel.getStoreID();
        String storeName = detailsModel.getStoreName();
        String storeClass = detailsModel.getStoreClassification();
        HashMap storeImg = detailsModel.getStoreImage();
        String image = storeImg.get("profile").toString();

        addCustomMarkerFromURL(SlatLng, image, storeName, storeClass, mMap, storeID, uid);
        builder.include(SlatLng);
    

LatLngBounds bounds = builder.build();
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, 200);
mMap.moveCamera(cameraUpdate);
mMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
mMap.setOnInfoWindowClickListener(MapFragment.this);


...


//Then here is the method for infoWindowClick Listener
@Override
    public void onInfoWindowClick(Marker marker) 
        Intent intent = new Intent(this, StoreDetailsActivity.class)

        //Here I have a trouble finding a way to pass the data for the storeID and buyerID

        //intent.putExtra("storeID", storeID);
        //intent.putExtra("buyerID", buyerID);
        startActivity(intent);
    

addCustomMarkerFromURL() 方法:

private void addCustomMarkerFromURL(final LatLng slatLng, String image, final String storeName, final String storeClass, final GoogleMap mMap, final String storeID, final String uid) 
        if (mMap == null) 
            return;
        
        Glide.with(getActivity())
                .asBitmap()
                .load(image).fitCenter()
                .into(new SimpleTarget<Bitmap>() 
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @androidx.annotation.Nullable Transition<? super Bitmap> transition) 
//                        mMap.setInfoWindowAdapter(new GoogleInfoWindowAdapter(getActivity(), storeID, uid));
                        mMap.addMarker(new MarkerOptions()
                                .position(slatLng)
                                .icon(BitmapDescriptorFactory.fromBitmap(getMarkerBitmapFromView(getActivity(), resource, storeName))).title(storeClass));
                    
                );
    

getMarkerBitmapFromView() 方法

private Bitmap getMarkerBitmapFromView(FragmentActivity activity, Bitmap resource, String storeName) 
        View view = ((LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.bitmap_google_marker_layout_item, null);

        TextView txt_name = (TextView) view.findViewById(R.id.name);
        txt_name.setText(storeName);

        de.hdodenhof.circleimageview.CircleImageView image = (de.hdodenhof.circleimageview.CircleImageView) view.findViewById(R.id.store_profile_image);
        image.setImageBitmap(resource);
        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.buildDrawingCache();
        Bitmap returnedBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(returnedBitmap);
        canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN);
        Drawable drawable = view.getBackground();
        if (drawable != null)
            drawable.draw(canvas);
        view.draw(canvas);
        return returnedBitmap;
    

【问题讨论】:

【参考方案1】:

你可以用tag代替sn-p。

创建一个具有 2 个属性“storeId”和“buyerId”的类

public class MyRefId 
    private int storeID;
    private int buyerID;

    public MyRefId(int storeID, int buyerID) 
        this.storeID = storeID;
        this.buyerID = buyerID;
    

    public int getStoreID() 
        return storeID;
    

    public void setStoreID(int storeID) 
        this.storeID = storeID;
    

    public int getBuyerID() 
        return buyerID;
    

    public void setBuyerID(int buyerID) 
        this.buyerID = buyerID;
    


在 addCustomMarkerFromURL() 中

private void addCustomMarkerFromURL(final LatLng slatLng, String image, final String storeName, final String storeClass, final GoogleMap mMap, final String storeID, final String uid) 
    if (mMap == null) 
        return;
    
    Glide.with(getActivity())
            .asBitmap()
            .load(image).fitCenter()
            .into(new SimpleTarget<Bitmap>() 
                @Override
                public void onResourceReady(@NonNull Bitmap resource, @androidx.annotation.Nullable Transition<? super Bitmap> transition) 
                     // to make it clear for you I do it this way
                     // start by new MarkerOption()
                        MarkerOptions markerOptions = new MarkerOptions()
                                 .position(slatLng) 
                                 .icon(BitmapDescriptorFactory.fromBitmap(
                                   getMarkerBitmapFromView(getActivity(), resource, storeName))).title(storeClass);

                    // when you call mMap.addMarker(markerOptions) a new 
                    // marker will be added to the map
                    // this method also return those marker
                    // to set tag u can do sth like:
                       Marker marker = mMap.addMarker(markerOptions);
                       marker.setTag(new MyRefId(storID,uid));

                   // or
                   // mMap.addMarker(markerOptions).setTag(new MyRefId(storID,uid));
                
            );

那么这里是infoWindowClick监听器的方法

@Override
    public void onInfoWindowClick(Marker marker) 
        // get marker tag
        MyRefId refId = (MyRefId) marker.getTag();

        if(refId != null)
            Intent intent = new Intent(this, StoreDetailsActivity.class);
            intent.putExtra("storeID", refId.getStoreID());
            intent.putExtra("buyerID", refId.getBuyerID());
            startActivity(intent);
        
    

【讨论】:

感谢您的回答,但请原谅我缺乏知识。您能以 Android Studio 的方式向我展示这个吗?谢谢! 你能告诉我这个函数的代码吗:addCustomMarkerFromURL(),在哪里调用了mMap.addMarker()? 我已经包含了创建自定义标记的方法。 我还有一个关于使用标记的问题。如果数据库中有很多数据,我应该如何实例化它?只实例化一个标记变量可以吗? 我认为实例化很多标记是可以的。当您调用 mMap.addMarker(markerOptions) 时,此方法将实例化一个新标记并将其添加到地图中

以上是关于在谷歌地图 V2 android studio 中单击 InfoWindowClick 时,如何设置唯一键?的主要内容,如果未能解决你的问题,请参考以下文章

在谷歌地图v2 android中同时旋转标记组的动画

如何使用LatLng在谷歌地图v2中以公里为单位获取两个地方之间的距离

如何在谷歌地图 api v2 上获取设备的当前位置?

使用 TimerTask 在谷歌地图中设置地图

如何使用 android studio 更改 Google 地图中“我的位置”按钮的位置

在谷歌地图android上显示位置