使用Google Maps Android API V2时,我会按照Google Play服务设置文档进行检查,以确保安装了Google Play服务,并在我的主要活动中使用以下代码:
@Override
public void onResume()
{
checkGooglePlayServicesAvailability();
super.onResume();
}
public void checkGooglePlayServicesAvailability()
{
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if(resultCode != ConnectionResult.SUCCESS)
{
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, this, 69);
dialog.setCancelable(false);
dialog.setOnDismissListener(getOnDismissListener());
dialog.show();
}
Log.d("GooglePlayServicesUtil Check", "Result is: " + resultCode);
}
Run Code Online (Sandbox Code Playgroud)
这很好用.但是,我注意到我已经存在的一些较旧的Android手机(大多数运行2.2)都缺少GooglePlayServices以及Google Maps应用程序本身.
LogCat将报告此错误:Google Maps Android API:Google地图应用程序丢失.
问题 - 如何在设备上对Google地图的可用性执行类似的检查?其次,如果用户已经安装了Google地图,我认为检查需要确保其安装的版本与Android Maps API的V2兼容.
更新 这是我的setupMapIfNeeded()方法,它在onCreate()的末尾被调用.这是我认为我想确定是否已安装Google地图并提醒用户的地方,请参阅else块:
private void setUpMapIfNeeded()
{
// Do a null check to confirm that we have not already instantiated the map.
if …
Run Code Online (Sandbox Code Playgroud) 在onCreate
方法中,我正在利用它SupportMapFragment
来显示地图.
SupportMapFragment fragment = new SupportMapFragment();
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, fragment).commit();
Run Code Online (Sandbox Code Playgroud)
与此相关,我想添加一个标记.问题是当调用getMap
为null时,我什么时候可以再试一次?是否有我可以注册的事件或我的方法本身是错的?
mMap = ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.map))).getMap();
if(mMap == null)
//what do I do here?
Run Code Online (Sandbox Code Playgroud)
事实上,地图显示在手机上,但我似乎没有运气获得添加标记的参考.
更新:
我SupportMapFragment
通过构造函数创建的原因是因为典型的setContentView
崩溃并且无法正常工作.这让我处于困境,在那里我无法获得我在onCreate
方法中的参考,因为我当时正在创建SupportMapFragment
它.在进一步调查中,似乎我的setContentView
问题是没有将Google-play-services jar和module/src设置为整个项目的一部分的副产品.完成这些后,setContentView
现在可以工作,我可以getMap()
像我期望的那样获得参考.
lots.xml ...
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)
LotsActivity.java ...
public class LotsActivity extends FragmentActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lots);
GoogleMap mMap;
mMap = ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.map))).getMap();
if(mMap == null) …
Run Code Online (Sandbox Code Playgroud)