1

ここに画像の説明を入力

私のコードでは、Android 画面の上半分に現在の場所を画像で表示しています。したがって、現在の場所を円の中心として、現在の場所の周りに上の画像のように円を描く必要があります。そして、半径10m、20m、30m、40mの4つの円を描く必要があります。以下は、Googleマップに現在の場所を表示する私のコードです。

    @Override
    protected void onCreate(Bundle icicle) {
        try {       
            super.onCreate(icicle);
            setContentView(R.layout.activity_main);
            initComponents();
            mapView.setBuiltInZoomControls(true);
            mapView.setSatellite(false);
            mapController = mapView.getController();
            mapController.setZoom(16);
            locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
            if (locationManager == null) {
                Toast.makeText(getApplicationContext(),
                        "Location Manager Not Available", Toast.LENGTH_SHORT)
                        .show();
                return;
            }
            location = locationManager
            .getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (location == null)
                location = locationManager
                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            if (location != null) {
                double lat = location.getLatitude();
                double lng = location.getLongitude();
                Toast.makeText(getApplicationContext(),
                        "Location Are" + lat + ":" + lng, Toast.LENGTH_SHORT)
                        .show();
                GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
                mapController.animateTo(point, new Message());
                mapOverlay.setPointToDraw(point);
                List<Overlay> listOfOverlays = mapView.getOverlays();
                listOfOverlays.clear();
                listOfOverlays.add(mapOverlay);
            }
            locationListener = new LocationListener() {
                @Override
                public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
                }

                @Override
                public void onProviderEnabled(String arg0) {
                }

                @Override
                public void onProviderDisabled(String arg0) {
                }

                @Override
                public void onLocationChanged(Location l) {
                    location = l;
                    locationManager.removeUpdates(this);
                    if (l.getLatitude() == 0 || l.getLongitude() == 0) {
                    } else {
                        double lat = l.getLatitude();
                        double lng = l.getLongitude();
                        Toast.makeText(getApplicationContext(),
                                "Location Are" + lat + ":" + lng,
                                Toast.LENGTH_SHORT).show();
                    }
                }
            };
            if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
                locationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER, 1000, 10f, locationListener);
            if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
                locationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, 1000, 10f, locationListener);
            }
            locationtimer = new CountDownTimer(30000, 5000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    if (location != null)
                        locationtimer.cancel();
                }

                @Override
                public void onFinish() {
                    if (location == null) {
                    }
                }
            };
            locationtimer.start();
        }
        catch(Exception e) {
            System.out.println(e);
        }
    }

    public MapView getMapView() {
        return this.mapView;
    }

    private void initComponents() {
        mapView = (MapView) findViewById(R.id.mapView);
    }

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

以下のクラスは、上記の続きです。以下のクラスでは、現在地を Google マップに表示しています。したがって、現在の位置を円の中心として 4 つの円を描画するためだけに、このクラスを変更する必要があると思います。

    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);

            Point screenPts = new Point();
            mapView.getProjection().toPixels(pointToDraw, screenPts);

            Bitmap bmp = BitmapFactory.decodeResource(getResources(),
                    R.drawable.current_user);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null);
            return true;
        }
    }

}
4

1 に答える 1

0

10-20-30-40mの円を取得するには、距離の式を使用して、 Canvas10、20mcanvas.drawCircle(screenPts.x,screenPts.y, radius, yourPaint); などに等しいピクセル数を見つける必要があります Location。メソッドを使用してこれを行うdistanceBetween()方法。それに関する投稿についてはこちらを、公式の Android ドキュメントについてはこちらを参照してください。開始点から 10m 離れた場所に GeoPoint があると、その画面座標を取得し、それを円の半径に使用できます。頑張ってください、これが役に立てば幸いです!GeoPointGeoPoint

于 2012-09-13T22:40:06.640 に答える