0

为什么当我进行触摸精确定位时,它只需要数组列表中的最后一个精确定位。当我测量 arraylist 大小时,它说大小为 1,即使我保存了 2 个或更多精确点。

谷歌地图类。

public class GoogleMaps extends MapActivity implements LocationListener {



public void addLocation() {
    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    final EditText input = new EditText(this);
    alert.setTitle("What do you want to call the location?");
    alert.setView(input);
    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

            value = input.getText().toString().trim();
            checklocationTitle();
        }
    });

    alert.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.cancel();
                }
            });
    alert.show();
}


public void checklocationTitle() {
    if (value.length() > 3) {
        Toast.makeText(this, "Name of the locations is know " + value,
                Toast.LENGTH_LONG).show();
        try {
            markedpinpoint = true;
            midllat = touchedPoint.getLatitudeE6() / 1E6;
            midlongi = touchedPoint.getLongitudeE6() / 1E6;
            Geocoder geocoder = new Geocoder(getBaseContext(),
                    Locale.getDefault());
            List<Address> adress = geocoder.getFromLocation(
                    touchedPoint.getLatitudeE6() / 1E6,
                    touchedPoint.getLongitudeE6() / 1E6, 1);
            if (adress.size() > 0) {
                String display = "";
                for (int i = 0; i < adress.get(0).getMaxAddressLineIndex(); i++) {
                    display += adress.get(0).getAddressLine(i) + "\n";
                    OverlayItem overlayitem = new OverlayItem(touchedPoint,
                            value, display);
                    custom = new Location_Service(d, GoogleMaps.this);
                    custom.insertLocation(overlayitem);
                    overlayList.add(custom);
                }
            } else {
                Toast.makeText(
                        this,
                        "There where a problem to locate the selected adresse",
                        Toast.LENGTH_LONG).show();
            }
        } catch (IOException e) {
        }
    } else {
        Toast.makeText(this,
                "Please provide a least 3 cifre Title for your location.",
                Toast.LENGTH_LONG).show();
        addLocation();
    }
}


public void buttonLocations(View view) {
    // stopLocationListner();
    // stopBackgroundService();
    Intent intent = new Intent(this, PinPoints.class);
    startActivity(intent);
    // Toast.makeText(this, "Gemte steder: " + custom.size(),
    // Toast.LENGTH_LONG).show();

}

}

位置_服务类

public class Location_Service extends ItemizedOverlay<OverlayItem> {

    public ArrayList<OverlayItem> pinpoints = new ArrayList<OverlayItem>();

    public Location_Service(Drawable defaultMarker) {
        super(boundCenter(defaultMarker));
        // TODO Auto-generated constructor stub
    }

    public ArrayList<Locations> getData() {
        Locations hej = new Locations();
        ArrayList<Locations> tt = new ArrayList<Locations>();
        for (OverlayItem test : pinpoints) {
            hej.setAdress(test.getSnippet());
            hej.setMidlat(test.getPoint().getLatitudeE6());
            hej.setMidlong(test.getPoint().getLongitudeE6());
            hej.setTitle(test.getTitle());
            tt.add(hej);
        }
        return tt;
    }

    public Location_Service(Drawable m, Context context) {
        this(m);
    }

    @Override
    protected OverlayItem createItem(int i) {
        return pinpoints.get(i);
    }

    @Override
    public int size() {
        return pinpoints.size();
    }

    public void insertLocation(OverlayItem item) {
        pinpoints.add(item);
        this.populate();
    }

}
4

1 回答 1

0

您正在将相同的 Locations 对象重复添加到列表中getData()......您没有实例化一个新对象。

你可能真的是说

ArrayList<Locations> tt = new ArrayList<Locations>();
for (OverlayItem test : pinpoints) {
    Locations hej = new Locations(); //instantiate each time!
    hej.setAdress(test.getSnippet());
    hej.setMidlat(test.getPoint().getLatitudeE6());
    hej.setMidlong(test.getPoint().getLongitudeE6());
    hej.setTitle(test.getTitle());
    tt.add(hej);
}
return tt;
于 2013-01-26T00:59:55.963 回答