Android 如何以编程方式显示/隐藏google mapview?
我的项目有2个片段(2个选项卡),每个片段上包含2个MapView。我想以编程方式显示/隐藏google mapview。怎么做? 下面是我的代码Android 如何以编程方式显示/隐藏google mapview?,android,google-maps,android-mapview,Android,Google Maps,Android Mapview,我的项目有2个片段(2个选项卡),每个片段上包含2个MapView。我想以编程方式显示/隐藏google mapview。怎么做? 下面是我的代码 <com.google.android.gms.maps.MapView android:id="@+id/mapView" android:layout_width="fill_parent" android:layout_height="200dp"
<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"/>
我的要求是,当重要性在家时,我想显示第一个谷歌地图,否则将其隐藏;当重要性在工作时,我想显示第二个谷歌地图,否则将其隐藏
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));
}
});
}
}
for(int i=0;i
试试这个
yourMapView.setVisibility(View.GONE);// to hide
并展示
yourMapView.setVisibility(View.VISIBLE);// TO SHOW
试试这个
mMapView.setVisibility(View.INVISIBLE); //To INVISIBLE
在java代码中
布局_2.setVisibility(View.VISIBLE);//显示线性布局
布局_2.setVisibility(View.GONE);//隐藏线性布局
布局_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>
我相信如果您只使用一个
MapView
并更新您显示的信息会更好。。除此之外,如果您想继续使用两个,则应该将它们视为<代码>视图< /代码>,并根据要显示的内容更改可见性。
<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>