Detect if marker is within circle overlay on Google Maps (Javascript API V3)

前端 未结 2 633
名媛妹妹
名媛妹妹 2020-12-08 05:18

I have markers dotted around a map, and a radius (circle overlay) on a marker marking your location (which changes every time you move). Is there any way I can check to see

相关标签:
2条回答
  • 2020-12-08 05:50

    Here's a way to add a contains method to the google.maps.Circle class. It first uses the bounding box to exclude a point if it's not even in the bounding box. If it is in the bounding box, then it compares the distance from the point to the center with the radius, and returns true only if the distance is shorter than the radius.

    Once you add the javascript below, you can call the contains() method on your circle object.

    google.maps.Circle.prototype.contains = function(latLng) {
      return this.getBounds().contains(latLng) && google.maps.geometry.spherical.computeDistanceBetween(this.getCenter(), latLng) <= this.getRadius();
    }
    
    0 讨论(0)
  • 2020-12-08 05:52
    google.maps.Circle.prototype.contains = function(latLng) {
      return this.getBounds().contains(latLng) && google.maps.geometry.spherical.computeDistanceBetween(this.getCenter(), latLng) <= this.getRadius();
    }
    
    0 讨论(0)
提交回复
热议问题