【问题标题】:Get custom title marker information google maps获取自定义标题标记信息谷歌地图
【发布时间】:2017-02-07 17:52:32
【问题描述】:

您好,我正在使用谷歌地图,我在我拥有的标记上创建了一个自定义标题,我需要知道何时选择了标记我如何获取标题上的信息,因为我需要打开一个片段有了这些信息,让我把我得到的代码贴给你吧:

 @Override
    public void onMapReady(GoogleMap googleMap) {
        map =googleMap;
        map.getUiSettings().setZoomControlsEnabled(true);


        for (Taxi taxi : taxis) {
            u= taxi.getUbicacionActual();
            LatLng ubicacion= new LatLng(Double.parseDouble(u.getLatitud()), Double.parseDouble(u.getLongitud()));


            map.moveCamera(CameraUpdateFactory.newLatLngZoom(ubicacion,15));

            MarkerOptions punto= new MarkerOptions().position(ubicacion);
            map.addMarker(punto);

            map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
                @Override
                public View getInfoWindow(Marker marker) {

                    return null;
                }

                @Override
                public View getInfoContents(Marker marker) {
                    View v = getLayoutInflater().inflate(R.layout.info_title, null);


                    TextView info1 = (TextView) v.findViewById(R.id.info1);
                    TextView info2 = (TextView) v.findViewById(R.id.info2);
                    TextView info3 = (TextView) v.findViewById(R.id.info3);

                    info1.setText("Fecha: " + u.getFecha());
                    info3.setText("Longitud: " + u.getLongitud().toString());

                    info2.setText("Ubicacion: "+obtenerDireccion(u.getLatitud(),u.getLongitud()));

                    return v;
                }
            });


        }
        map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {

                Fragment fragmento;
                fragmento = new HistorialFragment();
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.content_principal, fragmento)
                        .commit();


            }
        });

在我调用新片段的方法 OnInfoWindowClickListener 上,我需要从自定义标题发送信息,有人可以帮我吗?

【问题讨论】:

    标签: android maps


    【解决方案1】:

    试试这个:

    在getInfoContents(Marker marker) 方法中添加您的setOnInfoWindowClickListener:

    map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
            @Override
            public View getInfoWindow(Marker marker) {
    
                return null;
            }
    
            @Override
            public View getInfoContents(Marker marker) {
                View v = getLayoutInflater().inflate(R.layout.info_title, null);
    
    
                final TextView info1 = (TextView) v.findViewById(R.id.info1);
                TextView info2 = (TextView) v.findViewById(R.id.info2);
                TextView info3 = (TextView) v.findViewById(R.id.info3);
    
                info1.setText("Fecha: " + u.getFecha());
                info3.setText("Longitud: " + u.getLongitud().toString());
    
                info2.setText("Ubicacion: "+obtenerDireccion(u.getLatitud(),u.getLongitud()));
    
                map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
                    public void onInfoWindowClick(Marker marker)
                    {
                        Fragment fragmento;
                        fragmento = new HistorialFragment();
                        Bundle bundle = new Bundle();
                        bundle.putString("title",info1.getText().toString());
                        fragmento.setArguments(bundle);
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.content_principal, fragmento)
                                .commit();
                    }
                });
    
                return v;
            }
        });
    

    【讨论】:

      【解决方案2】:

      如果你设置了Marker的标题做

      MarkerOptions punto= new MarkerOptions()
          .position(ubicacion)
          .title("Fecha: " + u.getFecha()); // set the marker title
      map.addMarker(punto);
      

      您可以在 onInfoWindowClick 方法中检索它:

      map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
          @Override
          public void onInfoWindowClick(Marker marker) {
              String title = marker.getTitle(); // Retrieve the title
          }
      });
      

      【讨论】:

      • 不,我没有,我创建了一个带有 3 个文本字段的自定义窗口,我需要其中一个的信息,如果您签入代码,我需要信息表单:info1.setText("Fecha: " + u.getFecha());
      • 我已经编辑了我的答案。您仍然可以设置标记的标题并在您的 onInfoWindowClick 上使用它
      • 但是它在方法上创建的标记信息:public View getInfoContents(Marker marker),我没有使用.title,因为我有一个自定义窗口标题视图。
      猜你喜欢
      • 2019-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-21
      相关资源
      最近更新 更多