【问题标题】:Android Google Map how to check if user is in marker circle regionAndroid Google Map如何检查用户是否在标记圈区域
【发布时间】:2014-04-01 21:26:47
【问题描述】:

我正在使用 Google 地图,并希望能够使用用户当前位置来检测用户是否在标记的半径范围内(放置在地图上)。我有用户当前位置坐标(纬度和经度)和标记的坐标,但不知道如何计算用户是否在该区域内。下面的图片可能最能说明我想要做什么。

我有一个这样的算法:

map.OnInfoWindowClickListener(new OnInfoWindowClickListener()){
    public void isUserInArea(Marker marker){

        float[] distance = new float[2];
           //users current location 
        Location.distanceBetween(currentLocation.getLatitude(),currentLocatiocation.getLongitude(),
        marker.getPosition().latitude, marker.getPosition().longitude, distance);
}

找不到当前位置是否在标记区域内,因为我无法掌握标记圈。然而,这是我最接近它的地方。非常感谢帮助。

为了帮助,我在这样的地图上添加了一个圆圈

// adding circle to map
circleOptions = new CircleOptions();
circleOptions.center(new LatLng(locationlist.get(i)
        .getLatitude(), locationlist.get(i).getLongitude()));
circleOptions.radius(20);
circleOptions.strokeColor(Color.BLUE);
circleOptions.fillColor(Color.BLUE);
map.addCircle(circleOptions);

//check that all tasks are in circle radius

Marker locationMarker = map.addMarker(markerOp);
locationMarker.showInfoWindow();

【问题讨论】:

    标签: google-maps distance area marker


    【解决方案1】:

    我用这个:

    Location.distanceBetween(mMarker.getPosition().latitude,
                mMarker.getPosition().longitude, mCircle.getCenter().latitude,
                mCircle.getCenter().longitude, distance);
    
    if (distance[0] > mCircle.getRadius()) {
          //Do what you need
    
    }else if (distance[0] < mCircle.getRadius()) {
    //Do what you need
    }
    

    【讨论】:

      【解决方案2】:

      为什么不使用接近警报?

      您可以这样做:不要画一个圆圈,而是像这样在地图上放置一个Marker

      map.addMarker(new MarkerOptions()
          .title(name)
          .icon(BitmapDescriptorFactory.fromResource(R.drawable.gpsmap))
          .position(coordinates)
          .flat(true)
          .rotation(90));
      

      然后调用以下方法向它添加一个接近警报:

      // Proximity alert
      private void addProximityAlert(double lat, double lng, int id){
          Intent intent = new Intent(PROX_ALERT_INTENT);
          PendingIntent proximityIntent = PendingIntent.getBroadcast(this, id, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
          locationManager.addProximityAlert(lat, lng, POINT_RADIUS, EXPIRATION, proximityIntent);
      }
      

      其中EXPIRATION = -1(因此接近警报不会过期)和POINT_RADIUS 采用前一个圆的半径值。

      然后你需要在地图上添加一个接近警报监听器:

      IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
      registerReceiver(new ProximityIntentReceiver(), filter);
      

      在哪里PROX_ALERT_INTENT = "yourProjectPackageName.ProximityActivity";

      希望对你有帮助

      【讨论】:

        【解决方案3】:

        本教程可以满足您的要求。 User entry exit proximity alert

        一切顺利。

        【讨论】:

          猜你喜欢
          • 2021-08-20
          • 2022-10-06
          • 2015-07-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-02
          • 2017-10-10
          相关资源
          最近更新 更多