简体   繁体   English

群集不适用于Android的谷歌地图。 我究竟做错了什么?

[英]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.

相关问题 在Android中扩展EditText。 我究竟做错了什么? - Extending a EditText in Android. What am I doing wrong? Google Maps V2不适用于我,我在做什么错? - Google Maps V2 isn't working for me, what am I doing wrong? 制作一个简单的计数器,该计数器在android上每秒递增。 我在做什么错? - Making a simple counter that increments every second on android. What wrong am I doing? 为什么通知组在android中不起作用? 我做错了什么? - Why Notification group not working in android? what I am doing wrong? android中的HttpGet ..我在做什么错? - HttpGet in android .. what am I doing wrong? Android Fragments-我在做什么错 - Android Fragments - What am I doing wrong 在google maps活动中授予权限后,我无法立即获取位置。我在做什么错? - I cant get my location immediately after having given the permissions in google maps activity.What am i doing wrong? 我想将视图添加到Android LinearLayout,但无法正常工作。 我究竟做错了什么? - I want to add views to an Android LinearLayout, but it is not working. What am I doing wrong? Android Google App邀请示例未发送电子邮件和消息,我在做什么错? - Android Google App Invite sample is not sending email and message,What i am doing wrong? 我的活动没有任何进展。 我究竟做错了什么? - Nothing is working on my activity. What am I doing wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM