【问题标题】:Android-Maps-Extensions Generating Error with MapFragmentAndroid-Maps-Extensions 使用 MapFragment 生成错误
【发布时间】:2015-03-06 17:01:30
【问题描述】:

我正在将现有的 Google 地图代码(其中大部分是在我在 Android Studio 中选择“Google Maps Activity”时生成的)转换为 Android-Maps-Extensions 库,以便同时使用属于这个库和一个使用 Android-Map-Extensions 开发的 Overlapping Marker 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'
}

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

如果您想了解更多信息,请在下方发表评论,我会回复或编辑此帖子。

【问题讨论】:

  • mMap是如何定义的?
  • @TimCastelijns "私人 GoogleMap mMap;"它是在方法之外声明的。
  • 确保 GoogleMap 的定义引用了正确的包
  • @TimCastelijns 当我右键点击“GoogleMap”,点击“Go to”,然后点击“Declaration”,它带我去“package com.androidmapsextensions;”所以看起来它指的是正确的包。

标签: android google-maps google-maps-android-api-2 android-maps-extensions


【解决方案1】:

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

请注意,像您在演示应用程序中所称的去集群或重叠标记 Spiderfier 尚未准备好用于生产,您必须对其进行调整,以免崩溃。

全面披露:我是 Android 地图扩展开发人员。

【讨论】:

  • getMap 现已弃用
猜你喜欢
  • 2015-03-07
  • 1970-01-01
  • 1970-01-01
  • 2013-06-03
  • 1970-01-01
  • 1970-01-01
  • 2016-05-15
  • 2012-12-16
  • 2017-06-29
相关资源
最近更新 更多