我试图在点击地图时添加标记或通过搜索选择地点,我也使用GeoJson创建多个图层,但我获得空指针异常在Google地图中再次点击标记时。
错误讯息:
E / AndroidRuntime:致命异常:主要 过程:com.tekraiders.wherehouse.wherehouse,PID:7058 显示java.lang.NullPointerException 在 com.google.maps.android.data.Layer.multiObjectHandler(Layer.java:144) 在com.google.maps.android.data.Layer.access $ 000(Layer.java:41) 在com.google.maps.android.data.Layer $ 2.onMarkerClick(Layer.java:114) 在com.google.android.gms.maps.GoogleMap $ 2.zza(未知来源) 在com.google.android.gms.maps.internal.zzu $ zza.onTransact(未知 资源) 在android.os.Binder.transact(Binder.java:361) 在 com.google.android.gms.maps.internal.bz.a(:com.google.android.gms.DynamiteModulesB:84) 在 com.google.maps.api.android.lib6.impl.dd.b(:com.google.android.gms.DynamiteModulesB:292) 在 com.google.maps.api.android.lib6.gmm6.api.e.a(:com.google.android.gms.DynamiteModulesB:242) 在 com.google.maps.api.android.lib6.gmm6.vector.m.a(:com.google.android.gms.DynamiteModulesB:4070) 在 com.google.maps.api.android.lib6.gmm6.vector.af.c(:com.google.android.gms.DynamiteModulesB:611) 在 com.google.maps.api.android.lib6.gmm6.vector.df.onSingleTapConfirmed(:com.google.android.gms.DynamiteModulesB:236) 在 com.google.maps.api.android.lib6.impl.gesture.g.onSingleTapConfirmed(:com.google.android.gms.DynamiteModulesB:189) 在 com.google.maps.api.android.lib6.impl.gesture.i.handleMessage(:com.google.android.gms.DynamiteModulesB:132) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:136) 在android.app.ActivityThread.main(ActivityThread.java:5052) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:515) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609) 在dalvik.system.NativeStart.main(本地方法)
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
try {
int i = 0;
//Log.e("SahajLOG", " latlang > " + latLng + " " + layer.getBoundingBox().contains(latLng) + " " + layer.getBoundingBox().including(latLng));
String MarkerString = "location marked";
i = 0;
for (LatLngBounds bbc : latLngBoundsArray) {
Log.e("SahajLOG", "callad A1" + bbc.contains(latLng));
if (bbc.contains(latLng)) {
i++;
layer.addLayerToMap();
MarkerString = "Cyclone ZONE 1";
} else if (layer.isLayerOnMap() && i == 0) {
layer.removeLayerFromMap();
}
}
i = 0;
for (LatLngBounds bbc : latLngBoundsArray2) {
Log.e("SahajLOG", "callad A2" + bbc.contains(latLng));
if (bbc.contains(latLng)) {
i++;
layer2.addLayerToMap();
MarkerString = "Cyclone ZONE 2";
} else if (layer2.isLayerOnMap() && i == 0) {
layer2.removeLayerFromMap();
}
}
i = 0;
for (LatLngBounds bbc : latLngBoundsArray3) {
Log.e("SahajLOG", "callad A3" + bbc.contains(latLng));
if (bbc.contains(latLng)) {
i++;
layer3.addLayerToMap();
MarkerString = "Cyclone ZONE 3";
} else if (layer3.isLayerOnMap() && i == 0) {
layer3.removeLayerFromMap();
}
}
i = 0;
for (LatLngBounds bbc : latLngBoundsArray4) {
Log.e("SahajLOG", "callad A4" + bbc.contains(latLng));
if (bbc.contains(latLng)) {
i++;
layer4.addLayerToMap();
MarkerString = "Cyclone ZONE 4";
} else if (layer4.isLayerOnMap() && i == 0) {
layer4.removeLayerFromMap();
}
}
if (hashMapMarker != null) {
if (hashMapMarker.get("otherLocMark") != null) {
removeUpMaker("otherLocMark");
setUpMaker(latLng.latitude, latLng.longitude, "otherLocMark", MarkerString);
} else
setUpMaker(latLng.latitude, latLng.longitude, "otherLocMark", MarkerString);
} else
setUpMaker(latLng.latitude, latLng.longitude, "otherLocMark", MarkerString);
} catch (Exception e) {
e.printStackTrace();
}
}
});
和标记
private void setUpMaker(double currentLatitude,double currentLongitude,String Key,String title) {
hashMapMarker = new HashMap<>();
Marker marker = mMap.addMarker(new MarkerOptions().position(new LatLng(currentLatitude, currentLongitude)).title(title));
hashMapMarker.put(Key, marker);
marker.showInfoWindow();
// Log.e("SahajLOG", "setUpMarker > " +marker +" key "+Key);
//mMap.addMarker(new MarkerOptions().position(new LatLng(currentLatitude, currentLongitude)).title("I am here!"));
}
private void removeUpMaker(String Key) {
Marker marker = hashMapMarker.get(Key);
marker.remove();
hashMapMarker.remove(Key);
Log.e("SahajLOG", "removeMarker > " + marker + " key " + Key);
}