如何在点击工具栏中的搜索图标时集成搜索视图并在googlemap片段中显示位置?

时间:2016-07-15 01:43:57

标签: android android-layout google-maps-markers searchview

我想搜索工具栏中显示的搜索结果中的地点,并将结果作为标记显示在googlemap片段中。在我的代码中,我希望在searchview中执行的操作,我已经尝试了其他几种方法,但我没有计算任何结果。

公共类MainActivity扩展AppCompatActivity实现OnMapReadyCallback {

//Creating  the searchbar option
private MenuItem mSearchaction;
private boolean isSearchOpened = false;
private EditText edtSearch;

Geocoder geocoder;



//Creating map
private GoogleMap mMap;


//To store longitude and latitude from map
private double lng;
private double lat;

//From -> the first coordinate from where we need to calculate the distance
private double fromLongitude;
private double fromLatitude;

//To -> the second coordinate to where we need to calculate the distance
private double toLongitude;
private double toLatitude;

//Google ApiClient
private GoogleApiClient googleApiClient;




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



      // Adds our map fragment
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

private void handleIntent(Intent intent) {
}


@Override
public void onMapReady(GoogleMap googleMap) {

     mMap = googleMap;
    goToLocation(50.8278,12.9214);

}
  private void goToLocation(double lat, double lng){
      LatLng latLng = new LatLng(lat,lng);
      CameraUpdate update = CameraUpdateFactory.newLatLng(latLng);
      mMap.moveCamera(update);

      mMap.getUiSettings().isCompassEnabled();
      mMap.getUiSettings().isZoomControlsEnabled();

  }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu,menu);
    mSearchaction = menu.findItem(R.id.search_button);


    return super.onCreateOptionsMenu(menu);
}



@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){

        case R.id.search_button:
            handleMenuSearch();
            return true;
        case R.id.action_settings:
            return true;

        case R.id.maptype_normal:
            mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            if (item.isChecked()) item.setChecked(false);
            else item.setChecked(true);
            break;

        case R.id.maptype_satellite:
            mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
            if (item.isChecked()) item.setChecked(false);
            else item.setChecked(true);
            break;

        case R.id.maptype_terrain:
            mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
            if (item.isChecked()) item.setChecked(false);
            else item.setChecked(true);
            break;
       default:
           break;
    }






    return super.onOptionsItemSelected(item);
}
private void handleMenuSearch() {

    ActionBar action = getSupportActionBar(); // get the actionbar

    if(isSearchOpened)

    {



        action.setDisplayShowCustomEnabled(false);
        action.setDisplayHomeAsUpEnabled(true);

        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(edtSearch.getWindowToken(), 0);

        mSearchaction.setIcon(getResources().getDrawable(R.drawable.ic_search_black_24dp));

        isSearchOpened = false;





    }

    else{// open the search entry

        action.setDisplayShowCustomEnabled(true);
        action.setCustomView(R.layout.search_bar);
        action.setDisplayShowTitleEnabled(false);

        edtSearch= (EditText) action.getCustomView().findViewById(R.id.edtsearch); // get the text editor

        edtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {

                    doSearch();
                    return true;
                }

                return false;

            }


        });


        edtSearch.requestFocus();

        //open the keyboard focused in the edtSearch
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(edtSearch, InputMethodManager.SHOW_IMPLICIT);


        //add the close icon
        mSearchaction.setIcon(getResources().getDrawable(R.drawable.ic_search_black_24dp));

        isSearchOpened = true;





    }


}

private void doSearch() {
    EditText location_field = (EditText) findViewById(R.id.edtsearch);
    String location = location_field.getText().toString();
    List<Address> addressList = null;
    if (location != null || !location.equals("")) ;
    {
        Geocoder geocoder = new Geocoder(this);
        {
            try {
                addressList = geocoder.getFromLocationName(location, 1);
            } catch (IOException e) {
                e.printStackTrace();
            }

            Address address = addressList.get(0);
            LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
            mMap.addMarker(new MarkerOptions().position(latLng).title("Marker"));
            mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
            mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

            mMap.getUiSettings().isCompassEnabled();
            mMap.getUiSettings().isZoomControlsEnabled();
    }

}


@Override
public void onBackPressed() {
    if(isSearchOpened) {
        handleMenuSearch();
        return;
    }
    super.onBackPressed();
}

0 个答案:

没有答案