android - Google Maps Api v2 - 具有自己的信息窗口的多个标记

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

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

最佳答案

看看这段代码,有解释我的行为的注释。但这将从预定义的 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;

            }
        });  

关于android - Google Maps Api v2 - 具有自己的信息窗口的多个标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15853119/

相关文章:

android - 将 Libgdx 项目导出到 Android

android - 使用wifi ip定位

java - Android PlaceAutocompleteFragment自动关闭

java - 使用 Marker 对象外部的数据为 Marker 创建自定义信息窗口

android - 如何在我的 Activity 中显示弹出窗口?

Excel VBA 和 Google map

java - 带有 Android 的 Google Cloud Endpoints - 缓存响应?

java - android:onClick="@drawable/... 不更新按钮 onClick

javascript - 谷歌地图 JS v3 : Map display: none; after map initialization causing corrupted map

javascript - 使用 Javascript/JQuery 使用 Ajax 加载 Google Maps API,回调设置不正确