3

I want to ask about android app that I develop using google map in my apps. I already make multi marker on my map (1323 data so 1323 marker). So the question is:

I want to make search box to find specific marker on my map. I dont know how to find. Is it can find using title or snippet in marker

c = myDbHelper.query("Landslide", null, null, null, null,null, null);
if(c.moveToFirst()) {
    do {
        String loc = c.getString(3);
        Double lat = c.getDouble(4);
        Double lng = c.getDouble(5);
        String date = c.getString(1);
        //Integer death = c.getInt(6);

        mMap.addMarker(new MarkerOptions()
            .position(new LatLng(lat, lng))
            .title(loc + "")
            .snippet("Latitude:" + lat + " " + "Longitude:" + lng + " " + "Date:" + date + "")
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.red1)));
        } while (c.moveToNext());
    }
}

Thanks In advance who helping me, regards, Hafizul Reza

Ziem
  • 6,579
  • 8
  • 53
  • 86

2 Answers2

1

Create global List/Map/Array of your Markers:

private HashMap<String, Marker> markers = new HashMap<String,Marker>();

And fill it by Markers:

//...
Marker marker = mMap.addMarker(new MarkerOptions()
            .position(new LatLng(lat, lng))
            .title(loc + "")
            .snippet("Latitude:" + lat + " " + "Longitude:" + lng + " " + "Date:" + date + "")
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.red1)));
markers.put("Name1", marker);
//add more and more markers

Now you can easily find and get any marker:

Marker marker = markers.get("Name1");

As a first parameter of markers HashMap you can put Marker title/snippet/coordinates/... .

Volodymyr Kulyk
  • 6,455
  • 3
  • 36
  • 63
0

I gess your markers data are stored in SQLite on your device. You can try to add a search box such as EditText, in your layout xml :

<EditText android:id="@+id/edit_textt_d"  android:layout_width="wrap_content" android:layout_height="wrap_content"/>

Then implement then onChange :

    yourEditText = (EditText) findViewById(R.id.yourEditTextId);
    yourEditTextaddTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {}
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            changeMarker(s)
        } 
 });

On each change you should : delete the current marker of your map, query only markers that match the search box value :

public void changeMarker(String searchString) {
    ...
    myGoogleMap.clear();
    c=myDbHelper.getReadableDatabase().rawQuery("SELECT * FROM marker WHERE marker.name LIKE '"+searchString+"%'");
    ...
}

And then add your marker back on the map as you did in your question. Hope this help...

Lary Ciminera
  • 1,270
  • 8
  • 15
  • Dear Lary, I so confuse where to put onchange and what is "youredittext". Thanks for your reply sir. here my full code http://pastebin.com/qG3rJKtm. im so confuse. can you guide me sir . Thanks ;) – Hafizul Reza Apr 24 '15 at 03:17
  • Hey,sorry for the late answer. you can put the onchange in `onCreate(){...` of your `MapsActivity` and the `` in th xml of the file R.layout.activity_maps. – Lary Ciminera Apr 25 '15 at 20:32
  • This error come out.. The value myDbHelper.getReadableDatabase().rawQuery("SELECT * FROM marker WHERE marker.name LIKE '"+searchString+"%'") assigned to 'c' is never used less... This inspection points out the cases where a variable value is never used after its assignment, i.e.:  - the variable never gets read after assignment OR  - the value is always overwritten with another assignment before the next variable read OR  - the variable initializer is redundant (for one of the above two reasons) OR  - the variable is never used. – Hafizul Reza Apr 27 '15 at 07:12
  • my code sir. I really need your help. http://pastebin.com/mD84cJgA . can you check my coding. is it correct or not. I follow your step sir. I already comment which line error. Thanks btw Lary. I appreciate you work hard to help me – Hafizul Reza Apr 27 '15 at 07:16