0

私のアプリケーションでは、Web サービスから複数の緯度と経度を取得しています。現時点では、以下のコードを使用してグール マップに 1 つの座標のみを表示できます。

public class CustomMap extends MapActivity {

    MapView mapView;
    List<Overlay> mapOverlays;
    Drawable drawable;
    Drawable drawable2;
    CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay2;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gmap);
        //setContentView(LayoutInflater.from(getParent()).inflate(R.layout.main_ou, null));

        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapOverlays = mapView.getOverlays();

        // second overlay
        drawable2 = getResources().getDrawable(R.drawable.red);
        itemizedOverlay2 = new CustomItemizedOverlay<CustomOverlayItem>(drawable2, mapView);
        GeoPoint point3 =  new GeoPoint((int)(-10.224609*1E6),(int)(17.506044*1E6));
        CustomOverlayItem overlayItem3 = new CustomOverlayItem(point3, "Title", 
        "Cliquez here", null);

        itemizedOverlay2.addOverlay(overlayItem3);
        //itemizedOverlay2.onTap(0);

        mapOverlays.add(itemizedOverlay2);

        final MapController mc = mapView.getController();
        mc.animateTo(point3);
        mc.setZoom(16);

    }


    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }




}

今、私は配列を使用して動的に地図に緯度と経度を設定したいと考えています。ArrayList val; を使用しています。データを取得および設定します。クラスを動的にする方法について誰かがヒントをくれますか。

新規更新 以下のコードは 1 つのマーカーしか表示しないが、すべてのマーカーを表示したい

public class Map extends MapActivity {

     private MapController mapControll;
     private GeoPoint geoPoint=null;
     private MapView mapview;
     private MyItemizedOverlay userPicOverlay;
     private MyItemizedOverlay nearPicOverlay;
     private Drawable userPic,atmPic;
     private CustomOverlayItem nearatms[] = new CustomOverlayItem[50];
     public static Context context;

     double latitude,longitude;



    ArrayList<Article> mArticles;
    CustomOverlayItem overlayItem3 ;
    public ArrayList<Article> articles;
    public static ArrayList<Article> article;

    EditText search;
    DBHelper helper;
    GeoPoint point;

    MapView mapView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aproximite);
        context = getApplicationContext();

        mapView = (MapView) findViewById(R.id.mapview);
        mapView = (MapView) findViewById(R.id.mapview);
        search = (EditText)findViewById(R.id.search_box);

        helper = DBHelper.getInstance(this);

        geoPoint = new GeoPoint((int)(latitude * 1E6),(int)(longitude * 1E6));          
        mapview = (MapView)findViewById(R.id.mapview);
        mapControll= mapview.getController();
        mapview.setBuiltInZoomControls(true);
        mapview.setStreetView(true);
        mapControll.setZoom(16);
        mapControll.animateTo(geoPoint);

        userPic = this.getResources().getDrawable(R.drawable.boutton_log);
        userPicOverlay = new MyItemizedOverlay(userPic);
        OverlayItem overlayItem = new OverlayItem(geoPoint, "I'm Here!!!", null);
        userPicOverlay.addOverlay(overlayItem);
        mapview.getOverlays().add(userPicOverlay);


        atmPic = this.getResources().getDrawable(R.drawable.boutton_log);
        nearPicOverlay = new MyItemizedOverlay(atmPic);


        new GetLocationTask().execute();

    }




private class GetLocationTask extends AsyncTask<Void, Void, Void> {

        @Override
        public Void doInBackground(Void... params) {
            mArticles = helper.getArticlesList();

            return null;
        }

        @Override
        public void onPreExecute() {}

        @Override
        public void onPostExecute(Void result) {            

          try {

                for (int i = 0; i < mArticles.size(); i++) {


                    point =  new GeoPoint((int)(Double.parseDouble(mArticles.get(i).getLatitude())*1E6),(int)(Double.parseDouble(mArticles.get(i).getLongitude())*1E6));
                    nearatms[i] = new CustomOverlayItem(point ,"title", 
                    "click here", null);

                    nearPicOverlay.addOverlay(nearatms[i]);
                }
                mapview.getOverlays().add(nearPicOverlay);
                //Added symbols will be displayed when map is redrawn so force redraw now
                mapview.postInvalidate();
            } catch (Exception e) {
                e.printStackTrace();
            }


        }
     }





@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

}

MyItemizedOverlay.class

public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {

    private ArrayList<OverlayItem> myOverlays ;

    public MyItemizedOverlay(Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));
        myOverlays = new ArrayList<OverlayItem>();
        populate();
    }

    public void addOverlay(OverlayItem overlay){
        myOverlays.add(overlay);
        populate();
    }

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

    // Removes overlay item i
    public void removeItem(int i){
        myOverlays.remove(i);
        populate();
    }

    // Returns present number of items in list
    @Override
    public int size() {
        return myOverlays.size();
    }


    public void addOverlayItem(OverlayItem overlayItem) {
        myOverlays.add(overlayItem);
        populate();
    }


    public void addOverlayItem(int lat, int lon, String title) {
        try {
            GeoPoint point = new GeoPoint(lat, lon);
            OverlayItem overlayItem = new OverlayItem(point, title, null);
            addOverlayItem(overlayItem);    
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

    @Override
    protected boolean onTap(int index) {
        // TODO Auto-generated method stub
        String title = myOverlays.get(index).getTitle();
        System.out.println(index);
        return super.onTap(index);
    }
}
4

1 に答える 1