我正在使用Maps API返回地址。这个工作正常,然后在上周某个时候,没有改变任何东西,它开始用26分钟返回一个地址。我追踪它,发现在我点击搜索按钮后,花了26分钟才能调用地理编码器,而不是花了26分钟来检索地址。我不认为这是一个编码问题,但也许是其他人经历过的其他问题。我已经包含了下面的代码。我正在测试三星GalaxyTab 8(SM-T550)Android:6.0.1
片段中的按钮设置:
searchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String address = addressEdit.getText().toString();
//remove text from address
addressEdit.setText("");
//hide keyboard
final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
//submit address if one has been typed in
if(address != null && !address.equals("")){
//submit address to be found
new GeocoderTask().execute(address);
//submit the latlng of the marker on the map if not null
} else if (latLng != null){
address = getCompleteAddressString(latLng.latitude, latLng.longitude);
new GeocoderTask().execute(address);
//no address was entered
} else {
Toast.makeText(getContext(), R.string.no_address_entered, Toast.LENGTH_LONG).show();
}
}
});
Geocoder课程:
public class GeocoderTask extends AsyncTask<String, Void, List<Address>> {
private static final String TAG = "GeocoderTask";
@Override
protected List<Address> doInBackground(String... locationName){
Geocoder geocoder = new Geocoder(getContext());
List<Address> addresses = null;
try{
//return only 1 address
addresses = geocoder.getFromLocationName(locationName[0], 1);
}catch (IOException e){
e.printStackTrace();
}
return addresses;
}
@Override
protected void onPostExecute(List<Address> addresses) {
if(addresses==null || addresses.size()==0){
Toast.makeText(getContext(), R.string.no_location, 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());
// get the string address based on the retrieved latlng
String addressText = getCompleteAddressString(latLng.latitude, latLng.longitude);
/* convert address from list into string
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.newLatLng(latLng));
map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));
//call dialog fragment to confirm patient address
FragmentManager fragmentManager = getFragmentManager();
//load latlng and string address google maps returns
ConfirmAddressDialogFragment dialogFragment = ConfirmAddressDialogFragment.newInstance(addressText, latLng, patientName);
//set target fragments and show dialog
dialogFragment.setTargetFragment(MapFragment.this, REQUEST_ADDRESS);
dialogFragment.show(fragmentManager, DIALOG_ADDRESS);
}
}
}
}