I want to remove all the markers and remove setInfoWindowAdapter when i refresh the maps. But the app will crash if i refresh it.
Here's my code
private final BroadcastReceiver mNetworkReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null) { // connected to the internet
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
// connected to wifi
//Toast.makeText(context, activeNetwork.getTypeName(), Toast.LENGTH_SHORT).show();
new GetInfo().execute(Config.INFO_URL);
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
// connected to the mobile provider's data plan
//Toast.makeText(context, activeNetwork.getTypeName(), Toast.LENGTH_SHORT).show();
new GetInfo().execute(Config.INFO_URL);
}
}
}
};
class GetInfo extends AsyncTask<String, Void, Boolean> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(MapsActivity.this);
dialog.setMessage("Mohon tunggu");
dialog.setTitle("Mendapatkan data...");
dialog.show();
dialog.setCancelable(true);
}
@Override
protected Boolean doInBackground(String... urls) {
array.clear();
mMarkersHashMap.clear();
mMyMarkersArray.clear();
markerPoints.clear();
targets.clear();
try {
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
JSONArray konten = jsono.getJSONArray("konten");
mMarkersHashMap = new HashMap<Marker, MyMarker>();
for (int i = 0; i < konten.length(); i++) {
HashMap<String,String> newMap=new HashMap<String,String>();
JSONObject object = konten.getJSONObject(i);
newMap.put("nama", object.getString("nama"));
newMap.put("deskripsi",object.getString("deskripsi"));
newMap.put("foto",object.getString("foto"));
newMap.put("marker", object.getString("marker"));
newMap.put("lat",object.getString("lat"));
newMap.put("lng",object.getString("lng"));
array.add(newMap);
}
return true;
}
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(final Boolean result) {
dialog.cancel();
runOnUiThread(new Runnable() {
@Override
public void run() {
if (!result) {
//Toast.makeText(getApplicationContext(), "Tidak dapat mengambil data dari server, silahkan cek koneksi internet anda", Toast.LENGTH_LONG).show();
showInfo();
}
else {
for (int i = 0; i < array.size(); i++) {
HashMap<String, String> newMap = new HashMap<String, String>();
newMap = array.get(i);
mMyMarkersArray.add(new MyMarker(newMap.get("nama"), newMap.get("deskripsi"), newMap.get("foto"), newMap.get("marker"), Double.parseDouble(newMap.get("lat")), Double.parseDouble(newMap.get("lng"))));
}
plotMarkers(mMyMarkersArray);
}
}
});
}
}
Here's the plotMarkers
public void plotMarkers(ArrayList<MyMarker> markers) {
if(markers.size() > 0) {
for (MyMarker myMarker : markers)
{
dest = new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude());
markerOption = new MarkerOptions().position(dest);
location_marker = mMap.addMarker(markerOption);
Target target = new PicassoMarker(location_marker);
targets.add(target);
ImageView image = new ImageView(this);
image.setImageResource(R.mipmap.marker);
int width = image.getDrawable().getIntrinsicWidth();
int height = image.getDrawable().getIntrinsicHeight();
Picasso.with(MapsActivity.this).load(myMarker.getmIcon()).resize(width, height).onlyScaleDown().into(target);
mMarkersHashMap.put(location_marker, myMarker);
i = getIntent();
if(i.getBooleanExtra("maps", true)) {
location_marker.setTitle(i.getStringExtra("nama"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(dest, 16));
}
else {
mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
}
}
}
}
Here's the refresh menu on action bar
case R.id.action_refresh:
registerReceiver(mNetworkReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
return true;
What's wrong with my code?
I'm assuming you still have similar implementation from before.. In order to remove all Marker
s. You could just call Marker.remove()
on the marker instance. In your case, you put the marker instance in a HashMap
. So what you could do is something like this:
private void removeAllMarkers() {
// hmap here is the HashMap you have.
for (HashMap.Entry<Marker, MyMarker> entry : hmap.entrySet()) {
entry.getKey().remove();
}
}
This iterates over the HashMap
where you add the Marker
s in plotMarkers
function and remove them all. Just call this function in the code where you refresh the Map
.
Regarding your concern with the setInfoWindowAdapter
, if you're pertaining to the InfoWindow
of the Marker
, I think so long as you are able to remove the Marker
itself, you'll no longer have a problem with it.
Found this answer that shows how to remove a marker. I think this post on iterating over hashmaps would also be helpful for you.