我在谷歌地图上尝试添加搜索栏。
代码和logcat都没有显示任何错误。 但是,该程序不起作用。 它显示了页面,但是当我点击“查找”时,它没有向我显示任何内容。
logcat的
07-23 11:15:25.330: W/KeyCharacterMap(30119): No keyboard for id 0
07-23 11:15:25.330: W/KeyCharacterMap(30119): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
07-23 11:15:26.180: I/APACHE HTTP (thCr=20) - NafHttpAuthStrategyDefault(30119): (thUse=28) cached value : gbaSupportIsPossible=false
07-23 11:15:26.180: I/APACHE HTTP (thCr=20) - NafHttpAuthStrategyDefault(30119): (thUse=28) It is impossible to support GBA now (many possible reasons: no Android Context, current client is GBA service, etc.), then it will be just usual HTTP.
07-23 11:15:26.180: I/APACHE HTTP (thCr=28) - NafRequestExecutorWrapperRedirectionHandler(30119): (thUse=28) It isn't GBA flow, redirection responses are not handled.
07-23 11:15:26.190: I/AndroidHttpClient$2(30119): executeRequestSending() director.getClass()=class org.apache.http.impl.client.DefaultRequestDirector
07-23 11:15:26.480: I/AndroidHttpClient$2(30119): execute() finalHttpResponse.getStatusLine()=HTTP/1.1 400 Bad Request
07-23 11:15:26.490: I/AndroidHttpClient$2(30119): execute()#finished
07-23 11:15:31.400: I/APACHE HTTP (thCr=20) - NafHttpAuthStrategyDefault(30119): (thUse=28) cached value : gbaSupportIsPossible=false
07-23 11:15:31.400: I/APACHE HTTP (thCr=20) - NafHttpAuthStrategyDefault(30119): (thUse=28) It is impossible to support GBA now (many possible reasons: no Android Context, current client is GBA service, etc.), then it will be just usual HTTP.
07-23 11:15:31.400: I/APACHE HTTP (thCr=28) - NafRequestExecutorWrapperRedirectionHandler(30119): (thUse=28) It isn't GBA flow, redirection responses are not handled.
07-23 11:15:31.410: I/AndroidHttpClient$2(30119): executeRequestSending() director.getClass()=class org.apache.http.impl.client.DefaultRequestDirector
07-23 11:15:31.830: I/AndroidHttpClient$2(30119): execute() finalHttpResponse.getStatusLine()=HTTP/1.1 400 Bad Request
07-23 11:15:31.850: I/AndroidHttpClient$2(30119): execute()#finished
这是我的编码
MainActivity.java
public class MainActivity extends FragmentActivity {
GoogleMap googleMap;
MarkerOptions markerOptions;
LatLng latLng;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
//SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
//Getting a referenece to the map
googleMap = supportMapFragment.getMap();
//Getting reference to btn_find of the layout activity
Button btn_find = (Button) findViewById(R.id.btn_find);
//Defining button click event listener for the find button
OnClickListener findClickListener = new OnClickListener(){
@Override
public void onClick(View v) {
// Getting reference to EditText to get the user input location
EditText etLocation = (EditText) findViewById(R.id.et_location);
//Getting user input location
String location = etLocation.getText().toString();
if(location != null && !location.equals("")) {
new GeocoderTask().execute(location);
}
}
};
//Setting button click event listener for the find button
btn_find.setOnClickListener(findClickListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//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 Geocode class
Geocoder geocoder = new Geocoder(getBaseContext());
List<Address> addresses = null;
try {
//Getting a max of 3 address that matches the input text
addresses = geocoder.getFromLocationName(locationName[0],3);
}
catch (IOException e) {
e.printStackTrace();
}
return addresses;
}
@Override
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
googleMap.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);
googleMap.addMarker(markerOptions);
//Locate the first location
if(i==0)
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
}
}
答案 0 :(得分:0)
我可能知道你按下按钮时没有得到任何东西的原因。这是因为String location = etLocation.getText()。toString();返回一个空字符串。你可以放入日志条目或祝酒词,看看这是不是发生了什么? 我在这里遇到类似EditText和共享意图的问题 Trouble sending stuff from an EditText to another activity using intents
不幸的是,如果是这种情况,我不知道如何解决您的问题。