Search 在Android操作栏上搜索位置

Search 在Android操作栏上搜索位置,search,location,android-actionbar,google-maps-android-api-2,google-geocoding-api,Search,Location,Android Actionbar,Google Maps Android Api 2,Google Geocoding Api,我想问一下如何使用操作栏查找位置。我做了​​程序如下所示: 我已经上了一节课来寻找位置。但在我调用EditText搜索时,类不起作用 // An AsyncTask class for accessing the GeoCoding Web Service private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{ @Override protected List&

我想问一下如何使用操作栏查找位置。我做了​​程序如下所示:

我已经上了一节课来寻找位置。但在我调用EditText搜索时,类不起作用

// An AsyncTask class for accessing the GeoCoding Web Service
private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{

    @Override
    protected List<Address> doInBackground(String... locationName) {
        // Creating an instance of Geocoder class
        Geocoder geocoder = new Geocoder(getBaseContext());
        List<Address> addresses = null;

        try {
            // Getting a maximum of 3 Address that matches the input text
            addresses = geocoder.getFromLocationName(locationName[0], 3);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return addresses;
    }
}

protected void onPostExecute(List<Address> addresses) {

    if(addresses==null || addresses.size()==0){
        Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();
    }

    // Clears all the existing markers on the map
    map.clear();

    // Adding Markers on Google Map for each matching address
    for(int i=0;i<addresses.size();i++){

        Address address = (Address) addresses.get(i);

        // Creating an instance of GeoPoint, to display in Google Map
        latLng = new LatLng(address.getLatitude(), address.getLongitude());

        String addressText = String.format("%s, %s",
        address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
        address.getCountryName());

        markerOptions = new MarkerOptions();
        markerOptions.position(latLng);
        markerOptions.title(addressText);

        map.addMarker(markerOptions);

        // Locate the first location
        if(i==0)
            map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,10));
    }
}
请帮助那些知道我遇到的问题的人。:)

String strPlace=etSearch.getText().toString();
Geocoder gc=新的地理编码器(getBaseContext(),Locale.getDefault());
列表adrs=null;
试一试{
adrs=gc.getFromLocationName(strPlace,5);
}捕获(IOE异常){
}最后{
如果(ADR!=null){
如果(adrs.size()>0)
{
LatLng loc=新LatLng(adrs.get(0.getLatitude(),adrs.get(0.getLatitude());
地图。移动摄像机(摄像机更新工厂。newLatLngZoom(位置15));
//放大,设置摄影机动画。
animateCamera(CameraUpdateFactory.zoomTo(13),2000年,空);
}

您的错误输出是什么?请提供LogCat
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.cost_direction, menu);

    /** Get the action view of the menu item whose id is search */
    View v = (View) menu.findItem(R.id.search).getActionView();

    /** Get the edit text from the action view */
    EditText txtSearch = ( EditText ) v.findViewById(R.id.txt_search);

    /** Setting an action listener */
    txtSearch.setOnEditorActionListener(new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

            // Getting user input location
            String location = v.getText().toString();

            if(location!=null && !location.equals("")){
                new GeocoderTask().execute(location);
            }

            Toast.makeText(getBaseContext(), "Search : " + v.getText(), Toast.LENGTH_SHORT).show();
            return false;
        }
    });
    return super.onCreateOptionsMenu(menu);
}
String strPlace = etSearch.getText().toString();

        Geocoder gc = new Geocoder(getBaseContext(), Locale.getDefault());
        List<Address> adrs = null;
        try{
            adrs = gc.getFromLocationName(strPlace,5);
        }catch(IOException e){

        }finally{

            if (adrs != null){
                if(adrs.size() > 0)
                {

                     LatLng loc = new LatLng(adrs.get(0).getLatitude(), adrs.get(0).getLongitude());

                     map.moveCamera(CameraUpdateFactory.newLatLngZoom(loc, 15));

                    // Zoom in, animating the camera.
                    map.animateCamera(CameraUpdateFactory.zoomTo(13), 2000, null); 
}