2013-07-13 63 views
0

我试着去把所有的地址(字符串)到一个列表,然后通过一个让他们一个和填充标记的地图,但我得到这个错误saing那java.util.arraylist不能转换为android.location.address。任何帮助?的java.util.ArrayList不能被转换到android.location.address

这是代码,在定义生成错误

  int i = 0; 
      List<List<Address>> addressList = new ArrayList<List<Address>>(); 
      //while (indirizzi != null) { 
      while (i <= 3) { 
       try { 

        addressList.add(geocoder.getFromLocationName(indirizzi.get(i), 1)); 
        Log.i("indirizzo i-esimo",indirizzi.get(i)); 
        i++; 
       } catch (IOException e) { 
        Log.i("geolocation","geolocation IOException"); 
        e.printStackTrace(); 
       } 
      } 

      for (int j = 0; j < addressList.size(); j++) { 

       Address address = (Address) addressList.get(j); 
       if(address.hasLatitude() && address.hasLongitude()){ 
        latLng = new LatLng(address.getLatitude(), address.getLongitude()); 
       } 

       markerOptions = new MarkerOptions(); 
       markerOptions.position(latLng); 
       markerOptions.title(indirizzi.get(i)); 

       markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.icn_albero)); 

       Log.i("for", Integer.toString(i)); 
       j++; 
       googleMap.addMarker(markerOptions); 
      } 

回答

2

看起来snipet,它是的AddressListList一个。

List<List<Address>> addressList = new ArrayList<List<Address>>(); 

你不能做到这一点:

Address address = (Address) addressList.get(j); 

由于这会给你一个List<Address>这不是一个Address对象。

  1. 你能做:

    Address address = (Address) addressList.get(j).get(someOtherIndex); 
    
  2. 定义List为:

    List<Address> addressList = new ArrayList<Address>(); 
    
+0

我已经triend 2方法,你已经证明了我,但后来我不得不施放此addressList.add((地址)geocoder.getFromLocationName(indirizzi.get(I) ,1));它仍然不起作用!并用第一种方法我得到一个索引超出界限的错误! – Pheonix7

0

我用下面的代码。

我呼吁从OnCreate中的GeocoderTask类。

ed.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, final View view, 
       int position, long id) { 
      addresstext = (String) parent.getItemAtPosition(position); 

      Toast.makeText(getBaseContext(),""+addresstext+ "", Toast.LENGTH_SHORT).show(); 

      if(addresstext!=null && !addresstext.equals("")){ 
       new GeocoderTask().execute(addresstext); 
      } 
      } 

     }); 

-

private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{ 

@Override 
protected List<Address> doInBackground(String... locationName) { 
    // Creating an instance of Geocoder class 
    Geocoder geocoder = new Geocoder(getBaseContext()); 
    List<Address> addresses = null; 

    try { 
     // Getting a maximum of 10 Address that matches the input text 
     addresses = geocoder.getFromLocationName(locationName[0], 10); 
     System.out.println(" Inside Background Process"); 
    } 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 
    if(marker!=null){ 
    marker.remove(); 
    } 

    // 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 latLng = new LatLng(address.getLatitude(), address.getLongitude()); 

     addressText = String.format("%s, %s", 
     address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "", 
     address.getCountryName()); 

     System.out.println(" Inside OnPostExecutemeathod Process"); 

     Toast.makeText(getBaseContext(), ""+address.getLatitude()+" - "+address.getLongitude()+"", Toast.LENGTH_SHORT).show(); 

     CameraPosition cameraPosition = new CameraPosition.Builder() 
     .target(latLng)  // Sets the center of the map to Mountain View 
     .zoom(17)     // Sets the zoom 
     .bearing(90)    // Sets the orientation of the camera to east 
     .tilt(30)     // Sets the tilt of the camera to 30 degrees 
     .build();     // Creates a CameraPosition from the builder 
    map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 
     map.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18)); 
     marker = map.addMarker(new MarkerOptions() 
     .position(latLng) 
     .title(""+addresstext+"")); 

     marker.showInfoWindow(); 
    } 

} 
相关问题