0

这是我编写的代码,用于获取手机的 gps 坐标并指向您所在的位置。但在改进中,我需要得到 1 公里半径的圆。我怎么才能得到它?

package m.a.p;
public class MappingActivity extends MapActivity {    
    MapController mControl;
    GeoPoint GeoP;
    MapView mapV;
    Drawable d;
    List<Overlay> overlaylist;

    public double lat;
    public double lng;
    Button checkin, addplace;

    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in
                                                                        // Meters
    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 10000; // in
                                                                    // Milliseconds

    protected LocationManager locationManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mapV = (MapView) findViewById(R.id.mapview);
        checkin = (Button) findViewById(R.id.check);
        addplace = (Button) findViewById(R.id.addp);

        overlaylist = mapV.getOverlays();

        d = getResources().getDrawable(R.drawable.point);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                MINIMUM_TIME_BETWEEN_UPDATES,
                MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, new MyLocationListener());

        Location location = locationManager
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location != null) {

            lat = location.getLatitude();
            lng = location.getLongitude();

        }

        Button check = (Button) findViewById(R.id.check);
        Button addplace = (Button) findViewById(R.id.addp);
        Button nearby = (Button) findViewById(R.id.point);

        check.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                TextView result = (TextView) findViewById(R.id.result);
                result.setText("Checked the Plce");
            }
        });
        addplace.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                TextView result = (TextView) findViewById(R.id.result);
                result.setText("Added the Plce");
            }
        });
        nearby.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                TextView result = (TextView) findViewById(R.id.result);
                result.setText("Nearby the Plce");
            }
        });

    }

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

    public class MyLocationListener implements LocationListener {

        public void onLocationChanged(Location location) {
            String message = String.format("You are Here");

            Toast.makeText(MappingActivity.this, message, Toast.LENGTH_LONG)
                    .show();

            GeoP = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));

            mControl = mapV.getController();
            mControl.animateTo(GeoP);
            mControl.setZoom(19);

            OverlayItem overlayItem = new OverlayItem(GeoP, "You are Here",
                    "Point");
            CustomPinpoint custom = new CustomPinpoint(d, MappingActivity.this);
            custom.insertPinpoint(overlayItem);
            overlaylist.add(custom);
        }

        public void onProviderDisabled(String provider) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    }
}

custompinpoint 公共类 CustomPinpoint 扩展 ItemizedOverlay{

    private ArrayList<OverlayItem> pinpoints = new ArrayList <OverlayItem>();
    private Context c;


    public CustomPinpoint(Drawable defaultMarker) {
        super(boundCenter(defaultMarker));
    }

    public CustomPinpoint(Drawable m, Context context){
        this(m);
        c = context;
    }

    @Override
    protected OverlayItem createItem(int i) {
        // TODO Auto-generated method stub
        return pinpoints.get(i);
    }

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

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

}
4

1 回答 1

4

尝试使用自定义 MapOverlay 绘制您的位置并在其周围画一个圆圈,并覆盖 onDraw() 方法:

public class MyOwnLocationOverlay extends MyLocationOverlay{

    private MapView mapView;
    private Paint circlePainter;
    private Point screenCurrentPoint;
    private GeoPoint geoCurrentPoint;
    private int meters;

    public MyOwnLocationOverlay(Context context, MapView mapView) {
        super(context, mapView);
        this.mapView = mapView;
    }

    // This method is used to get user submitted radius from our application
    public void setMeters(int meters) {
        this.meters = meters;
    }

    @Override
    public synchronized boolean draw(Canvas canvas, MapView mapView,
            boolean shadow, long when) {
        // Set the painter to paint our circle. setColor = blue, setAlpha = 70 so the background
        // can still be seen. Feel free to change these settings
        circlePainter = new Paint();
        circlePainter.setAntiAlias(true);
        circlePainter.setStrokeWidth(2.0f);
        circlePainter.setColor(0xff6666ff);
        circlePainter.setStyle(Style.FILL_AND_STROKE);
        circlePainter.setAlpha(70);

        // Get projection from the mapView. 
        Projection projection = mapView.getProjection();
        // Get current location
        geoCurrentPoint = getMyLocation();
        screenCurrentPoint = new Point();
        // Project the gps coordinate to screen coordinate
        projection.toPixels(geoCurrentPoint, screenCurrentPoint);

        int radius = metersToRadius(geoCurrentPoint.getLatitudeE6() /1000000);
        // draw the blue circle
        canvas.drawCircle(screenCurrentPoint.x, screenCurrentPoint.y, radius, circlePainter);
        return super.draw(canvas, mapView, shadow, when);
    }

    // hack to get more accurate radius, because the accuracy is changing as the location
    // getting further away from the equator
    public int metersToRadius(double latitude) {
        return (int) (mapView.getProjection().metersToEquatorPixels(meters) * (1/ Math.cos(Math.toRadians(latitude))));         
    }
}

有关更多详细信息,请参阅此链接

于 2012-06-21T08:24:39.470 回答