Hi guys! I created an array of items, each item represents a type of establishment that will be displayed on the map. What I need is to filter the items according to the button clicked, and this is where the problem comes from, since I have to call the onMapReady () method again, and I do not know if this is a good practice.
For more information, look at Android Maps API: https://developers.google.com/maps/documentation/android-api/ /** * Runs when a GoogleApiClient object successfully connects * * @param bundle */ @Override public void onConnected(@Nullable Bundle bundle) { //@TODO --> MOCK my currently location mLastLocation.setLatitude(-25.4370264); mLastLocation.setLongitude(-49.2677954); if (mLastLocation != null) { // Determine whether a Geocoder is available. if (!Geocoder.isPresent()) { showToast(getString(R.string.no_geocoder_available)); return; } mLocationRequested = true; if (mLocationRequested) { //@TODO - Add a marker array[], and move the camera for close to me. mMarkers = mockMarkersArray(); mButtonFilter = null; showCoordinatesInMap(); } } } /** * */ private void showCoordinatesInMap() { mMapFragment.getMapAsync(this); } /** * Draw Google Maps. * * @param googleMap */ @Override public void onMapReady(GoogleMap googleMap) { if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } googleMap.setMyLocationEnabled(true); googleMap.setOnMarkerClickListener(this); googleMap.setOnInfoWindowClickListener(this); googleMap.setOnInfoWindowCloseListener(this); if (mMarkers.length() > 0) { mAccomodButton.setOnClickListener(this); loopMarkers(googleMap); } //@TODO - Zooming in on the map by focusing on my currently position. LatLng currentlyPosition = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude()); googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentlyPosition, 18)); } /** * Loop of activities with markers. * * @param googleMap */ private void loopMarkers(GoogleMap googleMap) { mGoogleMap = googleMap; for (int i = 0; i < mMarkers.length(); i++) { try { JSONObject marker = mMarkers.getJSONObject(i); String[] latLng = marker.getString("latLng").split(","); double lat = Double.parseDouble(latLng[0]); double lng = Double.parseDouble(latLng[1]); LatLng position = new LatLng(lat, lng); float distanceInMeters = calcDistanceToMarker(i, position); drawMarker(googleMap, marker, position); // showToast("Distance with " + marker.getString("title") + " is: " + distanceInMeters + "m"); } catch (JSONException e) { e.printStackTrace(); } } } /** * Drawing each marker on the map. * * @param googleMap * @param marker * @param position * @throws JSONException */ private void drawMarker(GoogleMap googleMap, JSONObject marker, LatLng position) throws JSONException { googleMap.addMarker(new MarkerOptions() .title(marker.getString("title")) .snippet(marker.getString("desc")) .position(position) ); } /** * @param filter * @return */ @Nullable private JSONArray filterMarkersByType(String filter) { if (mMarkers.length() > 0) { for (int i = 0; i < mMarkers.length(); i++) { try { JSONObject marker = mMarkers.getJSONObject(i); if (filter != null) { if (! marker.getString("type").equals(filter)) { Log.d("DEBUG", "============"); Log.d("DEBUG", "Marker title: " + marker.getString("title")); Log.d("DEBUG", "Marker desc: " + marker.getString("desc")); Log.d("DEBUG", "Marker type: " + marker.getString("type")); Log.d("DEBUG", "============"); mMarkers.remove(i); } } } catch (JSONException e) { e.printStackTrace(); } } return mMarkers; } return null; } /** * Button click event. * * @param v */ @Override public void onClick(View v) { switch (v.getId()) { case R.id.accomod_button: filterMarkersByType("accomod"); mGoogleMap.clear(); onMapReady(mGoogleMap); // loopMarkers(mGoogleMap); break; } } -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/android-developers. To view this discussion on the web visit https://groups.google.com/d/msgid/android-developers/671438f7-9347-4f98-bef4-f8037b10ee20%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.

