【问题标题】:How to show/hide google mapview programatically?如何以编程方式显示/隐藏谷歌地图视图?
【发布时间】: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


【解决方案1】:

在java代码中

layout_2.setVisibility(View.VISIBLE); // 显示线性布局

layout_2.setVisibility(View.GONE); // 隐藏线性布局

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>

【讨论】:

    【解决方案2】:

    试试这个

    yourMapView.setVisibility(View.GONE);// to hide
    

    并显示

    yourMapView.setVisibility(View.VISIBLE);// TO SHOW
    

    【讨论】:

      【解决方案3】:

      试试这个

       mMapView.setVisibility(View.INVISIBLE); //To INVISIBLE
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-10
        • 2013-04-28
        • 2017-05-04
        • 1970-01-01
        • 2011-04-16
        • 1970-01-01
        相关资源
        最近更新 更多