2013-07-23 58 views
1

我试图在Google地图上添加搜索栏。谷歌地图V2的搜索栏

代码和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

我可以知道你为什么没有得到任何东西,当你按下按钮。这是因为String location = etLocation.getText()。toString();返回一个空白字符串。你可以把日志条目或吐司放进去,看看是不是发生了什么? 我在我的胎面具有的EditText类似的问题和共同意图在这里 Trouble sending stuff from an EditText to another activity using intents

不幸的是,如果这是我不知道如何解决你的问题的情况下。