1

I asked one question about how to get a Google Map V2 in android application. Now i want to put one functionality on that Google Map. Anyone can suggeset me a good answer, I m trying to get it with Geocode. Thanks in advance.

1 Answers1

0

This code you can put in your onCreateOptionMenu of your activity

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.show_map, menu);

    SearchManager searchManger =  (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView =  (SearchView) menu.findItem(R.id.action_search).getActionView();

    searchView.setSearchableInfo(searchManger.getSearchableInfo(getComponentName()));
    //searchView.setIconifiedByDefault(false);

    SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener()
    {

        @Override
        public boolean onQueryTextSubmit(String newText) 
        {
            return true;
        }

        @Override
        public boolean onQueryTextChange(String query) 
        {
            Geocoder geocoder = new Geocoder(CreateReminderActivity.this);  

            List<Address> addresses;

                    String areaname = edLocation.getText().toString();

                    addresses = geocoder.getFromLocationName(areaname, 1);

                    if(addresses.size() > 0) {

                        double latitude= addresses.get(0).getLatitude();

                        double longitude= addresses.get(0).getLongitude();

                        insLat = latitude;

                        insLong = longitude;

                        String statename = addresses.get(0).getAdminArea();

                        googleMap.addMarker(new MarkerOptions()
                        .title(areaname)
                        .snippet("SNIPPET")
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))
                        .anchor(0.0f, 1.0f) // Anchors the marker on the bottom left
                        .position(new LatLng(latitude, longitude)));

                        LatLng latLng = new LatLng(latitude, longitude);

                        googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

                     // Zoom in the Google Map
                     googleMap.animateCamera(CameraUpdateFactory.zoomTo(10));

                     Toast.makeText(getApplicationContext(), statename, 20).show(); 

            return true;
        }
    };


    searchView.setOnQueryTextListener(queryTextListener);



    return true;
}
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Gulnaz Ghanchi
  • 485
  • 3
  • 14