【问题标题】:Android google maps v2 custom dilogAndroid google maps v2 自定义对话框
【发布时间】:2013-04-22 16:46:34
【问题描述】:

由于现在我无法获得地图 V1 的 API 密钥,我需要将我的代码迁移到 v2。所以我需要的是当用户点击地图上的一个大头针以显示一个对话框(包含点的名称和一个按钮)。如果他点击按钮,我会打开一个显示该地点信息的新活动。我已经使用地图覆盖成功地做到了这一点,我在构造函数中传递了我的自定义数据,并且我拥有了我需要的一切。但是如何使用地图 v2 的标记来完成呢?我找不到有关自定义对话框的任何信息。

【问题讨论】:

  • 如果您有 v1 的应用程序和密钥,您仍然可以使用它。您不必“迁移”。
  • 不,对于这个证书,我没有密钥。这是给一个新客户的,我创建了一个新证书(我不能给我以前的证书)。

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


【解决方案1】:

没有办法用按钮实现InfoWindow,因为Google Map 会自动将其所有内容呈现到图像中。您唯一可以听到的点击是InfoWindow it self。

这里是创建InfoWindow 并分配OnInfoWindowClickListener 的代码,有注释会解释这些步骤。

// 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 OnInfoWindowClickListener
                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;

            }
        });  

【讨论】:

    【解决方案2】:

    自定义对话框和地图本身是两个完全独立的实体,一个的开发不会影响另一个的开发。

    您将使用 OnMarkerClickListener (here the link to the docs),无论何时收到该消息,您都可以像以前一样创建自定义对话框。

    【讨论】:

      【解决方案3】:

      您可以使用

      添加标记
      Marker marker = mMap.addMarker(new MarkerOptions().position(pos1).title("title").snippet("description"));
      

      如果您点击标记,则会打开一个信息窗口,其中包含“标题”和“描述”,甚至您可以通过扩展 InfoWindowAdapter 类来自定义此信息窗口,然后将该 infoWindowAdapter 设置为 GoogleMap obj。欲了解更多信息请参考下面的链接 Google Map API V2

      【讨论】:

        猜你喜欢
        • 2013-04-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-05
        • 1970-01-01
        • 2014-11-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多