【问题标题】:How to make anything when my geolocation is changed?当我的地理位置改变时如何做任何事情?
【发布时间】:2012-09-25 13:06:13
【问题描述】:

我想进行一些操作,即每次更改我的地理位置时显示带有文本“我在这里”的对话框。

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

我怎样才能做到这一点?

【问题讨论】:

    标签: android google-maps android-location


    【解决方案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);
            }
    
        }
    

    当你移动和你的坐标改变时,它会画一个蓝点。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 我知道,但我做不到——你能给我举个例子吗?
      猜你喜欢
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 2011-12-17
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多