[英]Clustering not working in google maps for android. What am i doing wrong?
Here is my code to render 100 markers around a given location, but no matter what i do, the clusters/markers wont show up. 这是我在给定位置周围渲染100个标记的代码,但无论我做什么,簇/标记都不会出现。 I have a doubt the if the issue is with the initialization of ClusterManager,
mClusterManager = new ClusterManager<MarkerManager>(getActivity(), mMap);
我怀疑是否问题是ClusterManager的初始化,
mClusterManager = new ClusterManager<MarkerManager>(getActivity(), mMap);
From the example google shown here , ClusterManager
is initialized as mClusterManager = new ClusterManager<MyItem>(this, getMap());
从此处显示的示例google,
ClusterManager
初始化为mClusterManager = new ClusterManager<MyItem>(this, getMap());
But my class inherits from fragment
, is the getActivity
return the wrong Context? 但是我的类继承自
fragment
,是getActivity
返回错误的Context吗? i think it expects com.example.mkallingal.mapapp3.MainActivity.PlaceholderFragment
我认为它期望
com.example.mkallingal.mapapp3.MainActivity.PlaceholderFragment
Your help is much appreciated and i will mark as best answer if you were to help me. 非常感谢您的帮助,如果您能帮助我,我会将其标记为最佳答案。 Thank you.
谢谢。
below is my code: 下面是我的代码:
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;
}
}
}
whoops, my bad, the event handlers where attached before the initialization of the ClusterManager, it should be like, 哎呀,我的坏,在ClusterManager初始化之前附加的事件处理程序,应该是这样的,
mClusterManager = new ClusterManager<MarkerManager>(getActivity(), mMap);
mMap.setOnCameraChangeListener(mClusterManager);
mMap.setOnMarkerClickListener(mClusterManager);
Now it works... 现在它有效......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.