MyLocationOverlay 下次加载活动时消失问题?

发布于 2024-11-09 11:21:30 字数 1220 浏览 0 评论 0原文

朋友们,

我注意到一件事,当我的位置相同时,屏幕上不显示 MyLocationOverlay 标记。

我已经测试过,当我更改模拟器位置值时,它会出现在屏幕上,我转到下一页,然后再次转到同一页面,但它没有显示。是的,当我更改位置值时,它会再次出现。我想无论位置是否改变都始终显示它 有人指导我如何吗?

这是我的代码

onCreate()
{

 myLocOverlay = new MyLocationOverlay(this, mapView);
         myLocOverlay.enableMyLocation();
         mapView.getOverlays().add(myLocOverlay);
         mapView.postInvalidate();
zoomToMyLocation();
}


@Override
    protected void onPause() {
        super.onPause();
        if (myLocOverlay != null)
            myLocOverlay.disableMyLocation();
    }

    @Override
    public void onResume() {


        if (myLocOverlay != null)
            myLocOverlay.enableMyLocation();

        super.onResume();
    }

  private void zoomToMyLocation() {
                GeoPoint myLocationGeoPoint = myLocationOverlay.getMyLocation();
                if(myLocationGeoPoint != null) {
                        mapView.getController().animateTo(myLocationGeoPoint);
                        mapView.getController().setZoom(10);
                }
                else {
                        Toast.makeText(this, "Cannot determine location", Toast.LENGTH_SHORT).show();
                }
        }

friends,

i have noticed one thing when my location is same MyLocationOverlay marker is not displayed on screen.

i have tested it when i change emulator location values it is appeared on screen i go to next page then come to same page again and it is not shown.yes when i change location values it is appeared again. i want to show it always if location is changed or not
any one guide me how?

here is my code

onCreate()
{

 myLocOverlay = new MyLocationOverlay(this, mapView);
         myLocOverlay.enableMyLocation();
         mapView.getOverlays().add(myLocOverlay);
         mapView.postInvalidate();
zoomToMyLocation();
}


@Override
    protected void onPause() {
        super.onPause();
        if (myLocOverlay != null)
            myLocOverlay.disableMyLocation();
    }

    @Override
    public void onResume() {


        if (myLocOverlay != null)
            myLocOverlay.enableMyLocation();

        super.onResume();
    }

  private void zoomToMyLocation() {
                GeoPoint myLocationGeoPoint = myLocationOverlay.getMyLocation();
                if(myLocationGeoPoint != null) {
                        mapView.getController().animateTo(myLocationGeoPoint);
                        mapView.getController().setZoom(10);
                }
                else {
                        Toast.makeText(this, "Cannot determine location", Toast.LENGTH_SHORT).show();
                }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往日情怀 2024-11-16 11:21:30

如果您在调用意图(意味着您的其他页面)时调用 finish() 方法,那么您将销毁页面中的所有值并移至新页面。

示例 Activity A Activity B

从 Activity A 移动到 Activity B

Intentintent = new Intent();

意图.setclass(...);

启动活动(意图);

回到 Activity A

只需在 Activity B 上调用 finish()

If you are calling finish() method while calling intent (means your other page) then you are destroying all the values in your page and moving towards new page.

example Activity A Activity B

moving from Activity A to Activity B

Intent intent = new Intent();

intent.setclass(...);

startActivity(intent);

coming back to Activity A

just call finish() on Activity B

空城缀染半城烟沙 2024-11-16 11:21:30

其实原因是。

当我第一次加载记录时,它会在现场获取 GPS 位置。有时,当我进行此活动时,GPS 需要一些时间来获取位置,并且我的代码已通过,但没有出现任何内容。

所以问题是,如果没有及时获取位置,那么我应该等待它并在获取时显示。

所以我实现了 RunOnFirstFix() 来解决这个问题。另一个最重要的事情是使用 Mapview.postInvalidate() 将其显示在 Mapview 上。

因此,如果我们按照此步骤操作,就不会出现 MyLocationOverlay 消失的问题。

code sample onCreate()
{

// new line
myLocOverlay.runOnFirstFix(showCurrentLocationIfObtainedLate );

}


Runnable showCurrentLocationIfObtainedLate = new Runnable() {

            @Override
            public void run() {



                if(myLocOverlay.getMyLocation() != null)
                {
                mapView.getController().animateTo(myLocOverlay.getMyLocation());
                    mapView.postInvalidate();
                }



            }
        };

Actually the reason was.

first time when i load record it get gps location on the spot. sometimes when i come to this activity gps take time to obtain location and my code is passed and nothing is appeared.

so problem is if location is not obtained intime then i should wait for it and display as we get it.

so i implemented RunOnFirstFix() to get rid of this issue. and another most important thing use Mapview.postInvalidate() to display it on mapview.

so if we follow this step we wont be having MyLocationOverlay disappear problem.

code sample onCreate()
{

// new line
myLocOverlay.runOnFirstFix(showCurrentLocationIfObtainedLate );

}


Runnable showCurrentLocationIfObtainedLate = new Runnable() {

            @Override
            public void run() {



                if(myLocOverlay.getMyLocation() != null)
                {
                mapView.getController().animateTo(myLocOverlay.getMyLocation());
                    mapView.postInvalidate();
                }



            }
        };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文