2017-04-01 4 views
1

맵보기에서 특정 마커의 'setInfoWindowAdapter'프라퍼티를 사용하려하지만 '마커 2'와 함께 어떤 이유로 든 제목 아래에 원치 않는 공백이 나타나는 것 같습니다 (마커 2) (첨부 스크린 샷 참조). 이런 일이 발생하지 않도록하기 위해 할 수있는 일은 무엇입니까?특정 마커의 경우에만 InfoWindowAdapter를 설정하는 방법

FragmentCustomMapview.java

public class FragmentCustomMapview extends android.support.v4.app.Fragment implements OnMapReadyCallback { 

    public FragmentCustomMapview() { 
     // Required empty constructor 
    } 

    GoogleMap mGoogleMap; 
    MapView mMapView; 
    SwitchCompat swt; 

    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.fragment_custommapview, container, false); 

     mMapView = (MapView) v.findViewById(R.id.map_custommapview); 
     mMapView.onCreate(savedInstanceState); 
     mMapView.getMapAsync(this); //this is important 

     swt = (SwitchCompat) v.findViewById(R.id.switch_map_custommapview); 

     return v; 
    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mGoogleMap = googleMap; 
     mGoogleMap.getUiSettings().setZoomControlsEnabled(true); 
     mGoogleMap.setBuildingsEnabled(true); 
     mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 

     swt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       initMap(isChecked); 
      } 
     }); 

     initMap(swt.isChecked()); 

     // Add markers and move the camera 
     LatLng marker1 = new LatLng(51.510256, -0.135106); 
     mGoogleMap.addMarker(new MarkerOptions() 
       .position(marker1) 
       .title("Lorem ipsum dolor") 
       .snippet("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.") 
     ); 

     LatLng marker2 = new LatLng(51.509793, -0.134961); 
     mGoogleMap.addMarker(new MarkerOptions() 
       .position(marker2) 
       .title("Marker 2") 
     );  


     mGoogleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() { 
      @Override 
      public View getInfoWindow(Marker arg0) { 
       return null; 
      } 

      @Override 
      public View getInfoContents(Marker marker) { 

       Context mContext = getActivity(); 

       LinearLayout info = new LinearLayout(mContext); 
       info.setOrientation(LinearLayout.VERTICAL); 

       TextView title = new TextView(mContext); 
       title.setTextColor(Color.BLACK); 
       title.setGravity(Gravity.CENTER); 
       title.setTypeface(null, Typeface.BOLD); 
       title.setText(marker.getTitle()); 
       title.setSingleLine(false); 

       TextView snippet = new TextView(mContext); 
       snippet.setTextColor(Color.GRAY); 
       snippet.setText(marker.getSnippet()); 
       snippet.setMultiline(false) 

       info.addView(title); 
       info.addView(snippet); 

       return info; 
      } 
     }); 

     // Updates the location and zoom level of the MapView 
     CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(51.509932, -0.134720), 18); 
     mGoogleMap.animateCamera(cameraUpdate); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     mMapView.onResume(); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     mMapView.onPause(); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     mMapView.onDestroy(); 
    } 

    @Override 
    public void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     mMapView.onSaveInstanceState(outState); 
    } 

    @Override 
    public void onLowMemory() { 
     super.onLowMemory(); 
     mMapView.onLowMemory(); 
    } 

    private void initMap(boolean isChecked){ 
     if (isChecked) { 
      mGoogleMap.setMapStyle(new MapStyleOptions(getResources().getString(R.string.style_json))); 
     } else { 
      mGoogleMap.setMapStyle(null); 
     } 
    } 
} 

1 마커

enter image description here

2 마커

enter image description here

답변

1

추가하지 마십시오에 TextView snippet 당신의 LinearLayout info 당신의 Marker에는 미리보기가없는 경우 :

@Override 
public View getInfoContents(Marker marker) { 

    Context mContext = getActivity(); 

    LinearLayout info = new LinearLayout(mContext); 
    info.setOrientation(LinearLayout.VERTICAL); 

    TextView title = new TextView(mContext); 
    title.setTextColor(Color.BLACK); 
    title.setGravity(Gravity.CENTER); 
    title.setTypeface(null, Typeface.BOLD); 
    title.setText(marker.getTitle()); 
    title.setSingleLine(false); 

    info.addView(title); 

    if (marker.getSnippet() != null) { 
     TextView snippet = new TextView(mContext); 
     snippet.setTextColor(Color.GRAY); 
     snippet.setText(marker.getSnippet()); 
     snippet.setSingleLine(false); 

     info.addView(snippet); 
    } 

    return info; 
}