无法为Android版Google地图创建ClusterManager

时间:2016-03-02 09:00:25

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

我正在尝试在我的Android项目中使用Google地图群集。

目前我开始实际复制了演示中的代码 https://github.com/googlemaps/android-maps-utils  (对我来说运行正常)

在我的项目上,

mClusterManager = new ClusterManager<MyItem>(this, getMap());
它扔了:

java.lang.ClassCastException: android.widget.ImageView cannot be cast to com.google.maps.android.ui.RotationLayout

我甚至无法弄清楚为什么有铸造?

基本上从中获取活动(以及所有需要的相关课程) https://github.com/googlemaps/android-maps-utils/blob/master/demo/src/com/google/maps/android/utils/demo/BigClusteringDemoActivity.java

public class ClusteringDemoActivity extends BaseDemoActivity {
private ClusterManager<MyItem> mClusterManager;

@Override
protected void startDemo() {
    getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(51.503186, -0.126446), 10));

    mClusterManager = new ClusterManager<MyItem>(this, getMap());
    getMap().setOnCameraChangeListener(mClusterManager);

    try {
        readItems();
    } catch (JSONException e) {
        Toast.makeText(this, "Problem reading list of markers.", Toast.LENGTH_LONG).show();
    }
}

private void readItems() throws JSONException {
    InputStream inputStream = getResources().openRawResource(R.raw.radar_search);
    List<MyItem> items = new MyItemReader().read(inputStream);
    mClusterManager.addItems(items);
}

public static void launch(Context context) {
    context.startActivity(new Intent(context, ClusteringDemoActivity.class));
}

}

public abstract class BaseDemoActivity extends FragmentActivity {
private GoogleMap mMap;

protected int getLayoutId() {
    return R.layout.map;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(getLayoutId());
    setUpMapIfNeeded();
}

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

private void setUpMapIfNeeded() {
    if (mMap != null) {
        return;
    }
    mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    if (mMap != null) {
        startDemo();
    }
}

/**
 * Run the demo-specific code.
 */
protected abstract void startDemo();

protected GoogleMap getMap() {
    setUpMapIfNeeded();
    return mMap;
}

}

public class MyItem implements ClusterItem {
private final LatLng mPosition;

public MyItem(double lat, double lng) {
    mPosition = new LatLng(lat, lng);
}

@Override
public LatLng getPosition() {
    return mPosition;
}

}

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

并拥有我的build.gradle

compile 'com.google.android.gms:play-services-maps:8.4.0'
compile 'com.google.maps.android:android-maps-utils:0.3+'

有人有任何想法吗?

1 个答案:

答案 0 :(得分:2)

这可以帮助任何人 -

我在自己的项目中有一个名为text_bubble.xml的布局 这与图书馆的布局发生冲突,名称相同,引起了例外。

只需重命名我的布局即可解决此问题。