Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 以当前位置为圆心,在谷歌地图上画四个圆_Java_Android_Google Maps_Android Mapview_Android Canvas - Fatal编程技术网

Java 以当前位置为圆心,在谷歌地图上画四个圆

Java 以当前位置为圆心,在谷歌地图上画四个圆,java,android,google-maps,android-mapview,android-canvas,Java,Android,Google Maps,Android Mapview,Android Canvas,在我的代码中,我在Android屏幕的上半部分用图像显示我的当前位置。所以我需要画一个圆圈,就像上面的图片一样,围绕着我的当前位置,以我的当前位置为圆心。我需要画四个半径为10m,20m,30m,40m的圆。下面是我在谷歌地图上显示当前位置的代码 @Override protected void onCreate(Bundle icicle) { try { super.onCreate(icicle);

在我的代码中,我在Android屏幕的上半部分用图像显示我的当前位置。所以我需要画一个圆圈,就像上面的图片一样,围绕着我的当前位置,以我的当前位置为圆心。我需要画四个半径为10m,20m,30m,40m的圆。下面是我在谷歌地图上显示当前位置的代码

    @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;
    }

在绘图时,您可以简单地使用画布绘制圆,使用Canvas.drawCirclescreenPts.x、screenPts.y、radius、yourPaint;至于获取10-20-30-40m的圆,您可能需要使用距离公式来确定多少像素等于10、20m等。位置提供了一种使用distanceBetween方法进行此操作的好方法。有关这方面的帖子和官方Android文档,请参见。一旦你有一个距起始点10米的地质点,你就可以得到该地质点的屏幕坐标,并将其用于圆的半径。祝你好运,希望这有帮助

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

}