无法在谷歌地图上添加多个标记

3
在我的应用程序中,我正在使用Google Maps。首先,我通过标记显示用户当前位置,并通过透明圆圈显示其准确性。现在,当我想要向表示我的前一个位置的同一点添加另一个标记时,相同的代码对我无效。请有人帮我找出代码有什么问题,我只想在单个地图视图中添加多个标记。
以下是显示两个标记的代码:
LocationResult locationResult = new LocationResult(){
        @Override
        public void gotLocation(Location location){
            //Got the location!
            if (location != null) {
                GeoPoint point = new GeoPoint(
                        (int) (location.getLatitude() * 1E6), 
                        (int) (location.getLongitude() * 1E6));

                accuracy = location.getAccuracy();
                cur_lati = location.getLatitude();
                cur_longi = location.getLongitude();

                String address = "";
                Geocoder geoCoder = new Geocoder(
                        getBaseContext(), Locale.getDefault());
                try {
                    List<Address> addresses = geoCoder.getFromLocation(
                            point.getLatitudeE6()  / 1E6, 
                            point.getLongitudeE6() / 1E6, 1);

                    if (addresses.size() > 0) {
                        for (int index = 0; 
                                index < addresses.get(0).getMaxAddressLineIndex(); index++)
                            address += addresses.get(0).getAddressLine(index) + " ";
                    }
                }catch (IOException e) {        
                    e.printStackTrace();
                }   


                Input.setPinPoints(cur_lati,cur_longi,address);                 // sending lat,lon to input class

                Toast.makeText(getBaseContext(),"Latitude: " + location.getLatitude() + " Longitude: " + location.getLongitude() + "accuracy" + location.getAccuracy(),Toast.LENGTH_SHORT).show();
                mc = mapView.getController();
                mc.animateTo(point);
                mc.setZoom(19);


                MapOverlay mapOverlay = new MapOverlay();
                mapOverlay.setPointToDraw(point);
                List<Overlay> listOfOverlays = mapView.getOverlays();
                listOfOverlays.clear();
                listOfOverlays.add(mapOverlay);

                Bundle bundle=getIntent().getExtras();

                if(bundle!=null){

                    int value = bundle.getInt("tab_value");
                    lati = bundle.getDouble("lati");
                    longi = bundle.getDouble("longi");



                    //GeoPoint point1 = new GeoPoint((int) lati, (int) longi);

                    if(value == 1){

                        MapOverlay1 mapOverlay1 = new MapOverlay1();
                        //      mapOverlay1.setPointToDraw(point1);
                        List<Overlay> listOfOverlays1 = mapView.getOverlays();
                        listOfOverlays1.add(mapOverlay1);


                    }

                }

            }
        }
    };
    MyLocation myLocation = new MyLocation();
    myLocation.getLocation(this, locationResult);


    btnimg.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent=new Intent(getApplicationContext(),Input.class);
            startActivity(intent);

        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_safe_city, menu);
    return true;
}

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

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();

    finish();
}

@Override
public void finish() {
    // TODO Auto-generated method stub
    super.finish();

}


class MapOverlay extends Overlay
{
    private GeoPoint pointToDraw;

    public void setPointToDraw(GeoPoint point) {
        pointToDraw = point;
    }

    public GeoPoint getPointToDraw() {
        return pointToDraw;
    }

    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
        super.draw(canvas, mapView, shadow);           

        // convert point to pixels
        Point screenPts = new Point();
        Paint mPaintBorder,mPaintFill ;
        mapView.getProjection().toPixels(pointToDraw, screenPts);

        // add marker
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pinimage);
        canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null);   

        mPaintBorder = new Paint();
        mPaintBorder.setStyle(Paint.Style.STROKE);
        mPaintBorder.setAntiAlias(true);
        mPaintBorder.setColor(0xee4D2EFF);
        mPaintFill = new Paint();
        mPaintFill.setStyle(Paint.Style.FILL);
        mPaintFill.setColor(0x154D2EFF);

        int radius = (int) mapView.getProjection().metersToEquatorPixels(accuracy);

        canvas.drawCircle(screenPts.x, screenPts.y, radius, mPaintBorder);

        canvas.drawCircle(screenPts.x, screenPts.y, radius, mPaintFill);

        return true;
    }
} 

class MapOverlay1 extends com.google.android.maps.Overlay
{

    private GeoPoint p1 = new GeoPoint((int)(lati),(int)(longi));

    Projection projection;



    @Override
    public boolean draw(Canvas canvas, MapView mapView,
            boolean shadow, long when)
    {

        super.draw(canvas, mapView, shadow);


        Point screenPts1 = new Point();
        mapView.getProjection().toPixels(p1, screenPts1);
        //---add the marker---
        Bitmap bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.car);
        canvas.drawBitmap(bmp1, screenPts1.x, screenPts1.y-50, null);

        return true;

    }


}

通过使用MapOverlay类,我能够在地图上放置一个标记,显示用户当前的位置,但是当我使用MapOverlay1来显示第二个标记时,它没有起作用...任何帮助将不胜感激。提前致谢...
1个回答

2

转换到新的Google地图API,它更好用。 如果必须使用旧的API,请使用MyLocationOverlay来获取您当前的位置。 此外,如果我没记错的话,有一个set/getMarker方法对可以帮助您省去自己在画布上绘制标记的麻烦。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接