这是我在给定位置周围渲染 100 个标记的代码,但无论我做什么,集群/标记都不会出现。我怀疑问题是否与 ClusterManager 的初始化有关,mClusterManager = new ClusterManager<MarkerManager>(getActivity(), mMap);
从示例谷歌显示 here , ClusterManager
被初始化为 mClusterManager = new ClusterManager<MyItem>(this, getMap());
但是我的类继承自 fragment
,是不是 getActivity
返回了错误的上下文?我认为它需要 com.example.mkallingal.mapapp3.MainActivity.PlaceholderFragment
非常感谢您的帮助,如果您要帮助我,我会将其标记为最佳答案。谢谢你。
下面是我的代码:
public static class PlaceholderFragment extends Fragment
{
public GoogleMap mMap;
private ClusterManager<MarkerManager> mClusterManager;
Context _MapContext;
private void setUpMapIfNeeded(View RootView) {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
FragmentActivity activity = (FragmentActivity)RootView.getContext();
FragmentManager manager = activity.getSupportFragmentManager();
Fragment x= manager.findFragmentById(R.id.container);
Fragment _ChildFragment= x.getChildFragmentManager().findFragmentById(R.id.map);
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) _ChildFragment)
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
clusterUp(5.293842, 101.682636);
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
_MapContext= container.getContext();
setUpMapIfNeeded(rootView);
return rootView;
}
private void clusterUp(double lat, double lng)
{
LatLng SYDNEY = new LatLng(5.415694, 101.678282);
LatLng MOUNTAIN_VIEW = new LatLng(lat,lng);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(SYDNEY, 15));
mMap.animateCamera(CameraUpdateFactory.zoomIn());
mMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(MOUNTAIN_VIEW) // Sets the center of the map to Mountain View
.build(); // Creates a CameraPosition from the builder
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
mMap.setOnCameraChangeListener(mClusterManager);
mMap.setOnMarkerClickListener(mClusterManager);
mClusterManager = new ClusterManager<MarkerManager>(getActivity(), mMap);
for (int i=0;i<100;i++){
LatLng _NewLocation= getLocationNearCords(5.415694, 101.678282, 1000); //randomly generate 100 cordinates around the location
MarkerManager offsetItem = new MarkerManager(_NewLocation.latitude,_NewLocation.longitude);
mClusterManager.addItem(offsetItem);
}
}
public class MarkerManager implements ClusterItem {
private LatLng mPosition = new LatLng(22.2222, 33.3333);
public MarkerManager() {
}
public MarkerManager(double lat, double lng) {
mPosition = new LatLng(lat, lng);
}
@Override
public LatLng getPosition() {
return mPosition;
}
}
}
最佳答案
哎呀,我的错,在 ClusterManager 初始化之前附加的事件处理程序应该是这样的,
mClusterManager = new ClusterManager<MarkerManager>(getActivity(), mMap);
mMap.setOnCameraChangeListener(mClusterManager);
mMap.setOnMarkerClickListener(mClusterManager);
现在它起作用了...
关于android - 聚类在 android 的谷歌 map 中不起作用。我究竟做错了什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30636509/