Android-Maps-Extensions 使用 MapFragment 生成错误

Android-Maps-Extensions Generating Error with MapFragment

我正在将我现有的 Google 地图代码(其中大部分是我在 Android Studio 中选择 "Google Maps Activity" 时生成的)转换为 Android-Maps-扩展库,以便同时使用作为该库一部分的标记聚类和使用 Android-Map-Extensions 开发的重叠标记 Spiderfier 工具。

现在,我对这个方法有疑问(我把它做得比我实际现有的方法更小,以便于阅读,所以如果有任何错误,请告诉我,我会编辑它):

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(); //ERROR IS HERE
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }

基本上是行:

mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

正在生成一条消息

Incompatible Types:
 Required: com.androidmapsextensions.GoogleMap
 Found: com.google.android.gms.maps.GoogleMap

这是我的导入语句(为了以防万一,我包括了所有这些语句,包括当前被注释掉的语句):

import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.androidmapsextensions.*;
//import com.google.android.gms.maps.GoogleMap;
//import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
//import com.google.android.gms.maps.model.Marker;
//import com.google.android.gms.maps.model.MarkerOptions;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

我在 activity_maps.xml 中将 android:name 从 "android:name="com.google.android.gms.maps.SupportMapFragment" 更改为 "com.androidmapsextensions.SupportMapFragment" 看起来像这样:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:id="@+id/map" tools:context=".MapsActivity"
    android:name="com.androidmapsextensions.SupportMapFragment" />

此外,这些是我的 Gradle 文件中的依赖项:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.google.android.gms:play-services:6.5.87'
    compile 'com.androidmapsextensions:android-maps-extensions:2.0.0'
}

仍然收到相同的错误消息。是否有我忘记更改或需要添加的内容?

如果您想了解更多信息,请在下方留言,我会回复或编辑此post。

您必须使用 getExtendedMap 而不是 getMap(或 getExtendedMapAsync),如 Migration from Android API v2 中所述。

请注意,您在演示应用程序中调用的去聚类或重叠标记 Spiderfier 尚未准备好生产,您必须对其进行调整,因此它不会崩溃。

完全披露:我是 Android Maps Extensions 开发人员。