如何手动刷新Google地图? [英] How to refresh a google map manually?

查看:267
本文介绍了如何手动刷新Google地图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标记-创建活动,用户可以自定义位置.此活动关闭后,我想放置新的标签,并且将保存的标记保存在我的Google地图上. (因此更新地图)

I have a marker - creating activity, when the user can customise the position. After this activity is closed I want to put the new, and the ealier saved markers on my google map. (so UPDATE the map)

我正在使用此功能:

onMapReady(GoogleMap googleMap)

,但无法正常工作.我必须关闭并重新打开应用程序才能看到更改.

but it is not working properly. I have to close and reopen the app to see the changes.

推荐答案

如果您在活动/片段中覆盖onResume,则标记创建活动结束后便可以进行工作.在其中,如果将GoogleMap对象设置为全局对象,则可以像在onMapReady中一样更新地图.例如:

If you override onResume in your activity/fragment, you can do work once the marker creation activity closes. In there, you can update your map like you would in onMapReady if you set your GoogleMap object as global. For example:

GoogleMap mGoogleMap;

...

...onMapReady(GoogleMap googleMap){

    mGoogleMap = googleMap;

    ...

}

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

    if(mGoogleMap != null){ //prevent crashing if the map doesn't exist yet (eg. on starting activity)
        mGoogleMap.clear();

        // add markers from database to the map
    }
}

您可以考虑将标记添加到在onResumeonMapReady中被调用的函数中,以使您的代码更具可维护性.

You could consider making your marker adding into a function that gets called in onResume and onMapReady to make your code more maintainable.

这篇关于如何手动刷新Google地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆