java.util.arraylist不能强制转换为android.location.address

时间:2013-07-13 15:06:23

标签: android geolocation google-maps-markers

我试图将所有地址(字符串)放入列表然后逐个获取它们并使用标记填充地图,但是我得到这个错误,因为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 个答案:

答案 0 :(得分:2)

查看定义,它是List List的{​​{1}}。

Address

你不能这样做:

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

因为这会为您提供Address address = (Address) addressList.get(j); ,而不是List<Address>对象。

  1. 您可以这样做:

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

    List

答案 1 :(得分: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();
     }

 }