Android - 使用我自己的过滤器和对象的Arraylist绘制标记

时间:2016-11-24 03:27:49

标签: android google-maps arraylist

我创建了一个项目数组,每个项目代表一种将在地图上显示的机构。 我需要的是根据点击的按钮过滤项目,这就是问题的来源, 因为我必须再次调用onMapReady()方法,我不知道这是不是一个好习惯。

有关详情,请参阅Android Maps API:

https://developers.google.com/maps/documentation/android-api/

...

public void onConnected(@Nullable Bundle bundle) {

    ...

    mMarkers = mockMarkersArray();
    markers = new HashMap<>();
    mButtonFilter = null;
}

/**
 * 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, null);
    }

    //@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, String filter) {
    mGoogleMap = googleMap;

    mGoogleMap.clear();
    markers.clear();

    for (int i = 0; i < mMarkers.length(); i++) {
        try {
            JSONObject markerObj = mMarkers.getJSONObject(i);

//                if (markerObj.getString("type").equals("accomod")) {

                Log.d("DEBUG", "============");
                Log.d("DEBUG", "Marker title: " + markerObj.getString("title"));
                Log.d("DEBUG", "Marker desc: " + markerObj.getString("desc"));
                Log.d("DEBUG", "Marker type: " + markerObj.getString("type"));
                Log.d("DEBUG", "============");

                String[] latLng = markerObj.getString("latLng").split(",");
                double lat = Double.parseDouble(latLng[0]);
                double lng = Double.parseDouble(latLng[1]);

                LatLng position = new LatLng(lat, lng);

                MarkerOptions markerOptions = new MarkerOptions()
                        .title(markerObj.getString("title"))
                        .snippet(markerObj.getString("desc"))
                        .position(position);
                Marker marker = mGoogleMap.addMarker(markerOptions);
                markers.put(marker, mMarkers);
//                }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

/**
 * @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();
    }
}

/**
 * Button click event.
 *
 * @param v
 */
@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.accomod_button:

            loopMarkers(mGoogleMap, "accomod");

            break;
    }
}
```

此时我试图删除我不想要的项目,但后来我发现第一次点击时并没有删除所有项目,所以我需要创建一个arraylist并添加我需要显示的项目地图。

/**
 * @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;
}
```

0 个答案:

没有答案