如何显示带标题的搜索标记并隐藏其他标记android映射

时间:2016-11-08 07:13:34

标签: android json google-maps

我使用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));
            }
        }
    }

这里是xml文件

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity" >

        <EditText
            android:id="@+id/editText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:hint="Search Here" />
 <fragment  
     android:id="@+id/map"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     class="com.google.android.gms.maps.SupportMapFragment" />

</LinearLayout>

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

INSERT INTO `tbl_answers` () VALUES ('6','9','1','5','7','10','4','12','8','3','11','2'), ('1','1','1','1','1','1','1','1','1','1','1','1'), Array

答案 1 :(得分:0)

您只需要创建一个List<Marker>来保留您的标记(我使用List代替Map,因为您需要迭代所有标记以隐藏所有这些都只有一个):

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

然后,您需要像这样添加标记,而不是将标记添加到执行googleMap.addMarker(markerOptions);的地图中,而是将它们添加到List<Marker>

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

现在,除了具有所需标题的标记外,您可以隐藏所有标记:

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

在您的代码中:

public class MainActivity extends FragmentActivity {

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

    @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 void showMarker(String title) {
        for (Marker marker : markers) {
            if (marker.getTitle().equalsIgnoreCase(title)) {
                marker.setVisible(true);
            } 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));
            }
        }
    }
}

您只需要使用所需标题调用showMarker以隐藏(但不删除)除所需标记之外的所有标记。