Android 如何在单击工具栏中的搜索图标并在googlemap片段中显示位置的同时集成searchview?
我想在工具栏中的searchview中搜索位置,并将结果作为标记显示在Google地图片段中。在我的代码中,我希望在searchview中执行操作,我尝试了其他几种方法,但没有得到任何结果 公共类MainActivity扩展了AppCompatActivity在MapreadyCallback上实现的AppCompatActivity{Android 如何在单击工具栏中的搜索图标并在googlemap片段中显示位置的同时集成searchview?,android,android-layout,google-maps-markers,searchview,Android,Android Layout,Google Maps Markers,Searchview,我想在工具栏中的searchview中搜索位置,并将结果作为标记显示在Google地图片段中。在我的代码中,我希望在searchview中执行操作,我尝试了其他几种方法,但没有得到任何结果 公共类MainActivity扩展了AppCompatActivity在MapreadyCallback上实现的AppCompatActivity{ //Creating the searchbar option private MenuItem mSearchaction; private boolean
//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();
}
//创建搜索栏选项
私有菜单项mSearchaction;
私有布尔值Issearchoped=false;
私人编辑文本搜索;
地理编码器;
//创建地图
私有谷歌地图;
//从地图存储经度和纬度的步骤
私人双液化天然气;
私人双lat;
//From->我们需要计算距离的第一个坐标
私家双经;
私人双纬度;
//到->第二个坐标,我们需要计算距离
私人双重容忍;
私人双重容忍;
//谷歌ApiClient
私人GoogleapClient GoogleapClient;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//添加我们的地图片段
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
}
私人无效手册内容(意图){
}
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
mMap=谷歌地图;
goToLocation(50.8278,12.9214);
}
私人空位(双lat、双lng){
LatLng LatLng=新LatLng(lat,lng);
CameraUpdate update=CameraUpdateFactory.newLatLng(latLng);
移动摄像头(更新);
mMap.getUiSettings().iscompassabled();
mMap.getUiSettings().isZoomControlsEnabled();
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(右菜单,菜单,菜单);
mSearchaction=menu.findItem(R.id.search_按钮);
返回super.onCreateOptions菜单(菜单);
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例R.id.search_按钮:
handleMenuSearch();
返回true;
案例R.id.action\u设置:
返回true;
案例R.id.maptype_正常:
mMap.setMapType(GoogleMap.MAP\u TYPE\u NORMAL);
如果(item.isChecked())item.setChecked(false);
else item.setChecked(true);
打破
案例R.id.maptype_卫星:
mMap.setMapType(GoogleMap.MAP_TYPE_卫星);
如果(item.isChecked())item.setChecked(false);
else item.setChecked(true);
打破
案例R.id.maptype_地形:
mMap.setMapType(GoogleMap.MAP\u TYPE\u地形);
如果(item.isChecked())item.setChecked(false);
else item.setChecked(true);
打破
违约:
打破
}
返回super.onOptionsItemSelected(项目);
}
私有void handleMenuSearch(){
ActionBar action=getSupportActionBar();//获取ActionBar
如果(已删除)
{
action.setDisplayShowCustomEnabled(false);
action.setDisplayHomeAsUpEnabled(true);
InputMethodManager imm=(InputMethodManager)getSystemService(Context.INPUT\u方法\u服务);
imm.hideSoftInputFromWindow(edtSearch.getWindowToken(),0);
setIcon(getResources().getDrawable(R.drawable.ic_search_black_24dp));
Issearchoped=false;
}
否则{//打开搜索条目
action.setDisplayShowCustomEnabled(true);
action.setCustomView(R.layout.search\u栏);
action.setDisplayShowTitleEnabled(false);
edtSearch=(EditText)action.getCustomView().findViewById(R.id.edtSearch);//获取文本编辑器
edtSearch.setOnEditorActionListener(新的TextView.OnEditorActionListener(){
@凌驾
公共布尔onEditorAction(TextView v、int actionId、KeyEvent事件){
if(actionId==EditorInfo.IME\u ACTION\u SEARCH){
doSearch();
返回true;
}
返回false;
}
});
edtSearch.requestFocus();
//打开搜索中的键盘
InputMethodManager imm=(InputMethodManager)getSystemService(Context.INPUT\u方法\u服务);
imm.showSoftInput(edtSearch,InputMethodManager.SHOW\u隐式);
//添加关闭图标
setIcon(getResources().getDrawable(R.drawable.ic_search_black_24dp));
Issearchoped=true;
}
}
私有void doSearch(){
EditText位置\字段=(EditText)findViewById(R.id.edtsearch);
字符串位置=位置\字段.getText().toString();
List addressList=null;
if(location!=null | |!location.equals(“”);
{
Geocoder Geocoder=新的Geocoder(本);
{
试一试{
addressList=geocoder.getFromLocationName(位置,1);
}捕获(IOE异常){
e、 printStackTrace();
}
地址=地址列表。获取(0);
LatLng LatLng=新LatLng(address.getLatitude(),address.getLongitude());
mMap.addMarker(新的MarkerOptions().position(latLng).title(“Marker”);
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.getUiSettings().iscompassabled();
mMap.getUiSettings().isZoomControlsEnabled();
}
}
@凌驾
public void onBackPressed(){
如果(已删除){
handleMenuSearch();
返回;
}
super.onBackPressed();
}