Search box for finding a marker (Google Map)

I want to ask about an android app that I am developing using google map in my apps. I am already making some markers on my map (1323 data, so the marker is 1323). So the question is:

I want to create a search box to find a specific marker on my map. I don't know how to find it. Can it find the title or snippet in the 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 helps me, Regards, Hafizul Reza

+3


source to share


3 answers


I'm pretty sure your token data is stored in SQLite on your device. You can try to add a search box like EditText to your xml layout:

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

      

Then we 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 every change, you must: remove the current marker of your map, only ask for markers that match the value of the search box:

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

      

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

0


source


Create a global list / map / array of your markers:

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

      


And fill it with 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 the first parameter of the HashMap markers, you can put the title / fragment Marker / coordinates / ....

+1


source


did you get a solution? I am also facing the same problem. could you help me? I want to create a search bar in a Google Maps Android project where I can find my own marker locations. I have 70 markers in my Google Map project. Thank you so much.

0


source







All Articles