How to Search in Google Map Api V2 Android?

35,950

You can use geocoding to search or see this nice tutorial which uses json parsing GoogleMaps.

Share:
35,950
sr.farzad
Author by

sr.farzad

Updated on July 09, 2022

Comments

  • sr.farzad
    sr.farzad almost 2 years

    I'm developing an Android application and using GoogleMaps API v2. This application works so far but I want search in the map with a path:

    Screenshot :

    GoogleMaps Screenshot

    my codes:

    Position_mark = new LatLng(35.7008, 51.437);
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(Position_mark, 15));
    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
    map.setOnMapClickListener(this);
    map.setOnMapLongClickListener(this);
    map.setOnMarkerDragListener(this);
    

    ...

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //Used to put dark icons on light action bar
    
        mGoItem = menu.add(0, Delete_ITEM_ID, 0, null);
        mGoItem.setIcon(R.drawable.ic_launcher)
               .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    
        SearchView searchView = 
            new SearchView(getSupportActionBar().getThemedContext());
        searchView.setQueryHint("Search for Places…");
        searchView.setIconified(false);
    
        menu.add("Search")
            .setIcon(R.drawable.abs__ic_search)
            .setActionView(searchView)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    
            @Override
            public boolean onQueryTextSubmit(String newText) {
                return true;
            }
    
            /**
             * Called when the query text is changed by the user.
             * @param newText the new content of the query text field.
             * @return false if the SearchView should perform the 
             * default action of showing any suggestions if available, 
             * true if the action was handled by the listener.
             */
            @Override
            public boolean onQueryTextChange(String newText) {
                Toast.makeText(getApplicationContext(), newText, 1).show();
                return true;
            }
        });
    
        return true;
    }
    
    @Override
    public void onMapClick(LatLng point) {
        // TODO Auto-generated method stub
        // tvLocInfo.setText(point.toString());
        map.animateCamera(CameraUpdateFactory.newLatLng(point));
        map.clear();
    
        Marker Kiel = map.addMarker(new MarkerOptions()
            .position(point)
            .title("Kiel")
            .snippet("Kiel is cool").draggable(true)
            .icon(BitmapDescriptorFactory
            .fromResource(R.drawable.ic_launcher)));
    }
    

    I use ActionBarSherlock and SearchView for searching. How to search in GoogleMaps API with path not latitute or longitude?

  • sr.farzad
    sr.farzad over 10 years
    this example is very good for me..thanks wptrafficanalyzer.in/blog/…
  • Sagar Maiyad
    Sagar Maiyad over 10 years
    @Kavyan its not good to use geocoding for searching location in google map. because its not working in all devices..
  • smith324
    smith324 over 10 years
    @Segi It should work on the same devices that have access to the Google Maps V2 API (available through Play Services) because the geocoder in use is powered by maps