【发布时间】:2017-10-30 09:46:01
【问题描述】:
我的项目有 2 个片段(2 个选项卡),每个片段包含 2 个 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));
}
});
}
}
【问题讨论】:
-
我相信如果您只使用一个
MapView并更新您显示的信息会更好。除此之外,如果您想继续使用两个您应该将它们视为View和根据您要显示的内容更改可见性
标签: android google-maps android-mapview