2017-04-19 26 views
3

我试图通过从编辑文本中获取输入来搜索地图,我有两个编辑文本;一个用于源位置,另一个用于目的地,当用户输入源目标并移动到下一个(或者我们可以说失去焦点时)比“用户输入的地点在地图上用标记指出”。如何在Android中编辑文本中的文本完成后调用方法

但是在这里,当我输入GUJRAT大学比它需要GUJRAT和大学作为不同的不同价值。

et.addTextChangedListener(new TextWatcher() { 
         @Override 
         public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

         } 

         @Override 
         public void onTextChanged(CharSequence s, int start, int before, int count) { 
          // findPlaceWaypoints(); 
         } 

         @Override 
         public void afterTextChanged(Editable s) { 
          findPlaceWaypoints(); 
         } 
        }); 

但它搜索每一个字即中央PRADES然后搜索中央和PRADES和相关中央PRADES每一个可能的话。我希望所有的文本都是ONE VALUE这是在编辑文本中键入。

这是我findPlaceWaypoints方法:

private void findPlaceWaypoints() { 
     String location = et.getText().toString().toLowerCase(); 
     if (location == null || location.equals("")) { 
      // Toast.makeText(getBaseContext(), "No Place is entered", Toast.LENGTH_SHORT).show(); 
      return; 
     } 
     String url = "https://maps.googleapis.com/maps/api/geocode/json?"; 
     try { 
      // encoding special characters like space in the user input place 
      location = URLEncoder.encode(String.valueOf(location), "utf-8"); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } 
     String address = "address=" + location; 
     String sensor = "sensor=true"; 

     // url , from where the geocoding data is fetched 
     url = url + address + "&" + sensor; 

     // Instantiating DownloadTask to get places from Google Geocoding service 
     // in a non-ui thread 
     DownloadTask downloadTask = new DownloadTask(); 

     // Start downloading the geocoding places 
     downloadTask.execute(url); 
    } 
+0

你能详细解释你的查询吗? –

+0

你需要它来进行搜索吗? –

+0

如果你用相同的编辑文本编写它,那么它就像古吉拉特大学一样。 –

回答

0

试试这个它可以帮助你:

edSearch.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       String strname = edSearch.getText().toString().toLowerCase(); 

      } 

      @Override 
      public void afterTextChanged(Editable s) { 
      } 
     }); 
+1

其仍将每个单词视为不同的不同值, –

0

我不认为这有什么关系EditText上,它与做地理编码api。它通常发送的是一组对象(Places),如下面的模板。

results[]: { 
types[]: string, 
formatted_address: string, 
address_components[]: { 
    short_name: string, 
    long_name: string, 
    postcode_localities[]: string, 
    types[]: string 
}, 
partial_match: boolean, 
place_id: string, 
postcode_localities[]: string, 
geometry: { 
    location: LatLng, 
    location_type: GeocoderLocationType 
    viewport: LatLngBounds, 
    bounds: LatLngBounds 
} 
} 

这些对象是所有匹配查询的地方(即MADHYA PRADES,MADHYA,PRADES)。

您可以检查确切匹配的内容是响应对象中名为“partial_match”的字段。如果partial_match为false,则为完全匹配,否则为部分匹配。

希望这会有所帮助。

相关问题