1

我正在尝试构建一个谷歌地图应用程序,它具有基于用户输入的一些功能。所以我有这个大的融合表,里面有很多与地理点相关的维基百科文章。我试图让用户输入地址并在距搜索点 3 或 10 英里范围内显示结果。这是我的代码。我遇到的第一个问题是我在搜索时无法清理现有标记,然后我需要显示结果。关于如何做到这一点的任何想法?方向部分效果很好。

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style>
   html, body, #map-canvas {
     height: 100%;
     margin: 0px;
     padding: 0px
   }
   #panel {
     position: absolute;
     top: 5px;
     left: 50%;
     margin-left: -180px;
     z-index: 5;
     background-color: #fff;
     padding: 5px;
     border: 1px solid #999;
   }
 </style>
 <style>
   #directions-panel {
     height: 100%;
     float: right;
     width: 390px;
     overflow: auto;
   }

   #map-canvas {
     margin-right: 390px;
   }

         .dirhead{
             background-color: rgb(30, 113, 160);
             color: white;
             font-size: 25px;
             padding: 10px;
         }

   #control {
     background: #fff;
     padding: 5px;
     font-size: 14px;
     font-family: Arial;
     border: 1px solid #ccc;
     box-shadow: 0 2px 2px rgba(33, 33, 33, 0.4);
     display: none;
   }

   @media print {
     #map-canvas {
       height: 500px;
       margin: 0;
     }

     #directions-panel {
       float: none;
       width: auto;
     }
   }
 </style>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script
            src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/src/markerclusterer.js">
        </script>
    <script
            src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
        </script>
<style type="text/css">
  html { height: 100% }
  body { height: 100%; margin: 0; padding: 0 }
  #map-canvas { height: 100% }
</style>
<script type="text/javascript"
  src="https://maps.googleapis.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
    google.load('visualization', '1');
    var map;
    var directionsDisplay;
    var geocoder;
    var directionsService = new google.maps.DirectionsService();
    var markers = [];
function initialize(lat,lng) {
        geocoder = new google.maps.Geocoder();
        directionsDisplay = new google.maps.DirectionsRenderer();
        var lat = 39.50;
        var lng = -98.35;
        var bounds;
        var infoWindow = new google.maps.InfoWindow();
        var markerClusterer = null;
        directionsDisplay.setPanel(document.getElementById('directions-panel'));

  var mapOptions = {
    center: new google.maps.LatLng(lat,lng),
    zoom: 6,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById("map-canvas"),
    mapOptions);
            directionsDisplay.setMap(map);
     // var query = "SELECT wikipedia_article,location FROM 1NICsBtZWfpi3kF_3OHsDe7Z9CU2biwyx_A4HRg";
//         query = encodeURIComponent(query);
        var control = document.getElementById('control');
        control.style.display = 'block';
        map.controls[google.maps.ControlPosition.TOP_CENTER].push(control);
        google.maps.event.addListener(map, 'tilesloaded', function() {
                 loadmapHere(map,infoWindow,"regular");
        });
}


    function loadmapHere(map,infoWindow){
            var swPoint = map.getBounds().getSouthWest();
            var nePoint = map.getBounds().getNorthEast();
                console.log(swPoint);
                console.log(nePoint);
            // Now, each individual coordinate
            var swLat = swPoint.lat();
            var swLng = swPoint.lng();
            var neLat = nePoint.lat();
            var neLng = nePoint.lng();
              var query = "SELECT wikipedia_article,location FROM " + '1NICsBtZWfpi3kF_3OHsDe7Z9CU2biwyx_A4HRg where ST_INTERSECTS(location, RECTANGLE(LATLNG('+swLat+', '+swLng+'),LATLNG('+neLat+', '+neLng+')))';
          var encodedQuery = encodeURIComponent(query);

         // Construct the URL
          var url = ['https://www.googleapis.com/fusiontables/v1/query'];
          url.push('?sql=' + encodedQuery);
          url.push('&key=key here);
          url.push('&callback=?');

              $.ajax({
           url: url.join(''),
           dataType: 'jsonp',
           success: function (data) {
             var rows = data['rows'];
             for (var i in rows) {
               var wiki_article = rows[i][0];
               var position = rows[i][1];
               var location = position.split(' ');
               var lat = location[0];
               var lng = location[1];
               var positioni = new google.maps.LatLng(lat, lng);
               var marker = new google.maps.Marker({
                             map: map,
                 position: positioni,
                 icon: new google.maps.MarkerImage('https://cdn1.iconfinder.com/data/icons/Map-Markers-Icons-Demo-PNG/32/Map-Marker-Ball-Pink.png'),
                             customInfo: wiki_article
               });
                         markers.push(marker);
                         google.maps.event.addListener(markers[i], 'click', function(e) {
                             infoWindow.setContent('Wiki Link: <a href="'+decodeURIComponent(this.customInfo)+'" target="_new">'  + decodeURIComponent(this.customInfo) + '</a>');
                             infoWindow.open(map, this);
                                 document.getElementById('destination').value = this.position;
                         });
               }
                         markerClusterer = new MarkerClusterer(map, markers);
             }
         });
        }

        function showMeNear() {
            var infoWindow = new google.maps.InfoWindow();
        var address = document.getElementById("milesAddress").value;
        geocoder.geocode( { 'address': address}, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
                    google.maps.event.clearListeners(map, 'tilesloaded');
                    hideMarkers(map,markers);
            map.setCenter(results[0].geometry.location);
                    var layer = new google.maps.FusionTablesLayer({
                        query: "SELECT Address FROM 1NICsBtZWfpi3kF_3OHsDe7Z9CU2biwyx_A4HRg WHERE ST_INTERSECTS(location, RECTANGLE(LATLNG(37.3, -122.3), LATLNG(37.6, -121.9)))"
                      });
                      layer.setMap(map);
          } else {
            alert("Geocode was not successful for the following reason: " + status);
          }
        });
      }


        function calcRoute() {
          var start = document.getElementById('initAddress').value;
          var end = document.getElementById('destination').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);
            }
          });
        }

        function hideMarkers(map, markers) {
    /* Remove All Markers */
    while(markers.length){
        markers.pop().setMap(null);
    }

    console.log("Remove All Markers");
    }

  google.maps.event.addDomListener(window, 'load', initialize);
</script>
 </head>
 <body>
    <div id="control">
        <strong>Search for Articles in:</strong>
        <select id="milesMenu">
          <option value="3">3 miles</option>
          <option value="10">10 miles</option>
          <option value="17">17 miles</option>
          <option value="25">25 miles</option>
        </select>
         from <input type="text" id="milesAddress" placeholder="Enter Address"><button class="showNear" onclick="showMeNear()">Go</button><br />
          <strong>Get Directions from here:</strong><input type="text" id="initAddress" placeholder="Enter Address"></input><input id="destination" type="hidden" value=""></input> to the clicked marker <button class="givemeRoute" onclick="calcRoute()">Go</button>
    </div>
    <div id="directions-panel"><div class="dirhead">Directions Here</div></div>
<div id="map-canvas"/>
</body>
</html>
4

0 回答 0