2016-09-06 46 views
0

我想在片段中显示MapView。地图显示为我所期望的,但它似乎只是作为图片显示。我无法拖动移动相机或传播/捏缩放。我也尝试在我的片段中使用SupportMapFragment,但得到了相同的结果。Android MapView在初始化后的片段冻结(不可点击或拖动)

我在我的Activity中有一个NavigationView。我不认为NavigationView和MapView触摸事件存在冲突。

以前有人遇到过这个吗?

片段

public class CustomizedMapFragment extends Fragment implements OnMapReadyCallback{ 
    MapView mapView; 
    GoogleMap googleMap; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_map, container, false); 

     mapView = (MapView) rootView.findViewById(R.id.mapView); 
     mapView.onCreate(savedInstanceState); 
     mapView.getMapAsync(this); 
     return rootView; 
    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     this.googleMap = googleMap; 
     this.googleMap.getUiSettings().setZoomControlsEnabled(true); 
     this.googleMap.getUiSettings().setZoomGesturesEnabled(true); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     mapView.onResume(); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     mapView.onPause(); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     mapView.onDestroy(); 
    } 

    @Override 
    public void onLowMemory() { 
     super.onLowMemory(); 
     mapView.onLowMemory(); 
    } 
} 

布局

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <com.google.android.gms.maps.MapView 
     android:layout_margin="16dp" 
     android:id="@+id/mapView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

</RelativeLayout> 
+0

看看这里的答案,它可能会有所帮助:http://stackoverflow.com/questions/5758081/mapview-works-incorrect –

+0

@DanielNugent我试图设置clickable为true,但它不起作用 – DemonPilot

+0

Did你实现了'OnMapClickListener'? –

回答

0

尝试使用这个UiSettings.setScrollGesturesEnabled(boolean),利用这一点,你可以用手指拖动地图滚动(PAN)在地图各处。

另一种方式是在通过XML属性创建地图时配置这些选项。

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:map="http://schemas.android.com/apk/res-auto" 
    android:name="com.google.android.gms.maps.SupportMapFragment" 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    map:cameraBearing="112.5" 
    map:cameraTargetLat="-33.796923" 
    map:cameraTargetLng="150.922433" 
    map:cameraTilt="30" 
    map:cameraZoom="13" 
    map:mapType="normal" 
    map:uiCompass="false" 
    map:uiRotateGestures="true" 
    map:uiScrollGestures="false" 
    map:uiTiltGestures="true" 
    map:uiZoomControls="false" 
    map:uiZoomGestures="true"/> 

但是为了在XML布局文件中使用这些自定义属性,必须先添加以下命名空间声明。

xmlns:map="http://schemas.android.com/apk/res-auto" 

欲了解更多信息,请检查该documentation