Жїф Зх Жїф Зх - 29 days ago 16
JSON Question

How to show search marker with title and hide others marker android map

i retrieve several marker from database using json and php , Now i want to search marker by title and show that marker with title and hide other marker.
here is java code

public class MainActivity extends FragmentActivity {

private GoogleMap googleMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment fragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
googleMap = fragment.getMap();
// googleMap.setMyLocationEnabled(true);
new RetrieveTask().execute();
}

private class RetrieveTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
String strUrl = "http://192.168.43.229/location_marker_mysql/retrieve.php";
URL url = null;
StringBuffer sb = new StringBuffer();

try {
url = new URL(strUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream iStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(iStream));
String line = "";
while ((line = reader.readLine()) != null) {
sb.append(line);
}

reader.close();
iStream.close();

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
new ParserTask().execute(result);
}
}

MarkerOptions markerOptions = new MarkerOptions();
String name;
LatLng latLng;
double lat;
double lng;

private class ParserTask extends AsyncTask<String, Void, List<HashMap<String, String>>> {
@Override
protected List<HashMap<String, String>> doInBackground(String... params) {
MarkerJSONParser markerParser = new MarkerJSONParser();
JSONObject json = null;
try {
json = new JSONObject(params[0]);
} catch (JSONException e) {
e.printStackTrace();
}
List<HashMap<String, String>> markersList = markerParser.parse(json);
return markersList;
}

@Override
protected void onPostExecute(List<HashMap<String, String>> result) {
for (int i = 0; i < result.size(); i++) {
HashMap<String, String> hmPlace = result.get(i);
lat = Double.parseDouble(hmPlace.get("lat"));
lng = Double.parseDouble(hmPlace.get("lng"));
name = hmPlace.get("name");
String locality = hmPlace.get("locality");
String city = hmPlace.get("city");
latLng = new LatLng(lat, lng);
markerOptions.position(latLng);
markerOptions.title(name);
markerOptions.snippet(locality + "," + city);
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.school_marker));
googleMap.addMarker(markerOptions);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(24.66372, 93.90627), 8.0f));
}
}
}
}


Any help will be much appreciated.

Answer

You just need to create a List<Marker> to hold your markers (I'm using a List instead of a Map because you need to iterate over all your markers to hide all of them but one):

private List<Marker> markers = new ArrayList<>();

Then, instead of adding your markers to the map doing googleMap.addMarker(markerOptions); you will need to add them like this to also add them to the List<Marker>:

markers.add(googleMap.addMarker(markerOptions));

Now you can hide all your markers except the one with the desired title:

private Marker showMarker(String title) {
    for (Marker marker : markers) {
        if (marker.getTitle().equalsIgnoreCase(title)) {
            marker.setVisible(true);
        } else {
            marker.setVisible(false);
        }
    }
}