java - 以当前位置为圆心在google map 上画四个圆圈

标签 java android google-maps android-mapview android-canvas

enter image description here

在我的代码中,我用图像在 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;
    }

下面的类是上面类的延续。在下面的类(class)中,我将在谷歌地图上显示我当前的位置。所以我想我需要在这个类中进行更改,以当前位置为圆心绘制四个圆。

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

}

最佳答案

在绘制时,您可以简单地使用 Canvas 来绘制圆,使用 canvas.drawCircle(screenPts.x,screenPts.y, radius, yourPaint);获得 10-20-30-40m 的圆圈,您可能需要使用距离公式来查找有多少像素等于 10、20m 等。Location 提供了一种使用 distanceBetween() 方法。请参阅here有关于此的帖子,以及 here获取官方 Android 文档。一旦您拥有距起始 GeoPoint 10m 的 GeoPoint,您就可以获得该 GeoPoint 的屏幕坐标,并将其用作圆的半径。祝你好运,希望这有帮助!

关于java - 以当前位置为圆心在google map 上画四个圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11878734/

相关文章:

java - Elasticsearch 5.1 批量操作

java - JscrollPane 在将其添加到另一个扩展到 JPanel 的类后消失

java - 嵌入式tomcat与spring集成

php - 使用 Java 和 PHP 的 HTTP 发布消息

javascript - 我想使用数据库中的 map 标记

java - 如何通过 Acegi 的 beans 手动查询 LDAP?

java - 异步任务 : what am I misunderstanding?

android - Android 应用程序能否知道另一个 Android 应用程序何时运行?

php - 使用 Laravel 计算我当前点与 MySQL 中其他点之间的距离

css - 在不拉伸(stretch)的情况下将静态 Google Map API 调整为自定义大小