0

我想询问有关使用操作栏查找位置的问题。我制作的程序如下图:

我做了一个类来寻找位置。但是在我调用 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));
    }
}

我不得不把班级叫到班级行动栏菜单。像这样:

@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);
}

请帮助那些知道我遇到的问题的人。:)

4

1 回答 1

0
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); 
}
于 2013-05-28T16:56:09.193 回答