Google api v3 show nearby markers on customer road

后端 未结 2 1046
盖世英雄少女心
盖世英雄少女心 2020-12-19 20:37

How can I show only the markers (they are predefined, but hidden for the whole map), which are nearby (may by radius of 10mile or 20mile) to the road I choose using Google a

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-19 21:28

    You have 2 markers within 20 miles of a route from NY to LA:

    example fiddle using RouteBoxer

    function calcRoute() {
      // Clear any previous route boxes from the map
      clearBoxes();
    
      // Convert the distance to box around the route from miles to km
      distance = 20 * 1.609344;
    
        var start = document.getElementById('start').value;
        var end = document.getElementById('end').value;
        var request = {
            origin: start,
            destination: end,
            travelMode: google.maps.TravelMode.DRIVING
        };
        directionsService.route(request, function (response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(response);
                // Box around the overview path of the first route
                var path = response.routes[0].overview_path;
                var boxes = routeBoxer.box(path, distance);
                drawBoxes(boxes);
            } else alert("Directions request failed: " + status);
        });
    }
    
    // Draw the array of boxes as polylines on the map
    function drawBoxes(boxes) {
      boxpolys = new Array(boxes.length);
      for (var i = 0; i < boxes.length; i++) {
        boxpolys[i] = new google.maps.Rectangle({
          bounds: boxes[i],
          fillOpacity: 0,
          strokeOpacity: 1.0,
          strokeColor: '#000000',
          strokeWeight: 1,
          map: map
        });
          for (var j=0; j< gmarkers.length; j++) {
              if (boxes[i].contains(gmarkers[j].getPosition()))
                  gmarkers[j].setMap(map);
          }
      }
    }
    

提交回复
热议问题