Google Map Android api V2 中使用MapView遇到CameraUpdateFactory is not initialized!的解决办法

来源:互联网 发布:无网络传奇单机破解版 编辑:程序博客网 时间:2024/05/02 03:10

先说一下 Map V2 API Key 的问题吧:

在打包APP时需要自己生成一个XXX.keystore 用这个密室库生成的SHA1去申请的key 作为AndroidManifest.xml 中的Key使用。

(有时候用debug.keystore生成的Key也可以,不知原因)

进入正题:

根据 https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/CameraUpdateFactory?hl=zh-CN  描述,

CameraUpdateFactory

public final class CameraUpdateFactory

A class containing methods for creating CameraUpdate objects that change a map's camera. To modify the map's camera, call animateCamera(CameraUpdate)animateCamera(CameraUpdate, GoogleMap.CancelableCallback) or moveCamera(CameraUpdate), using a CameraUpdate object created with this class.

For example, to zoom in on a map, you can use the following code:

 GoogleMap map = ...;   map.animateCamera(CameraUpdateFactory.zoomIn());

Prior to using any methods from this class, you must do one of the following to ensure that this class is initialized:

  • Wait for a GoogleMap to become available from a MapFragment or MapView that you have added to your application. You can verify that the GoogleMap is available by calling the getMap() method and checking that the returned object is not null.
  • Call initialize(Context). As long as a com.google.android.gms.common.GooglePlayServicesNotAvailableException GooglePlayServicesNotAvailableException isn't thrown, this class will be correctly initialized.
即:初始化CameraUpdateFactory 需要保证

1.GoogleMap对象不为空

2.MapsInitializer.initialize(Context) 进行初始化


因此排除GoogleMap对象为空,只需在CameraUpdateFactory 之前调用MapsInitializer.initialize(Context) 即可进行初始化。


原创粉丝点击