My mock markers array is:

/**
 * @return
 * @TODO - Mock markers in maps
 */
private JSONArray mockMarkersArray() {
    JSONObject myMarkerOne = new JSONObject();
    try {
        myMarkerOne.put("id", 1);
        myMarkerOne.put("title", "Shopping Estação");
        myMarkerOne.put("desc", "Localizado em uma antiga estação ferroviária, 
foi inaugurado em 1997 e ...");
        myMarkerOne.put("type", "shopping");
        myMarkerOne.put("latLng", "-25.4381796,-49.2664607");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    JSONObject myMarkerTwo = new JSONObject();
    try {
        myMarkerTwo.put("id", 2);
        myMarkerTwo.put("title", "Câmara Municipal de Curitiba");
        myMarkerTwo.put("desc", "");
        myMarkerTwo.put("type", "administ");
        myMarkerTwo.put("latLng", "-25.4362749,-49.2666494");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    JSONObject myMarkerThree = new JSONObject();
    try {
        myMarkerThree.put("id", 3);
        myMarkerThree.put("title", "Hostel Roma");
        myMarkerThree.put("desc", "Hotel de 1 estrela. Reserve o seu quarto!");
        myMarkerThree.put("type", "accomod");
        myMarkerThree.put("latLng", "-25.4369412,-49.2659414");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    JSONObject myMarkerFour = new JSONObject();
    try {
        myMarkerFour.put("id", 4);
        myMarkerFour.put("title", "Slaviero Conceptual Rockefeller");
        myMarkerFour.put("desc", "Classificado entre os 10% dos melhores hotéis 
desta área.");
        myMarkerFour.put("type", "accomod");
        myMarkerFour.put("latLng", "-25.4383619,-49.2653989");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    JSONObject myMarkerFive = new JSONObject();
    try {
        myMarkerFive.put("id", 5);
        myMarkerFive.put("title", "Queen's Snooker Burger Bar");
        myMarkerFive.put("desc", "");
        myMarkerFive.put("type", "bar");
        myMarkerFive.put("latLng", "-25.4341294,-49.2665008");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    JSONObject myMarkerSix = new JSONObject();
    try {
        myMarkerSix.put("id", 6);
        myMarkerSix.put("title", "Bradesco");
        myMarkerSix.put("desc", "");
        myMarkerSix.put("type", "bank");
        myMarkerSix.put("latLng", "-25.4381796,-49.2664607");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    JSONObject myMarkerSeven = new JSONObject();
    try {
        myMarkerSeven.put("id", 7);
        myMarkerSeven.put("title", "Veterinária Prado");
        myMarkerSeven.put("desc", "");
        myMarkerSeven.put("type", "petshop");
        myMarkerSeven.put("latLng", "-25.4367331,-49.2649339");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    JSONObject myMarkerEight = new JSONObject();
    try {
        myMarkerEight.put("id", 8);
        myMarkerEight.put("title", "Praça Eufrásio Correia");
        myMarkerEight.put("desc", "");
        myMarkerEight.put("type", "square");
        myMarkerEight.put("latLng", "-25.4367893,-49.2670166");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    JSONObject myMarkerNine = new JSONObject();
    try {
        myMarkerNine.put("id", 9);
        myMarkerNine.put("title", "CM Shop");
        myMarkerNine.put("desc", "");
        myMarkerNine.put("type", "techcel");
        myMarkerNine.put("latLng", "-25.4372394,-49.2690715");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    JSONArray myMarkers = new JSONArray();
    try {
        myMarkers.put(0, myMarkerOne);
        myMarkers.put(1, myMarkerTwo);
        myMarkers.put(2, myMarkerThree);
        myMarkers.put(3, myMarkerFour);
        myMarkers.put(4, myMarkerFive);
        myMarkers.put(5, myMarkerSix);
        myMarkers.put(6, myMarkerSeven);
        myMarkers.put(7, myMarkerEight);
        myMarkers.put(8, myMarkerNine);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return myMarkers;
}



At this point I tried to remove the items I did not want but then I could 
see that not all items are removed on the first click, so I need to create 
an arraylist and just add the items I need to show on the map.


Em quarta-feira, 23 de novembro de 2016 22:22:42 UTC-2, Francis Rodrigues 
escreveu:
>
> 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/3e372a67-daf7-4f0e-928b-5078d4c007b2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to