【问题标题】:Google Maps API: Radius circle not drawingGoogle Maps API:未绘制半径圆
【发布时间】:2014-12-02 17:39:19
【问题描述】:

我已按照Google Maps API 网站上的说明进行操作,但我的圈子从未被绘制到我的地图上,而且我真的不明白我错过了什么,有人能指出我正确的方向吗?

这是我将新地址标记和搜索半径添加到地图的方法(注意标记是按预期添加的):

// Add the users point to the map
function addAddress() {

    // Get the users current location
    var location      = document.getElementById("P1_LOCATION").value;  // Users postcode
    var radius_size   = document.getElementById("P1_RADIUS").value;    // Users radius size in miles
    var search_radius;

    // Translate the users location onto the map
    geocoder.geocode({ 'address': location}, function(results, status) {
       if(status == google.maps.GeocoderStatus.OK) {

           // Center around the users location
           map.setCenter(results[0].geometry.location);

           // Place a marker where the user is situated
           var marker = new google.maps.Marker({
                map:map,
                position: results[0].geometry.location
           });

            // configure the radius
            // Construct the radius circle
            var radiusOptions = {
                strokeColor: "#FF0000",
                strokeOpacity: 0.8,
                strokeWeight: 2,
                fillColor: '#FF0000',
                fillOpacity: 0.35,
                map: map, 
                center: marker.center,     // I want to set the center around the users location
                radius: radius_size        // I want the radius to be in miles, how do I do this?
            };

            // add the radius circle to the map
            search_radius = new google.maps.Circle(radiusOptions); 
       } 
    });
}

而且我敢肯定有人会问我是否配置了底图对象,这是在我的初始化方法中完成的:

var geocoder;
var map;

// Create our base map object 
function initialize()
{
   geocoder = new google.maps.Geocoder();
   var latlng = new google.maps.LatLng(0,0);
   var mapOptions = { 
      zoom: 12,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
   }   
   map = new google.maps.Map(document.getElementById("map"), mapOptions);
}

如何让半径显示在给定点周围?任何建议将不胜感激。

【问题讨论】:

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


    【解决方案1】:

    发布的代码出现此错误:Uncaught InvalidValueError: setRadius: not a number 但真正的问题是:center: marker.center, 应该是 center: marker.getPosition(),google.maps.Marker 没有“中心”属性)

    工作代码sn-p:

    // Add the users point to the map
    function addAddress() {
    
      // Get the users current location
      var location = document.getElementById("P1_LOCATION").value; // Users postcode
      var radius_size = parseFloat(document.getElementById("P1_RADIUS").value); // Users radius size in meters (unless you scale it to miles)
      var search_radius;
    
      // Translate the users location onto the map
      geocoder.geocode({
        'address': location
      }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
    
          // Center around the users location
          map.setCenter(results[0].geometry.location);
    
          // Place a marker where the user is situated
          var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
          });
    
          // configure the radius
          // Construct the radius circle
          var radiusOptions = {
            strokeColor: "#FF0000",
            strokeOpacity: 0.8,
            strokeWeight: 2,
            fillColor: '#FF0000',
            fillOpacity: 0.35,
            map: map,
            center: marker.getPosition(), // I want to set the center around the users location
            radius: radius_size // I want the radius to be in miles, how do I do this?
          };
    
          // add the radius circle to the map
          search_radius = new google.maps.Circle(radiusOptions);
        }
      });
    }
    var geocoder;
    var map;
    
    // Create our base map object 
    function initialize() {
      geocoder = new google.maps.Geocoder();
      var latlng = new google.maps.LatLng(0, 0);
      var mapOptions = {
        zoom: 12,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      }
      map = new google.maps.Map(document.getElementById("map"), mapOptions);
    }
    
    google.maps.event.addDomListener(window, "load", initialize);
    html,
    body,
    #map {
      height: 500px;
      width: 500px;
      margin: 0px;
      padding: 0px
    }
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <input id="P1_LOCATION" value="08646" type="text" />
    <input id="P1_RADIUS" value="10000" type="text" />
    <input id="geocode" value="geocode" type="button" onclick="addAddress()" />
    <div id="map" style="width:750px; height:450px; border: 2px solid #3872ac;"></div>

    【讨论】:

    • 谢谢 :) 我没有添加 parseInt 方法,因为它以字符串的形式返回 - 现在一切都对我有用,但会接受你的答案,因为它肯定比我的更简洁。谢谢你的时间男人
    【解决方案2】:

    对于以后遇到同样问题的任何人,请确保您采取API指南中未演示的额外步骤,即设置半径对象所属的地图,在我的情况下:

    search_radius.setMap(map);
    

    【讨论】:

    • 您在代码中设置地图属性。您不应该需要再次设置它(除非地图是在地理编码器返回其结果后定义的,但您没有提供足够的上下文来知道这一点)
    • 谢谢@geocodezip,我觉得在完成教程时很奇怪——我想是我的错添加到我已经编写的代码中,这对 API 来说非常新,但我的新手错误代表:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-21
    • 1970-01-01
    相关资源
    最近更新 更多