Google api v3在客户道路上显示附近的标记

时间:2014-07-23 14:10:19

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

我如何只显示标记(它们是预定义的,但是对于整个地图是隐藏的),这些标记位于我选择的道路附近(可能是半径10英里或20英里),使用Google api v3 示例< / strong>我使用Directions service

HTML:

<div id="panel">
   <b>start: </b>
   <input type="text" id="start" name="start" maxlength="30" onchange="calcRoute();" />
   <b>end: </b>
   <input type="text" id="end" name="end" maxlength="30" onchange="calcRoute();" />
</div> 
<div id="map"></div>

JavaScript的:

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

function initialize() {
   directionsDisplay = new google.maps.DirectionsRenderer();
   var chicago = new google.maps.LatLng(41.891224, -87.638675);
   var mapOptions = {
     zoom:7,
     center: chicago
}

map = new google.maps.Map(document.getElementById('map'), mapOptions);
directionsDisplay.setMap(map);

/* === markers === */
var locations = [
  ['1', 40.577651, -82.200443],
  ['2', 40.760976, -93.911868],
  ['3', 39.110017, -111.116458],
  ['4', 27.036116, -81.717045],
  ['5', 34.104058, -117.444583],
  ['6', 44.790790, -121.443607],
];

var marker, i;

for (i = 0; i < locations.length; i++) {
    marker = new google.maps.Marker({
       map: map,
       title: locations[i][0],
       position: new google.maps.LatLng(locations[i][1], locations[i][2]),
       //visible: false,  //true for all, but hidden
       icon:'img/the_icon.png'
    });
}   

}


function calcRoute() {
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);
  }
});
}

google.maps.event.addDomListener(window, 'load', initialize);

2 个答案:

答案 0 :(得分:2)

从纽约到洛杉矶的路线20英里范围内有2个标记:

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);
      }
  }
}

答案 1 :(得分:1)

Example using JSTS(来自此问题:How to draw a polygon around a polyline in JavaScript?。使用google.maps.geometry.poly.containsLocation

代码:

function calcRoute() {
    var start = document.getElementById('start').value;
    var end = document.getElementById('end').value;
    var request = {
        origin: start,
        destination: end,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    directionsService.route(request, function (response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
            var overviewPath = response.routes[0].overview_path,
                overviewPathGeo = [];
            for (var i = 0; i < overviewPath.length; i++) {
                overviewPathGeo.push(
                [overviewPath[i].lng(), overviewPath[i].lat()]);
            }

            var distance = 10 / 111.12, // Roughly 10km
                geoInput = {
                    type: "LineString",
                    coordinates: overviewPathGeo
                };
            var geoInput = googleMaps2JTS(overviewPath);
            var geometryFactory = new jsts.geom.GeometryFactory();
            var shell = geometryFactory.createLineString(geoInput);
            var polygon = shell.buffer(distance);

            var oLanLng = [];
            var oCoordinates;
            oCoordinates = polygon.shell.points[0];
            for (i = 0; i < oCoordinates.length; i++) {
                var oItem;
                oItem = oCoordinates[i];
                oLanLng.push(new google.maps.LatLng(oItem[1], oItem[0]));
            }
            if (routePolygon && routePolygon.setMap) routePolygon.setMap(null);
            routePolygon = new google.maps.Polygon({
                paths: jsts2googleMaps(polygon),
                map: map
            });
            for (var j=0; j< gmarkers.length; j++) {
              if (google.maps.geometry.poly.containsLocation(gmarkers[j].getPosition(),routePolygon)) {
                gmarkers[j].setMap(map);
              } else {
                gmarkers[j].setMap(null);
              }
           }
        }
    });
}