【问题标题】:Mapview inside dialog对话框内的地图视图
【发布时间】:2016-03-29 11:14:17
【问题描述】:
MapView sch_map = (MapView) dialog.findViewById(R.id.schedule_map);
            sch_map.onCreate(savedInstanceState);

            sch_map.getMapAsync(new OnMapReadyCallback() {
                @Override
                public void onMapReady(final GoogleMap googleMap) {
                    LatLng sydney = new LatLng(-33.867, 151.206);
                    googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
                }
            });

我正在通过这个在对话框中加载地图,因为地图视图加载速度非常慢,但需要花费很多时间这是我定义地图视图的布局。

<com.google.android.gms.maps.MapView
    android:layout_width="match_parent"
    android:layout_height="300dp"
    map:uiRotateGestures="true"
    map:uiScrollGestures="true"
    map:uiTiltGestures="true"
    map:uiZoomControls="true"
    map:uiZoomGestures="true"
    map:liteMode="true"
    android:id="@+id/schedule_map" />

【问题讨论】:

    标签: android google-maps android-mapview


    【解决方案1】:

    1) 创建一个自定义dialogmap.xml

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
        <com.google.android.gms.maps.MapView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/mapView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            style="@style/MyMaterialTheme.Base">
    
        </com.google.android.gms.maps.MapView>
    </RelativeLayout>
    

    2)在java中定义那个.xml文件

    如果您想在按钮单击时打开对话框。所以在按钮点击事件中编写下面的代码。

     Dialog dialog = new Dialog(getActivity());
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
     dialog.setContentView(R.layout.dialogmap);
     dialog.show();
     GoogleMap googleMap;
    
    
     MapView mMapView = (MapView) dialog.findViewById(R.id.mapView);
     MapsInitializer.initialize(getActivity());          
    
    mMapView = (MapView) dialog.findViewById(R.id.mapView);
    mMapView.onCreate(dialog.onSaveInstanceState());
    mMapView.onResume();// needed to get the map to display immediately
    googleMap = mMapView.getMap();
    

    【讨论】:

    • 你在哪里?
    • 谢谢 :) 救了我 :P
    • 我以同样的方式在对话框中创建了地图视图,但我遇到了一个问题。我的地图显示黑暗然后正常。请查看this问题,如果您有任何解决方案,请回复我。
    • 能否请您提供整个代码,因为我不明白它是如何工作的。谢谢
    【解决方案2】:

    'Com.google.android.gms: play-services-maps: 9.2.0'

    不支持 getMap 但使用 getMapAsync,这是我的代码

    final Dialog dialog = new Dialog(getActivity());
    
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        /////make map clear
    dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    
    dialog.setContentView(R.layout.dialogmap);////your custom content  
    
    MapView mMapView = (MapView) dialog.findViewById(R.id.mapView);
    MapsInitializer.initialize(getActivity());
    
    mMapView.onCreate(dialog.onSaveInstanceState());
    mMapView.onResume();
    
    
        mMapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(final GoogleMap googleMap) {
                LatLng posisiabsen = new LatLng(lat, lng); ////your lat lng
                googleMap.addMarker(new MarkerOptions().position(posisiabsen).title("Yout title"));
                googleMap.moveCamera(CameraUpdateFactory.newLatLng(posisiabsen));
                googleMap.getUiSettings().setZoomControlsEnabled(true);
                googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
            }
        });
    
    
    Button dialogButton = (Button) dialog.findViewById(R.id.btn_tutup);
    // if button is clicked, close the custom dialog
    dialogButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    
    dialog.show();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-04
      • 1970-01-01
      • 1970-01-01
      • 2014-03-25
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 2014-05-09
      相关资源
      最近更新 更多