从一个角度出发

时间:2011-11-09 00:19:57

标签: javascript

我想使用Google Maps API在地图上的某个特定点周围绘制一个区域,人们可以在X分钟内走到该区域。换句话说,如何在用户定义的时间段内找到可以从给定点旅行的所有地点?

2 个答案:

答案 0 :(得分:4)

以下是可立即复制的解决方案:http://maps.forum.nu/gm_driving_radius.html#。另一种近乎完美的解决方案是使用空间填充曲线或空间索引。 sfc将2d复杂度降低到1d复杂度,并且它是近似算法而不是启发式算法。使用sfc,它应该是1/4的保证,以找到euklidian空间中的所有点 enter image description here

答案 1 :(得分:1)

<强>更新 玩一玩:

<!DOCTYPE html>
<html> 
<head> 
   <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
   <title>Google Maps Geometry</title> 
   <script src="http://maps.google.com/maps/api/js?sensor=false" 
           type="text/javascript"></script>

</head> 
<body> 
   <div id="map" style="width: 1000px; height: 800px"></div> 

   <script type="text/javascript"> 

   function CalculateDistanceBetweenTwoPoint(lat1, lon1, lat2, lon2) {

        var R = 6371; // km
        var dLat = (lat2-lat1).toRad();
        var dLon = (lon2-lon1).toRad();
        var lat1 = lat1.toRad();
        var lat2 = lat2.toRad();

        var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
        var d = R * c;      
        return d * 1000;

   }



      Number.prototype.toRad = function() {
         return this * Math.PI / 180;
      }

      Number.prototype.toDeg = function() {
         return this * 180 / Math.PI;
      }

      google.maps.LatLng.prototype.destinationPoint = function(brng, dist) {
         dist = dist / 6371;  
         brng = brng.toRad();  

         var lat1 = this.lat().toRad(), lon1 = this.lng().toRad();

         var lat2 = Math.asin(Math.sin(lat1) * Math.cos(dist) + 
                              Math.cos(lat1) * Math.sin(dist) * Math.cos(brng));

         var lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(dist) *
                                      Math.cos(lat1), 
                                      Math.cos(dist) - Math.sin(lat1) *
                                      Math.sin(lat2));

         if (isNaN(lat2) || isNaN(lon2)) return null;

         return new google.maps.LatLng(lat2.toDeg(), lon2.toDeg());
      }

      var pointA = new google.maps.LatLng(40.80, -74.00);   // Circle center
      var radius = 1;                                      // 10km

      var mapOpt = { 
         mapTypeId: google.maps.MapTypeId.TERRAIN,
         center: pointA,
         zoom: 17
      };

      var map = new google.maps.Map(document.getElementById("map"), mapOpt);

      // Draw the circle
      new google.maps.Circle({
         center: pointA,
         radius: radius * 1000,       // Convert to meters
         fillColor: '#FF0000',
         fillOpacity: 0.2,
         map: map
      });

      // Show marker at circle center
      new google.maps.Marker({
         position: pointA,
         map: map
      });

      var i = 0;
      setTimeout(LoopThroughPoints, 1000);


    var triangleCoords = [];

    function LoopThroughPoints() {


         var directionsService = new google.maps.DirectionsService();

            var destination = pointA.destinationPoint(i, radius);

          var request = {
              origin: pointA,
              destination: destination,
              travelMode: google.maps.TravelMode["WALKING"]
          };
          console.log(request);

          directionsService.route(request, function(response, status) {
        console.log(response);

            if (status == google.maps.DirectionsStatus.OK) {

                var cumulativeDistance = 0;

                new google.maps.Marker({
                            position: destination,
                            map: map
                        });


                loopy: for(var i = 0; i < response.routes[0].overview_path.length; i++)
                {

                    var path = response.routes[0].overview_path[i];
                    var pathNext = response.routes[0].overview_path[i+1];

                    cumulativeDistance += CalculateDistanceBetweenTwoPoint(path.Oa, path.Pa, pathNext.Oa, pathNext.Pa);

                    if(cumulativeDistance >= 500) {
                        triangleCoords.push(new google.maps.LatLng(path.Oa, path.Pa));
                        break loopy;                        
                    }

                }

            }
          });


        i = i + 10;
        if(i!=360) {

            setTimeout(LoopThroughPoints, 10);

        } else {
            bermudaTriangle = new google.maps.Polygon({
                paths: triangleCoords,
                strokeColor: "#FF0000",
                strokeOpacity: 0.8,
                strokeWeight: 2,
                fillColor: "#FF0000",
                fillOpacity: 0.35
            });

            bermudaTriangle.setMap(map);
        }


      }



   </script> 
</body> 
</html>

只需快速查看一些初学者,尽管有人可能会提供更好的解决方案。

似乎没有一种方法可以通过Maps API让所有道路都脱离某一点。

距离: 花费的时间很难,我会​​尝试以米为单位进行计算。所以根据http://answers.yahoo.com/question/index?qid=1006021202308(科学我知道......)5km / h或6分钟500m。

我的方法是:

这也可能有所帮助:

How to move a marker 100 meters with coordinates

更多! Google Maps: Given a point, how to find all points at a given road distance?

显示如何查找所有点,然后捕捉到最近的街道