我使用JSON和PHP从数据库中检索了几个标记,现在我想按标题搜索标记并显示带标题的标记并隐藏其他标记。
这是java代码
public class MainActivity extends FragmentActivity {
private GoogleMap googleMap;
private List<Marker> markers = new ArrayList<>();
// Search EditText
EditText inputSearch;
@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();
inputSearch = (EditText) findViewById(R.id.editText);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// When user changed the Text
showMarker(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
}
private void showMarker(String s) {
for (Marker marker : markers) {
if (marker.getTitle().equalsIgnoreCase(name)) {
marker.setVisible(true);
marker.showInfoWindow();
} else {
marker.setVisible(false);
}
}
}
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));
markers.add(googleMap.addMarker(markerOptions));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(24.66372, 93.90627), 8.0f));
}
}
}
答案 0 :(得分:0)
您应该为标记创建一个HashMap。
private HashMap<Marker, AnyObject> msHashMap;
Marker mkr = mMap.addMarker(newMarkerOptions().position(latLng).title(anyobject.getPsName()));
msHashMap.put(mkr, anyobject);
这里AnyObject应该是在从JSON解析后存储数据的Object类型。
还有一个输入 使用Volley或okHttp进行REST请求响应 和GSON解析JSON。
希望有所帮助......