18

我正在尝试使用 Android 支持库将新的 Google Maps Android API v2 合并到应用程序中。我经历了多次迭代,试图找到工作的地方,现在我放弃了,认为我应该在这里问。

我去了 API 控制台,创建了一个项目,为 Android 启用了 Google Maps API v2,根据需要创建了一个调试 KEY 并将其粘贴到清单上。没有身份验证问题或类似问题。很确定我做对了。

在我libs放置的项目文件夹中android-support-v4.jar(通过项目右键单击-> Android-> 添加支持库...)。在 Eclipse 中,我执行了 File->Import->Android->Existing Android Code Into Workspace 并导入了最新的(rev 3)android-sdk/google/google_play_services/libproject/google-play-services_lib。然后我将它作为依赖库添加到我的项目中(项目右键->属性->Android->添加为底部的库依赖。

在我的清单中,我有:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" ...>
    <permission
      android:name="myapp.permission.MAPS_RECEIVE"
      android:protectionLevel="signature"/>

    <All the uses-permission required like INTERNET, ACCESS_NETWORK_STATE, WRITE_EXTERNAL_STORAGE, FINE AND COARSE LOCATION, etc>
    <uses-permission android:name="myapp.permission.MAPS_RECEIVE"/>
    <uses-feature android:glEsVersion="0x00020000" android:required="true"/>
    <application ...>
        ...
        <uses-library android:name="com.google.android.maps" />
        <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AI..."/>
    </application>

在我的 mainview.xml 我有:

<fragment
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/map"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   class="com.google.android.gms.maps.SupportMapFragment" />

现在在 MainView.java 我有:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

public class MainView extends FragmentActivity ... {
    private GoogleMap mMap;

    @Override
    protected void onResume() {
        ...
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.map);
        SupportMapFragment mapFragment = (SupportMapFragment)fragment;
        mMap = mapFragment.getMap();
        //PROBLEM: mMap is null here even though mapFragment is not
    }
}

然后我想我可能需要初始化片段,所以我onCreate在设置内容视图后添加了:

//Fragments
FragmentManager manager = getSupportFragmentManager(); 
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.map, SupportMapFragment.newInstance());
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.commit();

然后我说,也许不是SupportMapFragment,但我实际上应该参考实际的片段,在我的onResume我做了:

Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.map);
SupportMapFragment mapFragment = (SupportMapFragment)fragment;

//Fragments
FragmentManager manager = getSupportFragmentManager(); 
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.map, mapFragment);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.commit();

mMap = mapFragment.getMap();

mMap在此之后再次为空。

然后我看到有一个MapsInitializer类,所以我想也许我应该先打电话。

所以我在上面代码中获取片段之前添加了代码:

try {
    MapsInitializer.initialize(this);
} catch (GooglePlayServicesNotAvailableException e) {
    e.printStackTrace();
}

有了这个 Logcat 报告了警告(第 313 行是MapsInitializer.initialize(this)):

com.google.android.gms.common.GooglePlayServicesNotAvailableException
    at com.google.android.gms.internal.n.c(Unknown Source)
    at com.google.android.gms.internal.n.a(Unknown Source)
    at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
    at myapp.activities.MainView.inflateSelectedViewAndSetData(MainView.java:313)
    at myapp.activities.MainView.onClick(MainView.java:642)
    at android.view.View.performClick(View.java:3153)
    at android.view.View$PerformClick.run(View.java:12148)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:152)
    at android.app.ActivityThread.main(ActivityThread.java:4615)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:491)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
    at dalvik.system.NativeStart.main(Native Method)

在我的尝试中,Logcat 会报告以下警告:

Google Play services out of date.  Requires 2010100 but found 1013

这可能是罪魁祸首,但我没有找到解决方案。

在这一点上,我没有想法。我在这里这里这里阅读了几个主题,但没有一个建议解决了这个问题。对于后者,他们建议不包括项目依赖项,只包括 jar 文件。google-play-services.jar好吧,无论是从google-play-services_lib/libs文件夹中使用还是从该项目中导出我自己的 jar,这都不起作用。

顺便说一句,我在实际设备(2.3.5 和带有 3.2 的平板电脑)上运行,而不是在模拟器上运行。

任何帮助都衷心感谢。:)

更新:

解决方案在量子位下方。

至于导出库依赖项,一种不必处理它的简洁方法(在处理存储库时很痛苦)是使用FatJar并导出google_play_services_lib项目(我没有从中创建本地副本,因为当它更新时我不想重新导入),并将输出的 fat jar 文件作为另一个 jar 文件包含在您的项目中。注意:在选择要包含在 fat jar 中的 jar 文件时,我必须排除该annotations.jar文件,因为它已经预先包含并且会导致重复错误。现在我要做的就是将它包含google_play_services_rev_3.jar在我的项目libs文件夹中,我很高兴。

4

3 回答 3

15

如该链接底部所述;http://developer.android.com/google/play-services/setup.html

这是正确处理此问题的示例代码;

int checkGooglePlayServices =    GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);
    if (checkGooglePlayServices != ConnectionResult.SUCCESS) {
    // google play services is missing!!!!
    /* Returns status code indicating whether there was an error. 
    Can be one of following in ConnectionResult: SUCCESS, SERVICE_MISSING, SERVICE_VERSION_UPDATE_REQUIRED, SERVICE_DISABLED, SERVICE_INVALID.
    */
       GooglePlayServicesUtil.getErrorDialog(checkGooglePlayServices, mActivity, 1122).show();
    }
于 2013-03-06T14:42:01.213 回答
6

我在我的项目中遇到了同样的问题。((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();解决方法很简单,只要处理可以返回的可能性null。如果您将处理null内置SupportMapFragment将处理Google Play services out of date.错误,并将向您显示地图本地化消息和用于更新 Google Play 服务的按钮,就像 @qubz 在示例项目中讲述的那样。

示例代码:

@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}

/**
 * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
 * installed) and the map has not already been instantiated.. This will ensure that we only ever
 * call {@link #setUpMap()} once when {@link #mMap} is not null.
 * <p>
 * If it isn't installed {@link SupportMapFragment} (and
 * {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
 * install/update the Google Play services APK on their device.
 * <p>
 * A user can return to this FragmentActivity after following the prompt and correctly
 * installing/updating/enabling the Google Play services. Since the FragmentActivity may not have been
 * completely destroyed during this process (it is likely that it would only be stopped or
 * paused), {@link #onCreate(Bundle)} may not be called again so we should call this method in
 * {@link #onResume()} to guarantee that it will be called.
 */
private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();
        }
    }
}

/**
 * This is where we can add markers or lines, add listeners or move the camera. In this case, we
 * just add a marker near Africa.
 * <p>
 * This should only be called once and when we are sure that {@link #mMap} is not null.
 */
private void setUpMap() {
    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}

结果:

在此处输入图像描述

作为文件悲伤

只有在加载了底层地图系统并且片段中存在底层视图时,才能使用 getMap() 获取 GoogleMap。该类自动初始化地图系统和视图;但是,您无法保证它何时准​​备就绪,因为这取决于 Google Play 服务 APK 的可用性。如果 GoogleMap 不可用,getMap() 将返回 null。支持MapFragment

于 2013-03-19T14:14:37.423 回答
5

Google Play services从 Play 商店下载并安装最新版本。由于某种原因,我无法通过搜索找到它。通过从文件夹中的 SDK 运行示例应用程序,/extras/google/google_play_services/samples/maps我被提示安装升级并被带到 Play 商店执行此操作。

于 2012-12-05T11:43:20.613 回答