onMarkerClick事件不适用于ClusterManager

时间:2018-03-05 23:03:30

标签: android google-maps

我知道在同一类别中还有一些其他问题,但没有解决我的问题。

我正在使用ClusterManager使用Google Maps API指南(https://developers.google.com/maps/documentation/android-api/utility/marker-clustering)对Markers进行聚类。

问题:我在ClusterManager对象上使用addItem(),传递位置以及Title和Snippet。但是,当我点击标记时,没有任何内容出现。我已经简化了代码以找出问题但无法解决此问题。

这是我的简化代码:

MyItem.java:

package com.example.android.testmarker.model;

import com.google.android.gms.maps.model.LatLng;
import com.google.maps.android.clustering.ClusterItem;

public class MyItem implements ClusterItem {
private final LatLng mPosition;
private String mTitle;
private String mSnippet;

public MyItem(double lat, double lng) {
    mPosition = new LatLng(lat, lng);
}

public MyItem(double lat, double lng, String title, String snippet) {
    mPosition = new LatLng(lat, lng);
    mTitle = title;
    mSnippet = snippet;
}

@Override
public LatLng getPosition() { return mPosition;}

public String getTitle() { return mTitle;}

public String getSnippet() { return mSnippet; }
}

MarkerclusterActivity.java:

package com.example.android.testmarker;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.example.android.testmarker.model.MyItem;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.maps.android.clustering.ClusterItem;
import com.google.maps.android.clustering.ClusterManager;


public class MarkerclusterActivity extends AppCompatActivity implements OnMapReadyCallback {

    private final String TAG = this.getClass().getSimpleName();
    GoogleMap mMap;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        Log.v(TAG, "onCreated called");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_map);

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

    }

    @Override
    public void onMapReady(GoogleMap map) {
        Log.v(TAG, "onMapReady callback triggered");
        mMap = map;

        //restricting users panning to Vancouver dt area. 
        LatLngBounds limit = new LatLngBounds(new LatLng( 49.268642, -123.148639),
                new LatLng( 49.300045, -123.095893));
        //Add limit to our map
        mMap.setLatLngBoundsForCameraTarget(limit);

        mMap.setMinZoomPreference(13.0f);

        //Set the initial camera to the center of downtown (where it says Vancouver)
        LatLng initialLocation =  new LatLng( 49.282733f, -123.120732f);
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(initialLocation, 13.0f));

        mClusterManager = new ClusterManager<MyItem>(this, map);
        mMap.setOnCameraIdleListener(mClusterManager);
        mMap.setOnMarkerClickListener(mClusterManager);

        // Add cluster items (markers) to the cluster manager.        
        addItems();
    }

    private void addItems(){
        mClusterManager.addItem(new MyItem(49.2831205, -123.1107811,
                "Location54", "Garbage"));
        mClusterManager.addItem(new MyItem(49.2839704, -123.1116479,
                "Location 55", "Garbage"));
        mClusterManager.addItem(new MyItem(49.2831205, -123.1135884,
                "Location 58", "Garbage"));
        mClusterManager.addItem(new MyItem(49.2816015, -123.1101502,
                "Location 60", "Garbage"));
        mClusterManager.addItem(new MyItem(49.2810562, -123.1083306,
                "Location 69", "Garbage"));

        //Add some locations a bit further from the ones above
        mClusterManager.addItem(new MyItem( 49.283200, -123.126951));
        mClusterManager.addItem(new MyItem( 49.283633, -123.136453));
        mClusterManager.addItem(new MyItem( 49.285686, -123.130786));

    }
}

现在当我点击标记时,相机以标记为中心,默认的GoogleMaps按钮(方向,googlemaps按钮)显示在右下方(表示触发了标记点击事件);但是,它从不显示Title / Snippet。

注意:我在谷歌地图API的演示应用程序中试过这个,它可以在不添加任何其他内容的情况下运行;但是,由于他们的演示使用的方法太多,我无法确定问题所在。

提前致谢

2 个答案:

答案 0 :(得分:0)

一切正常。 代码没有错;在尝试了所有可能的场景之后,我只是从头开始创建了一个新的应用程序并添加了代码,并且它有效。

答案 1 :(得分:0)

<块引用>
Here is how I did it: 
KOTLIN version
//adding items to cluster 
        mCustomerClusterManager.addItems(customerAroundMeItems)
        mEquipmentClusterManager.addItems(equipmentAroundMeItems)
override fun onMapReady(googleMap: GoogleMap?) {
 this.googleMap = googleMap

    //Step 1
    
        val markerManager = MarkerManager(googleMap) // your googleMap instance
    
     
    //Step 2


    mEquipmentClusterManager = ClusterManager(activity, googleMap,  markerManager)
    mCustomerClusterManager = ClusterManager(activity, googleMap, markerManager)

    googleMap.setOnMarkerClickListener(markerManager)
    googleMap.setOnCameraIdleListener(this)

    //Step 3
    
    val rendererCustomer =CustomerAroundMeMapPinsCluster(activity,googleMap, mCustomerClusterManager)
    
    val rendererEquipmentRental = EquipmentAroundMeMapPinsCluster(activity,googleMap,mEquipmentClusterManager)

    //Step 4
    
       

     mCustomerClusterManager.setOnClusterClickListener {
                    Toast.makeText(context, "Customer Cluster click", Toast.LENGTH_SHORT).show()
                    onClusterClick(it)
                    return@setOnClusterClickListener false
                }
    mCustomerClusterManager.setOnClusterItemClickListener {
                Toast.makeText(context, "Customer item click", Toast.LENGTH_SHORT).show()
                
                return@setOnClusterItemClickListener false
            }

    mEquipmentClusterManager.setOnClusterClickListener {
            Toast.makeText(context, "Equipment Cluster click", Toast.LENGTH_SHORT).show()
            return@setOnClusterClickListener true
        }
        mEquipmentClusterManager.setOnClusterItemClickListener {
            // there is a ticket for this
            Toast.makeText(context, "Equipment item clicked", Toast.LENGTH_SHORT).show()
            
            return@setOnClusterItemClickListener false
        }
 

    mCustomerClusterManager.renderer = rendererCustomer
    mEquipmentClusterManager.renderer = rendererEquipmentRental


}


     override fun onCameraIdle() {
            mEquipmentClusterManager.cluster()
            mCustomerClusterManager.cluster()
    
        }