google.maps.Circle捕捉到最近的英里

时间:2014-08-25 14:46:53

标签: javascript google-maps google-maps-api-3

在google v3 api中创建了一些功能,用户可以在其中放置一个引脚。一旦它们放下一个引脚,就会以引脚为中心创建一个可编辑的圆。他们可以扩展圆圈,看看在infowindow中弹出的距离。问题是难以到达最近的一英里或半英里。我会尝试通过拖动圆圈将它移到最近的英里,它会变得像10.23英里......

有人对此有推荐吗?    http://jsfiddle.net/wa8s0dya/

//create circle
    function createCircle() {
        if ((circle != null) && circle.setMap) {
            circle.setMap(null);
            circle = null;
        }
        var options = {
            strokeColor: '#0099FF',
            strokeOpacity: 0.8,
            strokeWeight: 2,
            fillColor: '#0099FF',
            fillOpacity: 0.35,
            map: map,
            center: marker.getPosition(),
            editable: true,
            radius: 500

        };

        // Add the circle for this city to the map.
        circle = new google.maps.Circle(options);
        google.maps.event.addListener(circle, 'radius_changed', function () {
            removeInfoWindow();
            popUpPinInfo(marker, circle.radius, map);
        });

        google.maps.event.addListener(circle, 'circlecomplete', function () {
            removeInfoWindow();
            popUpPinInfo(marker, circle.radius, map);
        });


    }

1 个答案:

答案 0 :(得分:2)

一英里是1609.34米(圆的半径以米为单位)。

要使圆圈大小以1英里为增量,您需要设置半径(+ 8米使其不会向下舍入到.99英里):

circle.setRadius(circle.getRadius() - (circle.getRadius() % 1609.34)+8);

每当半径改变时:

google.maps.event.addListener(circle, 'radius_changed', function () {
  if ((circle.getRadius() % 1609.34) > 160) {
      circle.setRadius(circle.getRadius() - (circle.getRadius() % 1609.34)+8) // meters per mile 
  }
  removeInfoWindow();
  popUpPinInfo(marker, circle.radius, map);
});
google.maps.event.addListener(circle, 'circlecomplete', function () {
  circle.setRadius(circle.getRadius() - (circle.getRadius() % 1609.34)+8) // meters per mile 
  removeInfoWindow();
  popUpPinInfo(marker, circle.radius, map);
});

updated fiddle