3

在我的应用程序中,我使用的是谷歌地图,首先我通过标记显示用户当前位置并通过透明圆圈显示其准确性,现在当我想将另一个标记添加到代表我以前位置的同一点时,相同的代码不起作用对我来说,请有人帮我找出代码有什么问题,我只想在单个地图视图中添加多个标记

这是我显示两个标记的代码:-

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 显示第二个标记时,它不起作用....

任何帮助将不胜感激在此先感谢......

4

1 回答 1

2

切换到谷歌地图的新 API。使用起来好多了。如果您必须使用旧 API,请使用 MyLocationOverlay 作为您当前的位置。另外,如果我没记错的话,有一对 set/getMarker 方法可以省去你自己在画布上绘制标记的麻烦。

于 2013-01-09T13:47:44.367 回答