如何在其他人之上显示Google Maps Android Marker?

时间:2013-04-17 08:05:20

标签: android google-maps google-maps-markers google-maps-android-api-2

我的应用程序中有多个标记,有时它们中的一些处于相同的位置。我希望其中一些标记始终显示在其他标记的前面。我该怎么办?

提前谢谢。

4 个答案:

答案 0 :(得分:2)

marker.setZIndex(Float.MAX_VALUE)

答案 1 :(得分:0)

您应该以某种方式对它们进行排序,并且适配器中的最后一个将位于顶部。

答案 2 :(得分:0)

选择您希望始终可见的那些,并将它们最后添加到地图中。

答案 3 :(得分:0)

我认为这是错误的做法。即使您将一个标记放在另一个标记之前,用户仍有可能单击后面的标记。

我不会这样做,在向地图添加新的Marker时,检查新添加的Marker位置是否已有Marker,如果是,则添加新数据到当前Marker并且没有添加另一个。

这样,相同的标记将代表两个指定的位置。

更新:

看看这段代码,我检查当前点击的标记是否是我当前的位置。 这样我就知道我是否应该开启方向活动。您可以使用相同的技术来确定您是否已在特定位置使用标记。

 map.setInfoWindowAdapter(new InfoWindowAdapter() {

            // Use default InfoWindow frame
            @Override
            public View getInfoWindow(Marker args) {
                return null;
            }

            // Defines the contents of the InfoWindow
            @Override
            public View getInfoContents(Marker args) {

                // Getting view from the layout file info_window_layout
                View v = getLayoutInflater().inflate(R.layout.info_window_layout, null);

                // Getting the position from the marker
                clickMarkerLatLng = args.getPosition();

                TextView title = (TextView) v.findViewById(R.id.tvTitle);
                title.setText(args.getTitle());

                map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {          
                    public void onInfoWindowClick(Marker marker) 
                    {
                        if (SGTasksListAppObj.getInstance().currentUserLocation!=null)
                        {   
                            if (String.valueOf(SGTasksListAppObj.getInstance().currentUserLocation.getLatitude()).substring(0, 8).contains(String.valueOf(clickMarkerLatLng.latitude).substring(0, 8)) &&
                                    String.valueOf(SGTasksListAppObj.getInstance().currentUserLocation.getLongitude()).substring(0, 8).contains(String.valueOf(clickMarkerLatLng.longitude).substring(0, 8)))
                            {
                                Toast.makeText(getApplicationContext(), "This your current location, navigation is not needed.",  Toast.LENGTH_SHORT).show();
                            }
                            else
                            {
                                FlurryAgent.onEvent("Start navigation window was clicked from daily map");
                                tasksRepository = SGTasksListAppObj.getInstance().tasksRepository.getTasksRepository();
                                for (Task tmptask : tasksRepository)
                                {
                                    String tempTaskLat = String.valueOf(tmptask.getLatitude());
                                    String tempTaskLng = String.valueOf(tmptask.getLongtitude());

                                    Log.d(TAG, String.valueOf(tmptask.getLatitude())+","+String.valueOf(clickMarkerLatLng.latitude).substring(0, 8));

                                    if (tempTaskLat.contains(String.valueOf(clickMarkerLatLng.latitude).substring(0, 8)) && tempTaskLng.contains(String.valueOf(clickMarkerLatLng.longitude).substring(0, 8)))
                                    {  
                                        task = tmptask;
                                        break;
                                    }
                                }
                                /*
                                findDirections(SGTasksListAppObj.getInstance().currentUserLocation.getLatitude(),
                                               SGTasksListAppObj.getInstance().currentUserLocation.getLongitude(),
                                               clickMarkerLatLng.latitude, clickMarkerLatLng.longitude, GMapV2Direction.MODE_DRIVING );
                                */
                                Intent intent = new Intent(getApplicationContext() ,RoadDirectionsActivity.class);
                                intent.putExtra(TasksListActivity.KEY_ID, task.getId());
                                startActivity(intent);

                            }
                        }
                        else
                        {
                            Toast.makeText(getApplicationContext(), "Your current location could not be found,\nNavigation is not possible.",  Toast.LENGTH_SHORT).show();
                        }
                    }
                });

                // Returning the view containing InfoWindow contents
                return v;

            }
        });