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也可以,不知原因)

进入正题:

根据 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) 即可进行初始化。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值