我正在尝试将新的Google Maps Android API v2整合到一个使用Android Support Library的应用程序中。我已经经历了许多次迭代,试图找到工作的地方,现在我已经放弃了,并认为我应该在这里询问。
我转到应用程序接口控制台,创建了一个项目,启用了Google Maps API v2 for Android,根据需要创建了一个调试密钥,并将其粘贴到清单中。没有身份验证问题或类似的问题。我很确定我做得对。
在我的项目的libs
文件夹中,我放置了android-support-v4.jar
(通过项目右键->Android->Add Support Library...)。在Eclipse中,我执行了文件->导入->安卓->现有安卓代码到工作区,并导入了android-sdk/google/google_play_services/libproject/google-play-services_lib.的最新版本(版本3然后,我将其作为依赖库添加到我的项目中(项目右键单击->Properties->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();
}
使用此日志报告警告(第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
这可能是罪魁祸首,但我找不到解决办法。
在这一点上,我没有想法。我读了几个帖子here,here,here,但是没有一个建议解决了这个问题。对于后者,他们建议不包括项目依赖项,而只包括jar文件。好吧,无论是使用google-play-services_lib/libs
文件夹中的google-play-services.jar
还是从该项目中导出我自己的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
文件夹中,然后就可以开始了。
发布于 2012-12-05 19:43:21
从Play Store下载并安装最新的Google Play services
。由于某种原因,我无法通过搜索找到它。通过运行/extras/google/google_play_services/samples/maps
文件夹中SDK中的示例应用程序,系统会提示我安装升级,并将我带到Play Store来执行此操作。
发布于 2013-03-06 22:42:01
如该链接底部所述;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-19 22:14:37
我在我的项目中也遇到了同样的问题。解决方案非常简单,只需处理((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。SupportMapFragment
https://stackoverflow.com/questions/13722192
复制相似问题