0

我试图将所有地址(字符串)放入一个列表中,然后一个一个地获取它们并使用标记填充地图,但我收到此错误,因为 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);
            }
4

2 回答 2

2

看定义,是ListofListAddress

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>();
    
于 2013-07-13T15:10:42.520 回答
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();
     }

 }
于 2014-10-07T10:25:04.390 回答