代码之家  ›  专栏  ›  技术社区  ›  Jordan Moffat

Android标记点击功能

  •  1
  • Jordan Moffat  · 技术社区  · 11 年前

    我想创建一个方法,当点击标记的信息框/片段时运行。到目前为止,我找不到任何关于这个的东西,只有记号笔。关于在哪里可以找到更多信息,有任何帮助或链接吗?

    2 回复  |  直到 11 年前
        1
  •  7
  •   Emil Adz    11 年前

    要创建一个在单击信息窗口时运行的方法,您需要 setOnInfoWindowClickListener :

     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;
                                        }
                                    }
    
                                    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();
                            }
                        }
                    });
    
        2
  •  1
  •   Matin Petrulak    10 年前

    只需为地图设置OnInfoWindowClickListener即可。

    map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
           //YOUR CODE
            }
        });