jjzjj

android - 旋转后 MapFragment 变得无响应

coder 2023-12-28 原文

我的问题很简单,我在我的 XML 中添加了 4 个 FrameLayout,并在这些容器中通过代码添加了 4 个 MapFragment。

像这样:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:map="http://schemas.android.com/apk/res-auto"
  android:id="@+id/map_container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.5"
    android:orientation="horizontal">
    <FrameLayout
      android:id="@+id/map1_container"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_weight="0.5""/>
    <FrameLayout
      android:id="@+id/map2_container"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_weight="0.5"/>
  </LinearLayout>
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.5"
    android:orientation="horizontal">
    <fragment
      android:id="@+id/map3_container"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_weight="0.5"/>
    <fragment
      android:id="@+id/map4_container"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_weight="0.5""/>
  </LinearLayout>

在我的 Activity 中

public class MultiGoogleMapActivity extends FragmentActivity {

    private GoogleMap mMap;
    private GoogleMap mMapUp;
    private GoogleMap mMapMiddle;
    private GoogleMap mMapLow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multi_google_map);

mMap1Fragment = SupportMapFragment.newInstance();
    FragmentTransaction fragmentTransaction = getSupportFragmentManager()
                        .beginTransaction();
                fragmentTransaction.add(R.id.map1_container, mMap1Fragment, MAP_FRAGMENT_TAG);
                fragmentTransaction.commit();
mMap2Fragment = SupportMapFragment.newInstance(new GoogleMapOptions().zoomControlsEnabled(false).zoomGesturesEnabled(false).compassEnabled(false).rotateGesturesEnabled(false).scrollGesturesEnabled(false));
    FragmentTransaction fragmentTransaction = getSupportFragmentManager()
                        .beginTransaction();
                fragmentTransaction.add(R.id.map2_container, mMap2Fragment, MAP_FRAGMENT_TAG);
                fragmentTransaction.commit();
mMap3Fragment = SupportMapFragment.newInstance(new GoogleMapOptions().zoomControlsEnabled(false).zoomGesturesEnabled(false).compassEnabled(false).rotateGesturesEnabled(false).scrollGesturesEnabled(false));
    FragmentTransaction fragmentTransaction = getSupportFragmentManager()
                        .beginTransaction();
                fragmentTransaction.add(R.id.map3_container, mMap3Fragment, MAP_FRAGMENT_TAG);
                fragmentTransaction.commit();
mMap4Fragment = SupportMapFragment.newInstance(new GoogleMapOptions().zoomControlsEnabled(false).zoomGesturesEnabled(false).compassEnabled(false).rotateGesturesEnabled(false).scrollGesturesEnabled(false));
    FragmentTransaction fragmentTransaction = getSupportFragmentManager()
                        .beginTransaction();
                fragmentTransaction.add(R.id.map4_container, mMap4Fragment, MAP_FRAGMENT_TAG);
                fragmentTransaction.commit();

        setUpMapIfNeeded();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the
        // map.
        if (mMap == null) {
            mMap = mMap1Fragment.getMap();
        }
        if (mMapUp == null) {
            mMapUp = mMap2Fragment.getMap();
            mMapUp.setOnMapClickListener(new OnMapClickListener() {

                public void onMapClick(LatLng arg0) {
                    CameraPosition cp = mMapUp.getCameraPosition();
                    mMapUp.moveCamera(CameraUpdateFactory.newCameraPosition(mMap.getCameraPosition()));
                    mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cp));
                }
            });

        }
        if (mMapMiddle == null) {
            mMapMiddle = mMap3Fragment.getMap();
            mMapMiddle.setOnMapClickListener(new OnMapClickListener() {

                public void onMapClick(LatLng arg0) {
                    CameraPosition cp = mMapMiddle.getCameraPosition();
                    mMapMiddle.moveCamera(CameraUpdateFactory
                            .newCameraPosition(mMap.getCameraPosition()));
                    mMap.moveCamera(CameraUpdateFactory
                            .newCameraPosition(cp));
                }
            });
        }
        if (mMapLow == null) {
            mMapLow = mMap4Fragment.getMap();
            mMapLow.setOnMapClickListener(new OnMapClickListener() {

                public void onMapClick(LatLng arg0) {
                    CameraPosition cp = mMapLow.getCameraPosition();
                    mMapLow.moveCamera(CameraUpdateFactory
                            .newCameraPosition(mMap.getCameraPosition()));
                    mMap.moveCamera(CameraUpdateFactory
                            .newCameraPosition(cp));
                }
            });
        }
        initializeMapsPositions();
    }

    private void initializeMapsPositions() {
        if (mMap != null) {
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
                    47.224217, -1.631409), 16));
        }
        if (mMapUp != null) {
            mMapUp.setOnCameraChangeListener(new OnCameraChangeListener() {

                public void onCameraChange(CameraPosition arg0) {
                    mMapUp.animateCamera(CameraUpdateFactory.newLatLngBounds(
                            new LatLngBounds(new LatLng(-21.417276,55.039925),
                                        new LatLng(-20.815174,56.008095)),
                            10));
                    mMapUp.setOnCameraChangeListener(null);
                }
            });

        }
        if (mMapMiddle != null) {
            mMapMiddle.setOnCameraChangeListener(new OnCameraChangeListener() {

                public void onCameraChange(CameraPosition arg0) {
                    mMapMiddle.animateCamera(CameraUpdateFactory.newLatLngBounds(
                            new LatLngBounds(new LatLng(14.337573,-61.441246), 
                                            new LatLng(14.947439,-60.603539)), 
                            10));
                    mMapMiddle.setOnCameraChangeListener(null);
                }
            });
        }
        if (mMapLow != null) {
            mMapLow.setOnCameraChangeListener(new OnCameraChangeListener() {

                public void onCameraChange(CameraPosition arg0) {
                    mMapLow.animateCamera(CameraUpdateFactory.newLatLngBounds(
                            new LatLngBounds(new LatLng(17.596903,-78.448288), 
                                            new LatLng(18.643643,-76.207077)), 
                            10));
                    mMapLow.setOnCameraChangeListener(null);
                }
            });

        }

    }

}

问题是:当我旋转设备时,MapFragments 不再响应触摸事件...

有没有人遇到同样的问题并解决了?

最佳答案

我遇到了同样的问题,经过多次故障排除后发现容器中有多个 map fragment 。

所以,我通过使用解决了这个问题

FragmentTransaction.replace

代替

.add

关于android - 旋转后 MapFragment 变得无响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14242209/

有关android - 旋转后 MapFragment 变得无响应的更多相关文章

  1. ruby-on-rails - 每次我尝试部署时,我都会得到 - (gcloud.preview.app.deploy) 错误响应 : [4] DEADLINE_EXCEEDED - 2

    我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie

  2. 旋转矩阵的几何意义 - 2

    点向量坐标矩阵的几何意义介绍旋转矩阵的几何含义之前,先介绍一下点向量坐标矩阵的几何含义点:在一维空间下就是一个标量,如同一条直线上,以任意某一个位置为0点,以一定的尺度间隔为1,2,3...,相反方向为-1,-2,-3...;如此就形成了一维坐标系,这时候任何一个点都可以用一个数值表示,如点p1=5,即即从原点出发沿着x轴正方向移动5个尺度;点p2=-3,负方向移动3个尺度;     在一维坐标系上过原点做垂直于一维坐标系的直线,则形成了二维坐标系,此时描述一个点需要两个数值来表示点p3=(3,2),即从原点出发沿着x轴正方向移动3个尺度,在此基础上沿着y轴正方向移动两个尺度的位置就是点p3。

  3. Unity 3D 制作开关门动画,旋转门制作,推拉门制作,门把手动画制作 - 2

    Unity自动旋转动画1.开门需要门把手先动,门再动2.关门需要门先动,门把手再动3.中途播放过程中不可以再次进行操作觉得太复杂?查看我的文章开关门简易进阶版效果:如果这个门可以直接打开的话,就不需要放置"门把手"如果门把手还有钥匙需要旋转,那就可以把钥匙放在门把手的"门把手",理论上是可以无限套娃的可调整参数有:角度,反向,轴向,速度运行时点击Test进行测试自己写的代码比较垃圾,命名与结构比较拉,高手轻点喷,新手有类似的需求可以拿去做参考上代码usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;u

  4. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  5. ruby-on-rails - 在 Ruby on Rails 中发送响应之前如何等待多个异步操作完成? - 2

    在我做的一些网络开发中,我有多个操作开始,比如对外部API的GET请求,我希望它们同时开始,因为一个不依赖另一个的结果。我希望事情能够在后台运行。我找到了concurrent-rubylibrary这似乎运作良好。通过将其混合到您创建的类中,该类的方法具有在后台线程上运行的异步版本。这导致我编写如下代码,其中FirstAsyncWorker和SecondAsyncWorker是我编写的类,我在其中混合了Concurrent::Async模块,并编写了一个名为“work”的方法来发送HTTP请求:defindexop1_result=FirstAsyncWorker.new.async.

  6. 网站日志分析软件--让网站日志分析工作变得更简单 - 2

    网站的日志分析,是seo优化不可忽视的一门功课,但网站越大,每天产生的日志就越大,大站一天都可以产生几个G的网站日志,如果光靠肉眼去分析,那可能看到猴年马月都看不完,因此借助网站日志分析工具去分析网站日志,那将会使网站日志分析工作变得更简单。下面推荐两款网站日志分析软件。第一款:逆火网站日志分析器逆火网站日志分析器是一款功能全面的网站服务器日志分析软件。通过分析网站的日志文件,不仅能够精准的知道网站的访问量、网站的访问来源,网站的广告点击,访客的地区统计,搜索引擎关键字查询等,还能够一次性分析多个网站的日志文件,让你轻松管理网站。逆火网站日志分析器下载地址:https://pan.baidu.

  7. ruby-on-rails - Rails 在记录 200 OK 后在做什么? (调试响应时间慢) - 2

    我试图在我的RubyonRails应用程序中调试一个极其缓慢的请求调用。我已设法根据自己的喜好优化Controller方法,Rails的日志告诉我它已在XX毫秒内完成操作(Completed200OKin5049ms(Views:34.9ms|ActiveRecord:76.3ms)).但是,在加载页面时,在浏览器中实际呈现任何内容之前打印此消息很长;最多约15秒的等待时间。Rackmini-profiler证实了这一点,告诉我GET操作(不计算完成Controller操作所花费的时间)花费了14秒左右。(分析器还确认Controller操作的执行时间约为5秒)。我可以接受Contro

  8. ruby - 带有 header 的 Sinatra 流式响应 - 2

    我想通过Sinatra应用程序代理远程文件。这需要将带有header的HTTP响应从远程源流式传输回客户端,但我不知道如何在Net::HTTP#提供的block内使用流式API时设置响应header获取响应。例如,这不会设置响应头:get'/file'dostreamdo|out|uri=URI("http://manuals.info.apple.com/en/ipad_user_guide.pdf")Net::HTTP.get_response(uri)do|file|headers'Content-Type'=>file.header['Content-Type']file.re

  9. ruby - Net::HTTP 对 HTTPS 请求的响应极其缓慢 - 2

    出于某种原因,在我的开发机器上,我对通过Net::HTTP执行的HTTPS请求的响应非常非常慢。我试过RestClient和HTTParty,它们都有同样的问题。它似乎是凭空冒出来的。我已毫无问题地提出这些请求数百次,但今天它们的速度慢得令人难以忍受。pry(main)>putsTime.now;HTTParty.get('https://api.easypost.com/v2/addresses');putsTime.now;2015-04-2908:07:08-05002015-04-2908:09:39-0500如您所见,响应耗时2.5分钟。不仅仅是这个EasyPostAPIUR

  10. ruby - 使用 Ruby 将 HTTP GET 的响应主体流式传输到 HTTP POST - 2

    我正在尝试下载一个大文件,然后使用Ruby将该文件发布到REST端点。该文件可能非常大,即超过可以存储在内存中甚至磁盘上的临时文件中的容量。我一直在用Net::HTTP尝试这个,但我愿意接受任何其他库(rest-client等)的解决方案,只要他们做我想做的事情。这是我尝试过的:require'net/http'source_uri=URI("https://example.org/very_large_file")source_request=Net::HTTP::Get.new(source_uri)source_http=Net::HTTP.start(source_uri.ho

随机推荐