[英]Android MapView only scrolling vertically
我的問題:我只能在MapView中上下滾動,而不能左右滾動。
我的LinearLayout中有一個MapView,由嵌套滾動視圖保存。 所以我的layout.xml看起來像這樣:
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
...
<com.google.android.gms.maps.MapView
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_margin="10dp"
android:id="@+id/jd_map_view"/>
...
</LinearLayout>
我正在片段中初始化MapView,由TabLayout持有,如下所示:
mapView = (MapView) mRootView.findViewById(R.id.jd_map_view);
mapView.onCreate(savedInst);
mapView.onResume();
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
LatLng latlng = new LatLng(mLat, mLng);
googleMap.addMarker(new MarkerOptions().position(latlng));
CameraUpdate center= CameraUpdateFactory.newLatLng(latlng);
CameraUpdate zoom=CameraUpdateFactory.zoomTo(14);
googleMap.moveCamera(center);
googleMap.animateCamera(zoom);
}
});
它或多或少正確地工作,這意味着我看到了地圖,看到了標記,並且地圖在正確的位置。 但不幸的是,我無法向右滾動...
這不適合您嗎?
<?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" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
如果您將地圖放在簡單的框架布局中,則它不會使用滾動布局,而是會自動沿任何方向滾動。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="thiscode.databasedemo.Main">
<TextView
android:id="@+id/tv"
// adjust margins for your need.
android:layout_marginTop="30dp"
// etc
<FrameLayout
android:id="@+id/fl"
android:layout_below="@+id/tv"
android:layout_height="fill_parent"
// adjust margins for your need.
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="40dp"
android:layout_width="match_parent">
<fragment
android:id="@+id/map"
android:layout_height="fill_parent"
android:layout_width="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
</FrameLayout>
</RelativeLayout>
活動:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_main);
// create class object
fragmentManager = getFragmentManager();
//
try {
GoogleMap googleMap =
((MapFragment) fragmentManager.findFragmentById(R.id.map))
.getMap();
// example data.
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
LatLng latlng = new LatLng(0.083037, 106.704897);
CameraPosition cameraPosition =
new CameraPosition.Builder().target(latlng).zoom(2).build();
googleMap.animateCamera(
CameraUpdateFactory.newCameraPosition(cameraPosition));
} catch (Exception e) {
e.printStackTrace();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.