【发布时间】:2016-03-02 09:00:24
【问题描述】:
我正在尝试在我的 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
我什至无法弄清楚为什么会有演员表?
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+'
请问有谁知道吗?
【问题讨论】:
-
您的其中一个布局有问题。你能分享你的布局和你使用它们的代码吗?
-
所有代码都是从我的问题的链接中复制粘贴的。不过,我会编辑我的帖子。谢谢。
-
在this 线程中,您可以找到解决问题的方法:)
标签: java android google-maps google-maps-android-api-2