Google Map Circle Radius无效的问题

时间:2016-06-23 18:11:08

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

我有以下代码允许用户向外扩展并在带有滑块的地图中。我的代码主要在这里,但我无法让它工作。这是显示地图的代码。 它不会为我更新半径。非常感谢任何帮助

<div data-role="collapsible">
    <h4>Location</h4>
    <center><p>
    <table>
      <tr>  
      <td colspan="2">

    </td></tr>
    </table>
    </p></center>
  </div>
<center>
    <script>getLocation();</script>
    <button onclick="getLocation()">Get My Location</button><p id="map"></p>

    Search Radius:
    <input type="range" name="search_radius" id="search_radius" min="10" max="100">
    <script>
        // Add a Circle overlay to the map.
        circle = new google.maps.Circle({
            map: map,
            radius: 10
        });

        // Since Circle and Marker both extend MVCObject, you can bind them
        // together using MVCObject's bindTo() method.  Here, we're binding
        // the Circle's center to the Marker's position.
        // http://code.google.com/apis/maps/documentation/v3/reference.html#MVCObject
        circle.bindTo('center', marker, 'position');
        }


        $("#search_radius").slider({
            orientation: "vertical",
            range: "min",
            max: 3000,
             min: 100,
            value: 500,
            slide: function(event, ui) {
                updateRadius(circle, ui.value);
            }
        });

        function updateRadius(circle, rad) {
            circle.setRadius(rad);
        }

        // Register an event listener to fire when the page finishes loading.
        google.maps.event.addDomListener(window, 'load', init);

    </script


</center>

下一部分是缩放功能和滑块

    <!--Check Geolocation Support -->
<script>
var x = document.getElementById("map");

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function showPosition(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude;
     var mapDiv = document.getElementById('map');
    var map = new google.maps.Map(mapDiv, {
      center: {lat: position.coords.latitude, lng: position.coords.longitude},
      zoom: 12
    });
}

function initMap() {
     var mapDiv = document.getElementById('map');
    var map = new google.maps.Map(mapDiv, {
      center: {lat: 53.3498, lng: -6.2603},
      zoom: 6
    });

  }

  function updateRadius(circle, rad) {
    circle.setRadius(rad);
    map.fitBounds(circle.getBounds());
  }

</script>
<script src="https://maps.googleapis.com/maps/api/js?callback=initMap" async defer></script>

1 个答案:

答案 0 :(得分:1)

似乎无效容器用于jQuery Slider元素:

<input type="range" name="search_radius" id="search_radius" min="10" max="100">

被替换为div容器:

<div id="search_radius"></div>

滑块小部件按预期开始工作。

示例

&#13;
&#13;
function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 5,
    center: { lat: -24.397, lng: 150.644 }
  });


  var marker = new google.maps.Marker({
    position: map.getCenter(),
    map: map,
  });

  var radius = 500; //in kilometers

  // Add a Circle overlay to the map.
  var circle = new google.maps.Circle({
    map: map,
    radius: 1000 * radius
  });
  circle.bindTo('center', marker, 'position');

  initSlider(circle);

}

function updateRadius(circle, rad) {
  var map = circle.getMap();
  circle.setRadius(rad * 1000);
  map.fitBounds(circle.getBounds());
}



function initSlider(circle) {
  $("#search_radius").slider({
    orientation: "vertical",
    range: "min",
    max: 3000,
    min: 100,
    value: circle.getRadius() / 1000,
    step: 100,
    slide: function (event, ui) {
      updateRadius(circle, ui.value);
      //console.log(ui.value);
    }
  });
  var map = circle.getMap();
  map.controls[google.maps.ControlPosition.LEFT_CENTER].push($("#search_radius").get(0));

}

google.maps.event.addDomListener(window, 'load', initMap);
&#13;
    html,
        body {
            height: 100%;
            margin: 0;
            padding: 0;
        }
        
        #map {
            height: 100%;
        }


        #search_radius {
            left: 20px !important;
        }

        
        
&#13;
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="https://maps.google.com/maps/api/js"></script>

<div id="search_radius"></div>
<div id="map"></div>
&#13;
&#13;
&#13;

Plunker