gpt4 book ai didi

android - 如何提高谷歌地点自动完成建议的性能?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:48:30 25 4
gpt4 key购买 nike

我在我的应用程序中使用 google places autocomplete suggestions。它工作正常,但我想提高它的性能。当用户输入一个地方时,它会在长时间延迟后或有时在删除最后一个字符后给出建议。我如何提高它的性能?请帮我。提前致谢

这是我的代码

public class invoice extends Activity
{

AutoCompleteTextView edit_destination;
DownloadTask placesDownloadTask;
DownloadTask placeDetailsDownloadTask;
ParserTask placesParserTask;
ParserTask placeDetailsParserTask;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.create_invoice_activity);

edit_destination=(AutoCompleteTextView) findViewById(R.id.destination);
edit_destination.setThreshold(1);

edit_destination.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Creating a DownloadTask to download Google Places matching "s"
placesDownloadTask = new DownloadTask(PLACES);

// Getting url to the Google Places Autocomplete api
String url = getAutoCompleteUrl(s.toString());

// Start downloading Google Places
// This causes to execute doInBackground() of DownloadTask class
placesDownloadTask.execute(url);
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});

edit_destination.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int index,
long id) {

ListView lv = (ListView) arg0;
SimpleAdapter adapter = (SimpleAdapter) arg0.getAdapter();

HashMap<String, String> hm = (HashMap<String, String>) adapter.getItem(index);


selected_place=hm.get("description");


// Creating a DownloadTask to download Places details of the selected place
placeDetailsDownloadTask = new DownloadTask(PLACES_DETAILS);

// Getting url to the Google Places details api
String url = getPlaceDetailsUrl(hm.get("reference"));

// Start downloading Google Place Details
// This causes to execute doInBackground() of DownloadTask class
placeDetailsDownloadTask.execute(url);

}
});

}


private String getAutoCompleteUrl(String place){

// Obtain browser key from https://code.google.com/apis/console
String key = "YOUR KEY";

// place to be be searched
String input = "input="+place;

// place type to be searched
String types = "types=geocode";

// Sensor enabled
String sensor = "sensor=false";

// Building the parameters to the web service
String parameters = input+"&"+types+"&"+sensor+"&"+key;

// Output format
String output = "json";

// Building the url to the web service
String url = "https://maps.googleapis.com/maps/api/place/autocomplete/"+output+"?"+parameters;

return url;
}


private String getPlaceDetailsUrl(String ref){

// Obtain browser key from https://code.google.com/apis/console
String key = "YOUR KEY";

// reference of place
String reference = "reference="+ref;

// Sensor enabled
String sensor = "sensor=false";

// Building the parameters to the web service
String parameters = reference+"&"+sensor+"&"+key;

// Output format
String output = "json";

// Building the url to the web service
String url = "https://maps.googleapis.com/maps/api/place/details/"+output+"?"+parameters;

return url;
}

/** A method to download json data from url */
private String downloadUrl(String strUrl) throws IOException{
String data = "";
InputStream iStream = null;
HttpURLConnection urlConnection = null;
try{
URL url = new URL(strUrl);

// Creating an http connection to communicate with url
urlConnection = (HttpURLConnection) url.openConnection();

// Connecting to url
urlConnection.connect();

// Reading data from url
iStream = urlConnection.getInputStream();

BufferedReader br = new BufferedReader(new InputStreamReader(iStream));

StringBuffer sb = new StringBuffer();

String line = "";
while( ( line = br.readLine()) != null){
sb.append(line);
}

data = sb.toString();

br.close();

}catch(Exception e){
Log.d("Exception while downloading url", e.toString());
}finally{
iStream.close();
urlConnection.disconnect();
}
return data;
}


// Fetches data from url passed
private class DownloadTask extends AsyncTask<String, Void, String>{

private int downloadType=0;

// Constructor
public DownloadTask(int type){
this.downloadType = type;

}

@Override
protected String doInBackground(String... url) {

// For storing data from web service
String data = "";

try{
// Fetching the data from web service
data = downloadUrl(url[0]);
}catch(Exception e){
Log.d("Background Task",e.toString());
}
return data;
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);

switch(downloadType){
case PLACES:
// Creating ParserTask for parsing Google Places
placesParserTask = new ParserTask(PLACES);

// Start parsing google places json data
// This causes to execute doInBackground() of ParserTask class
System.out.println(result);
placesParserTask.execute(result);

break;

case PLACES_DETAILS :
// Creating ParserTask for parsing Google Places
placeDetailsParserTask = new ParserTask(PLACES_DETAILS);

// Starting Parsing the JSON string
// This causes to execute doInBackground() of ParserTask class
placeDetailsParserTask.execute(result);
}
}
}


/** A class to parse the Google Places in JSON format */
private class ParserTask extends AsyncTask<String, Integer, List<HashMap<String,String>>>{

int parserType = 0;

public ParserTask(int type){
this.parserType = type;
}

@Override
protected List<HashMap<String, String>> doInBackground(String... jsonData) {

JSONObject jObject;
List<HashMap<String, String>> list = null;

try{
jObject = new JSONObject(jsonData[0]);

switch(parserType){
case PLACES :
PlaceJSONParser placeJsonParser = new PlaceJSONParser();
// Getting the parsed data as a List construct
list = placeJsonParser.parse(jObject);
break;
case PLACES_DETAILS :
PlaceDetailsJSONParser placeDetailsJsonParser = new PlaceDetailsJSONParser();
// Getting the parsed data as a List construct
list = placeDetailsJsonParser.parse(jObject);
}

}catch(Exception e){
Log.d("Exception",e.toString());
}
return list;
}

@Override
protected void onPostExecute(List<HashMap<String, String>> result) {

switch(parserType){
case PLACES :
String[] from = new String[] { "description"};
int[] to = new int[] { android.R.id.text1 };

// Creating a SimpleAdapter for the AutoCompleteTextView
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), result, android.R.layout.simple_list_item_1, from, to);

// Setting the adapter
edit_destination.setAdapter(adapter);
break;
case PLACES_DETAILS :
HashMap<String, String> hm = result.get(0);

// Getting latitude from the parsed data
latitude = Double.parseDouble(hm.get("lat"));

System.out.println(latitude);

// Getting longitude from the parsed data
longitude = Double.parseDouble(hm.get("lng"));
System.out.println(longitude);


Toast.makeText(invoice.this, latitude+","+longitude , Toast.LENGTH_LONG).show();



SharedPreferences pref=getSharedPreferences("LOC", 0);


String S_lat,S_long;
S_lat=pref.getString("LAT", "");
S_long= pref.getString("LONG","");

source_lat=Double.parseDouble(S_lat);
source_long=Double.parseDouble(S_long);








break;
}
}
}

最佳答案

用以下代码替换 autocompleteTextView 的“addTextChangedListener”方法...

edit_destination.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
return false;
}

});
edit_destination.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Creating a DownloadTask to download Google Places matching "s"

if(placesDownloadTask!=null)
{
Log.i("--placesDownloadTask--","progress_status : "+placesDownloadTask.getStatus());
placesDownloadTask.cancel(true);
}


}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
String chterm;
chterm=edit_destination.getText().toString();
Log.i("---final selected text---", ""+chterm);
placesDownloadTask = new DownloadTask(PLACES);

// Getting url to the Google Places Autocomplete api
String url = getAutoCompleteUrl(s.toString());

// Start downloading Google Places
// This causes to execute doInBackground() of DownloadTask class
placesDownloadTask.execute(url);
}
});

不是从 onTextChanged 发出调用,而是从 afterTextChanged 发出调用,它减少了每个字符后的调用次数,从而减少了延迟。

试试吧,它可能对你有很大帮助。

关于android - 如何提高谷歌地点自动完成建议的性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22060152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com