我的地理位置发生变化时如何制作任何东西?

时间:2012-09-25 13:06:14

标签: android google-maps android-location

我想做一些动作,即每当我的地理定位发生变化时,都会显示带有'im here'文本的对话框。

我正在使用MyLocationOverlay在Google地图的mapview上的位置上绘制一个点。

我怎样才能实现这一目标?

2 个答案:

答案 0 :(得分:1)

以下是位置监听器类 -

private MyOverLay draw = new MyOverLay();

 class MyLocationListener_gps implements LocationListener {
                public void onLocationChanged(Location location) {

                    clat = location.getLatitude();
                    clon = location.getLongitude();
                                GeoPoint geoPoint = new GeoPoint((int) (clat * 1E6),
                                        (int) (clon * 1E6));
                                    mapView.getController().animateTo(geoPoint);

                                    draw = new MyOverLay(geoPoint);
                                    mapView.getOverlays().add(draw);
        }
                }

这是代码覆盖类 -

class MyOverLay extends Overlay {
        private GeoPoint gp1;
        private int mRadius = 3;

        public MyOverLay() {

        }

        public MyOverLay(GeoPoint gp1) {
            this.gp1 = gp1;
        }

        @Override
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
                long when) {

            Projection projection = mapView.getProjection();
            if (shadow == false) {

                Paint paint = new Paint();
                paint.setAntiAlias(true);

                Point point = new Point();
                projection.toPixels(gp1, point);
                paint.setColor(Color.BLUE);

                RectF oval = new RectF(point.x - mRadius, point.y - mRadius,
                        point.x + mRadius, point.y + mRadius);

                canvas.drawOval(oval, paint);
            }

            return super.draw(canvas, mapView, shadow, when);
        }

    }

当你移动时它会画出一个蓝点,你会协调chnage。

答案 1 :(得分:0)

覆盖onLocationChanged方法:

// Create an overlay to show current location
mMyLocationOverlay = new MyLocationOverlay(this, mMapView) {
  @Override
  public void onLocationChanged(android.location.Location location) {
    String text = "New coordinates: " + location.getLatitude() + ", " + location.getLatitude();
    Toast toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
    toast.show();
    super.onLocationChanged(location);
  }

};
mMyLocationOverlay.runOnFirstFix(new Runnable() { public void run() {
  mMapView.getController().animateTo(mMyLocationOverlay.getMyLocation());
}});

mMapView.getOverlays().add(mMyLocationOverlay);