문제

I need to implement the search function on my custom map application like it is done in native google maps(the action bar turns into search field and you can write your query). Now I know how to use google geocoding api, and how to retrieve location from data. But I have failed to implement that changable actionbar.

My app looks like that:

enter image description here

And after I push the search button I would like to have this kind of layout displayed:

enter image description here

Thanks for the help, hopefully you can solve my problem.

도움이 되었습니까?

해결책

Here is some code for your search facility. This code work with me. If you enter the location name, this will redirect you on the map to the exact match place. The following code provide two way of searching, first by Name and second by LatLang.

  1. By Name og Location

    public void searchPlace()
    {       
    
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
    
        alert.setTitle("Search Location");
        alert.setMessage("Enter Location Name: ");
    
        // Set an EditText view to get user input 
        final EditText input = new EditText(this);
        alert.setView(input);
    
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
          String value = input.getText().toString();
          // Do something with value!
          Log.d("value", value);
    
          Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());    
            try {
                List<Address> addresses = geoCoder.getFromLocationName(
                    value, 5);
                String add = "";
                if (addresses.size() > 0) {
                    p = new GeoPoint(
                            (int) (addresses.get(0).getLatitude() * 1E6), 
                            (int) (addresses.get(0).getLongitude() * 1E6));
                    mc.animateTo(p);    // create mapController object like `MapController mc = mapView.getController();`
                    mapView.invalidate();
                }    
            } catch (IOException e) {
                e.printStackTrace();
            }
    
    
          }
        });
    
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int whichButton) {
            // Canceled.
          }
        });
    
        alert.show();
    
    }  
    
  2. By LAtLang.

    public void byLatLang()
    {       
    
        LayoutInflater factory = LayoutInflater.from(this);            
        final View textEntryView = factory.inflate(R.layout.latlong, null);
    
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
    
        alert.setTitle("Search Location");
        alert.setMessage("Enter Lattitude and Longitude: ");
    
        alert.setView(textEntryView); 
        // Set an EditText view to get user input
        AlertDialog latLongPrompt = alert.create();
    
        final EditText lat = (EditText) textEntryView.findViewById(R.id.lat);
        final EditText longi = (EditText) textEntryView.findViewById(R.id.longi);
    
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener()   {
        public void onClick(DialogInterface dialog, int whichButton) {
    
            Toast.makeText(getBaseContext(), "clicked ok ", Toast.LENGTH_SHORT).show();
          Double value1 = Double.parseDouble(lat.getText().toString());
          Double value2 = Double.parseDouble(longi.getText().toString());
          // Do something with value!
                      //Log.d("value1", value1);
              //Log.d("value2", value2);
    
          p = new GeoPoint(
                    (int) (value1 * 1E6), 
                    (int) (value2 * 1E6));
    
                mc.animateTo(p);
                mc.setZoom(17); 
                mapView.invalidate();
    
    
          }
        });
    
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int whichButton) {
            // Canceled.
          }
        });
    
        alert.show();
    
    }  
    
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top