Fragment Ratio 1: 1 for Android Google Map (same height and width)
I am trying to create google maps with 1: 1 aspect ratio so that the height is the same as the width. This is my current code:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/border"
android:orientation="vertical"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Locatiegegevens"
android:textStyle="bold" />
...
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Woonplaats:" />
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/mapPreview"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
map:cameraZoom="13"
map:liteMode="true"
map:mapType="normal" />
</LinearLayout>
Fragment class:
public class CaseFragment extends Fragment implements OnMapReadyCallback {
private GoogleMap mMap;
private Case caseObj;
private TextView title;
public CaseFragment() {
// Required empty public constructor
}
public static CaseFragment newInstance(Case caseObj) {
CaseFragment caseFragment = new CaseFragment();
Bundle args = new Bundle();
args.putSerializable("case", caseObj);
caseFragment.setArguments(args);
return caseFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle args = getArguments();
caseObj = (Case) args.getSerializable("case");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_case, container, false);
title = (TextView) view.findViewById(R.id.txtTitle);
title.setText(caseObj.getId());
MapFragment mapFragment = (MapFragment) getChildFragmentManager().findFragmentById(R.id.mapPreview);
mapFragment.getMapAsync(this);
return view;
}
@Override
public void onMapReady(GoogleMap googleMap) {
LatLng sydney = new LatLng(-33.852, 151.211);
googleMap.addMarker(new MarkerOptions().position(sydney)
.title("Marker in Sydney"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
I already tried to use LayoutParams
, but for some reason the width was the same as the height:
ViewGroup.LayoutParams params = mMapFragment.getView().getLayoutParams();
params.height = params.width;
mMapFragment.getView().setLayoutParams(params);
+3
source to share
1 answer