How can I filter my location using AutoCompleteTextView in Google Map Android?

  • Replies:7
bharathsk
  • Forum posts: 4

Sep 13, 2015, 5:23:57 AM via Website

Hi,

I am using the MapActivity with AutoCompleteTextView, Button, and Map Fragment. Mapactivity.xml file attachedimage
I want to filter my location using the AutoCompleteTextView.My sqlite database consist as follows:

ID Condition Latitude Longitude
1 Excellent 12.897485 15.425974
2 Good 12.897485 15.425974
3 Excellent 12.897485 15.425974
4 Poor 12.897485 15.425974
5 Poor 12.897485 15.425974
6 Excellent 12.897485 15.425974
7 Fair 12.897485 15.425974
8 Good 12.897485 15.425974
9 Good 12.897485 15.425974
10 Excellent 12.897485 15.425974

If I select "Excellent" in my AutoCompleteTextView and press Show Map Button. It Should show the location of my "Excellent" only with Marker i.e ID . Example I select "Good", Show the location for ID 2,8,& 9.Please Help me How can I write a code in Android Studio.

Reply
Ashish Tripathi
  • Forum posts: 211

Sep 14, 2015, 7:56:07 AM via Website

Ok so locations are in your local database.

For Studio first introduce the google play services in your project gradle file. (rather like in eclipse import the play services as a lib project). Also activate the google map services from google console. mention play services meta data in you project manifest.xml.

on click of show button fetch the lat ,long from your db and pass to location.

Reply
bharathsk
  • Forum posts: 4

Sep 14, 2015, 9:36:31 AM via Website

Ashish,

I already have my G play service in gradle. My question is How can I filter it. Right now I am getting all the 10 location in Map. I want them to filter like If I type "Good", It should show only Good location. Pl see my db table above.

Reply
Ashish Tripathi
  • Forum posts: 211

Sep 14, 2015, 10:24:58 AM via Website

indeed i have seen your table. when you are typing like good only get the coordinate of that. i think your query is fetching all the location and send to the map. can you share that piece of code so i will have some idea to help you.

Reply
bharathsk
  • Forum posts: 4

Sep 14, 2015, 10:48:03 AM via Website

Ashish here is my code: I have used "Ifelse condition" If my textview is empty display all location else use to filter the location. Pl help me to do coding for this

private AutoCompleteTextView inputSearch;
String[] conditions = {"Excellent",
"Good",
"Fair",
"Bad",

};



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    setUpMapIfNeeded();


    inputSearch = (AutoCompleteTextView) findViewById(R.id.et_search);
    ArrayAdapter adapter1 = new ArrayAdapter(this, android.R.layout.select_dialog_item, conditions);
    inputSearch.setThreshold(1);
    inputSearch.setAdapter(adapter1);
    btMap = (Button) findViewById(R.id.MapButton);


    //At layout actitvity-maps used onClick = onsearch to used the MapButton as listern

    btMap.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String search = inputSearch.getText().toString();

            //When My autocompletetextview is empty and press button show all the location
            if(search.equals("")) {
                double latitude = 0;
                double longitude = 0;
                // SQLite database handler
                db = new TreeData(getApplicationContext());



                Cursor locationCursor = db.getLocations();

                locationCursor.moveToFirst();

                do {
                    latitude = locationCursor.getDouble(locationCursor
                            .getColumnIndex(SQLiteHandler.KEY_TLATITUDE));
                    longitude = locationCursor.getDouble(locationCursor
                            .getColumnIndex(SQLiteHandler.KEY_TLONGITUDE));

                    LatLng location = new LatLng(latitude, longitude);

                    mMap.addMarker(new MarkerOptions().position(location).title("Marker"));


                } while (locationCursor.moveToNext());

            }else{
            //When My autocompletetextview is "Condition" and press button show only selected location
                Toast.makeText(getApplicationContext(), "Enter the search", Toast.LENGTH_LONG).show();
        }

    }
});}

Reply
Ashish Tripathi
  • Forum posts: 211

Sep 14, 2015, 11:19:11 AM via Website

else{
//When My autocompletetextview is "Condition" and press button show only selected location
Toast.makeText(getApplicationContext(), "Enter the search", Toast.LENGTH_LONG).show();
}

if your auto complete text has condition that this else will execute. write your code here for input location filter i.e. if good than only coordinates for good will fetch out from db.

Reply
bharathsk
  • Forum posts: 4

Sep 14, 2015, 11:22:31 AM via Website

Ashish, How to write a code to do that?

Reply
Ashish Tripathi
  • Forum posts: 211

Sep 14, 2015, 12:03:19 PM via Website

what you can do like i am assuming that your columns are id,condition, latitude,longitude.

inside your else condition you can pass the query to your database class,

select latitude,longitude from 'table name' where condition = 'enter condition'.

the cursor will return only input condition latitude and longitude if exist. also catch the null pointer exception in case no data found.

Reply