找不到setOnCameraIdleListener()方法 在Android谷歌地图集群。
我编译了这个库:
依赖项{编译 ' com.google.maps.android:机器人-MAPS-utils的:0.5 +'}
public class ClusteringDemoActivity extends MapsActivity {
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().setOnCameraIdleListener(mClusterManager);
getMap().setOnMarkerClickListener(mClusterManager);
addItems();
}
private void addItems() {
double lat = 51.5145160;
double lng = -0.1270060;
for (int i = 0; i < 10; i++) {
double offset = i / 60d;
lat = lat + offset;
lng = lng + offset;
MyItem offsetItem = new MyItem(lat, lng);
mClusterManager.addItem(offsetItem);
}
}
}
`
答案 0 :(得分:0)
我猜您使用的旧版google play服务并不包含相对于GoogleMap类模型 setOnCameraIdleListener 方法的实现。
因此,请尝试通过放置或编辑build.gradle文件来升级播放服务:
编译com.google.android.gms:播放服务:11.0.1&#39;
答案 1 :(得分:0)
我通过编辑build.gradle
文件来解决它:
compile 'com.google.maps.android:android-maps-utils:0.5'
答案 2 :(得分:0)
请尝试这个
MarkerClustering.java
import android.app.Activity;
import android.util.Log;
import com.google.android.gms.maps.GoogleMap;
import com.google.maps.android.clustering.ClusterManager;
public class MarkerClustering implements GoogleMap.OnCameraIdleListener {
private static final String TAG = MarkerClustering.class.getSimpleName();
public MarkerClustering(Activity activity, GoogleMap googleMap) {
try {
ClusterManager<CustomClusterItem> clusterManager = new ClusterManager<>(activity, googleMap);
MarkerRenderer markerRenderer = new MarkerRenderer(activity, googleMap, clusterManager, this);
clusterManager.setRenderer(markerRenderer);
googleMap.setOnCameraIdleListener(clusterManager);
googleMap.setOnMarkerClickListener(clusterManager);
} catch (Exception e) {
Log.e(TAG, Log.getStackTraceString(e));
}
}
@Override
public void onCameraIdle() {
// Your code
}
}
MarkerRenderer.java
import android.content.Context;
import com.google.android.gms.maps.GoogleMap;
import com.google.maps.android.clustering.ClusterManager;
import com.google.maps.android.clustering.view.DefaultClusterRenderer;
public class MarkerRenderer extends DefaultClusterRenderer<CustomClusterItem> implements GoogleMap.OnCameraIdleListener {
private GoogleMap.OnCameraIdleListener onCameraIdleListener;
public MarkerRenderer(Context context, GoogleMap googleMap, ClusterManager<CustomClusterItem> clusterManager, GoogleMap.OnCameraIdleListener onCameraIdleListener) {
super(context, googleMap, clusterManager);
this.onCameraIdleListener = onCameraIdleListener;
}
@Override
public void onCameraIdle() {
if (onCameraIdleListener != null) {
onCameraIdleListener.onCameraIdle();
}
}
}
您可以使用默认ClusterItem。如果需要,请按如下所示的自定义类对其进行自定义。
CustomClusterItem.java
import com.google.android.gms.maps.model.LatLng;
import com.google.maps.android.clustering.ClusterItem;
public class CustomClusterItem implements ClusterItem {
private final LatLng latLng;
CustomClusterItem(LatLng latLng) {
this.latLng = latLng;
}
@Override
public LatLng getPosition() {
return latLng;
}
}