2017-02-07 83 views
1

嗨我正在與谷歌地圖工作,我創建了一個自定義標題,我有標記,我需要知道什麼時候選擇標記我如何獲得標題的信息, becouse我需要打開一個片段與信息,letme粘貼你給我的代碼:獲取自定義標題標記信息穀歌地圖

@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,我稱之爲新的片斷,我需要從自定義標題發送信息,可能有人請幫助我呢?

回答

1

試試這個:

添加您setOnInfoWindowClickListenergetInfoContents(Marker marker)方法:

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; 
     } 
    }); 
+0

非常感謝很多人現在工作:) –

0

如果設置了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 
    } 
}); 
+0

不,我創建了一個帶有3個文本框的自定義窗口,並且我需要其中一個的信息,如果您檢查代碼,我需要信息窗體:info1.setText(「Fecha:」+ u.getFecha ()); –

+0

我編輯了我的答案。您仍然可以設置標記的標題,並將其用於onInfoWindowClick' – antonio

+0

,但標記信息在方法上創建:public View getInfoContents(Marker marker),即時消息不使用.title,becouse我有一個自定義窗口標題視圖。 –