【问题标题】:Google Maps Api v2 - Multiple markers with their own infowindowGoogle Maps Api v2 - 具有自己的信息窗口的多个标记
【发布时间】:2013-03-29 00:01:55
【问题描述】:

我正在尝试在地图上添加多个标记,每个标记都有自己的信息窗口(我需要信息窗口在多行上显示一些信息),但我不明白该怎么做。 我已经阅读了 Google 文档,现在我正在寻找示例代码或教程。 谁能帮帮我?

【问题讨论】:

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


    【解决方案1】:

    看看这段代码,有解释我的行为的 cmets。但这会从预定义的 XML 布局中为您创建一个 InfoWindow,您必须在运行时创建一个填充。

    接下来,我检查当前点击的标记InfoWindow 是否属于指示我的位置的标记,如果是,我提供正确的Toast,如果不是,我让用户使用另一个 Activity 导航到该位置:

            // Setting a custom info window adapter for the google map
            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());
    
                        //Setting InfoWindow click listener
                    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;
                                        }
                                    }
                                    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;
    
                }
            });  
    

    【讨论】:

    • 为什么每次弹出信息窗口时都要设置 OnInfoWindowClickListener?在设置 InfoWindowAdapter 的同时执行一次就足够了。
    • 其实这不是问题的一部分,可以忽略,但我猜你是对的......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-13
    • 2011-06-22
    • 2011-02-02
    • 2018-04-02
    • 2011-10-26
    相关资源
    最近更新 更多