Can't Create ClusterManager For Android Google Maps

蓝咒 提交于 2020-12-15 05:57:25

问题


I'm trying to use Google Maps clustering in my Android project.

Currently for a start I actually copied the code from the demo https://github.com/googlemaps/android-maps-utils (which runs fine for me)

On my project, however, on

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

it throws:

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

I can't even figure out what why is any there casting?

Basically took the activity (and all needed related classes) from 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"/>

and have on my build.gradle

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

Does anyone have any idea, please?


回答1:


Incase this can help anyone -

I had in my own project a layout named text_bubble.xml This clashed with a layout of the library, by the same name, and it caused the exception.

Simply renaming my layout resolved this.



来源:https://stackoverflow.com/questions/35742544/cant-create-clustermanager-for-android-google-maps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!