Google Map Android api V2 中使用MapView遇到CameraUpdateFactory is not initialized!的解决办法
先说一下 Map V2 API Key 的问题吧:
在打包APP时需要自己生成一个XXX.keystore 用这个密室库生成的SHA1去申请的key 作为AndroidManifest.xml 中的Key使用。
(有时候用debug.keystore生成的Key也可以,不知原因)
进入正题:
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 aMapFragment
orMapView
that you have added to your application. You can verify that theGoogleMap
is available by calling thegetMap()
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) 即可进行初始化。