谷歌地图API的Android抛出V2 GooglePlayServicesNotAvailable

2019-07-04 13:37发布

我试图将新的谷歌地图API的Android进入V2采用了Android支持库的应用程序。 我已经经历了许多次迭代去尝试得到的地方工作,现在我已经放弃了,我想我应该问在这里。

我去了API控制台,创建了一个项目,使谷歌地图API第2版的Android,根据需要创建调试KEY并粘贴它的清单。 没有身份验证问题或类似的东西。 肯定我没这个权利。

在我的项目的libs文件夹,我把android-support-v4.jar (通过项目右键单击- > Android->添加支持库...)。 在Eclipse中我做文件 - >导入 - > Android->将现有Android代码进入工作区,并引进国外最新(REV 3)Android的SDK的/谷歌/ google_play_services / libproject /谷歌播放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.jargoogle-play-services_lib/libs文件夹或导出我自己的罐子从该项目。

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

任何帮助,竭诚赞赏。 :)

更新:

该解决方案下面是由量子位。

至于出口的库的依赖,一种巧妙的方法没有处理它(这是与库打交道时有疼痛感)是使用FatJar和导出google_play_services_lib项目(我没有创建一个本地副本的,因为当它更新我不想重新导入),并在项目中包含的另一个jar文件THA输出脂肪jar文件。 注:选择在脂肪罐子包括jar文件,我不得不排除annotations.jar文件,因为它已经被预先包含并导致错误的重复。 现在我需要做的是包括google_play_services_rev_3.jar在我的项目的libs文件夹,我好去。

Answer 1:

下载并安装最新的Google Play services从Play商店。 出于某种原因,我无法通过搜索找到它。 通过在文件夹中运行SDK中的示例应用程序/extras/google/google_play_services/samples/maps系统提示我安装升级,被送往Play商店做到这一点。



Answer 2:

为在该链路的底部描述 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();
    }


Answer 3:

我在我的项目有同样的问题。 该解决方案是非常简单的只是处理的可能性, ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); 可以返回null 。 如果你将处理null的打造专业化,在SupportMapFragment将处理Google Play services out of date. 错误,会告诉你insted的地图本地化的消息和按钮用于更新谷歌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"));
}

而结果是:

由于文档伤心

甲GoogleMap的只能使用的GetMap获取()时标的地图系统被加载并在该片段中的底层视图存在。 此类自动初始化映射系统和视图; 但是你不能保证什么时候会准备好,因为这依赖于谷歌Play服务APK的可用性。 如果GoogleMap的不可用,的GetMap()将返回null。 SupportMapFragment



文章来源: Google Maps Android API v2 throws GooglePlayServicesNotAvailableException, out of date, SupportMapFragment.getMap() returns null