我的项目有2个片段(2个标签),每个片段包含2个MapView。我想以编程方式显示/隐藏谷歌mapview。怎么做? 以下是我的代码。
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"/>
<com.google.android.gms.maps.MapView
android:id="@+id/mapView1"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"/>
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_month, container, false);
mMapView = (MapView) view.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume(); // needed to get the map to display immediately
mMapView1 = (MapView) view.findViewById(R.id.mapView1);
mMapView1.onCreate(savedInstanceState);
mMapView1.onResume(); // needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
}
我的要求是当重要性在家时我想显示第一个谷歌地图否则隐藏它,当重要性工作时我想显示第二个谷歌地图否则隐藏它。
for (int i = 0; i < subArray.length(); i++) {
JSONObject ingredObject = subArray.getJSONObject(i);
JSONObject defObject = ingredObject.getJSONObject("place");
final String name = defObject.getString("name");
final String significance = ingredObject.getString("significance");
final String latitude = ingredObject.getString("latitude");
final String longitude = ingredObject.getString("longitude");
if (significance.equals("home")) {
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;
boolean success = googleMap.setMapStyle(new MapStyleOptions(getResources()
.getString(R.string.style_json)));
if (!success) {
Log.e(TAG, "Style parsing failed.");
}
// For dropping a marker at a point on the Map
LatLng home = new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude));
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(Double.parseDouble(latitude), Double.parseDouble(longitude), 1);
} catch (IOException e) {
e.printStackTrace();
}
String city = addresses.get(0).getLocality();
String country = addresses.get(0).getCountryName();
Marker marker3 = googleMap.addMarker(new MarkerOptions().position(home).title(significance).snippet(getString(R.string.home_location)));
marker3.showInfoWindow();
// For zooming automatically to the location of the marker
CameraPosition cameraPosition = new CameraPosition.Builder().target(home).zoom(17).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
});
}
if (significance.equals("work")) {
mMapView1.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;
boolean success = googleMap.setMapStyle(new MapStyleOptions(getResources()
.getString(R.string.style_json)));
if (!success) {
Log.e(TAG, "Style parsing failed.");
}
// For dropping a marker at a point on the Map
LatLng home = new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude));
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(Double.parseDouble(latitude), Double.parseDouble(longitude), 1);
} catch (IOException e) {
e.printStackTrace();
}
String city = addresses.get(0).getLocality();
String country = addresses.get(0).getCountryName();
Marker marker4 = googleMap.addMarker(new MarkerOptions().position(home).title(name).snippet(getString(R.string.work_location)));
marker4.showInfoWindow();
// For zooming automatically to the location of the marker
CameraPosition cameraPosition = new CameraPosition.Builder().target(home).zoom(17).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
});
}
}
答案 0 :(得分:1)
在java代码中
layout_2.setVisibility(View.VISIBLE); //显示LinearLayout
layout_2.setVisibility(View.GONE); //隐藏LinearLayout
对于layout_1
也是如此在xml代码中
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/layout_2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/layout_2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.gms.maps.MapView
android:id="@+id/mapView1"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"/>
</LinearLayout>
</LinearLayout>
答案 1 :(得分:0)
试试这个
yourMapView.setVisibility(View.GONE);// to hide
并显示
yourMapView.setVisibility(View.VISIBLE);// TO SHOW
答案 2 :(得分:0)
试试这个
mMapView.setVisibility(View.INVISIBLE); //To INVISIBLE