Java 地图冻结,在滑块选项卡中添加后不允许缩放
我正在尝试将地图添加到选项卡。我第一次使用tabActivity实现了这一点,但由于它已被弃用,我决定再次使用滑动选项卡和导航抽屉。我跟随并设法让它工作,但现在我有一个问题-当我尝试用手指移动地图时,它会变得非常慢,它会拖,并且只移动一点点。我的另一个问题是放大/缩小-我只能在双击时放大,不能缩小。此外,将禁用上下移动地图 我的地图片段:Java 地图冻结,在滑块选项卡中添加后不允许缩放,java,android,google-maps,android-fragments,Java,Android,Google Maps,Android Fragments,我正在尝试将地图添加到选项卡。我第一次使用tabActivity实现了这一点,但由于它已被弃用,我决定再次使用滑动选项卡和导航抽屉。我跟随并设法让它工作,但现在我有一个问题-当我尝试用手指移动地图时,它会变得非常慢,它会拖,并且只移动一点点。我的另一个问题是放大/缩小-我只能在双击时放大,不能缩小。此外,将禁用上下移动地图 我的地图片段: public class MapsActivity extends Fragment { private GoogleMap mMap;
public class MapsActivity extends Fragment {
private GoogleMap mMap;
MapView mapView;
Marker marker; // Marker
int markerCount = 0; // Marker counter
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflate and return the layout
View v = inflater.inflate(R.layout.activity_maps, container,
false);
mapView = (MapView) v.findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.onResume();// needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mMap = mapView.getMap();
mMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location myLocation = locationManager.getLastKnownLocation(provider);
double latitude = myLocation.getLatitude();
double longitude = myLocation.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
mMap.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_me)));
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
int iMax = 5; // Max number of markers
@Override
public void onMapLongClick(LatLng arg0) {
if (markerCount < iMax) {
// start SendMessageActivity need to add marker to message activity
Intent intent = new Intent(getActivity(), SendInvitationActivity.class);
startActivity(intent);
markerCount = markerCount + 1;
marker = mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_0))
.position(
new LatLng(arg0.latitude,
arg0.longitude))
.visible(true));
} else {
Toast.makeText(getActivity().getBaseContext(), "Only " + iMax + " markers allowed at the same time",
Toast.LENGTH_LONG).show();
}
}
});
return v;
}
@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();
}
公共类映射活动扩展了片段{
私有谷歌地图;
地图视图;
Marker;//Marker
int markerCount=0;//标记计数器
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//充气并返回布局
视图v=充气机。充气(R.layout.activity\u地图、容器、,
假);
mapView=(mapView)v.findViewById(R.id.mapView);
onCreate(savedInstanceState);
mapView.onResume();//需要立即显示地图
试一试{
初始化(getActivity().getApplicationContext());
}捕获(例外e){
e、 printStackTrace();
}
mMap=mapView.getMap();
mMap.setMyLocationEnabled(真);
LocationManager LocationManager=(LocationManager)getActivity().getSystemService(Context.LOCATION\u服务);
标准=新标准();
字符串提供程序=locationManager.getBestProvider(条件为true);
Location myLocation=locationManager.getLastKnownLocation(提供者);
双纬度=myLocation.getLatitude();
double longitude=myLocation.getLongitude();
LatLng LatLng=新LatLng(纬度、经度);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
mMap.addMarker(新标记选项()
.位置(新车床(纬度、经度))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_me));
mMap.setOnMapLongClickListener(新的GoogleMap.OnMapLongClickListener(){
int iMax=5;//标记的最大数量
@凌驾
在MaplongClick(LatLng arg0)上的公共无效{
如果(标记计数
和xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
所以我的问题是,我的地图对手指输入的反应非常差。我使用TabActivity时不是这样。我想知道这可能是滑块的问题吗?因为我滑动选项卡在它们之间移动,这会“混淆”地图吗
我尝试过这样做,但在地图上不起作用,它只禁用了在选项卡之间滑动。我找到了它。我最初在app_bar_main.xml的工具栏下方添加了TabLayout和ViewPager:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="@android:color/white" />
</android.support.design.widget.AppBarLayout>
在我从app\u bar\u main.xml中删除整个AppBarLayout并将其添加到content\u main.xml中后,它可以正常工作,没有任何问题。我找到了答案。我最初在app\u bar\u main.xml中的工具栏下方添加了TabLayout和ViewPager:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="@android:color/white" />
</android.support.design.widget.AppBarLayout>
从app_bar_main.xml中删除整个AppBarLayout并将其添加到content_main.xml中后,它可以正常工作,没有任何问题