首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >:Radius圆未绘制

:Radius圆未绘制
EN

Stack Overflow用户
提问于 2014-12-02 17:39:19
回答 2查看 1.9K关注 0票数 0

我遵循了Google网站上的指示,但是我的圆圈从来没有被绘制到我的地图上,我也不知道我错过了什么,谁能给我指明正确的方向吗?

下面是我向地图中添加新地址标记和搜索半径的方法(注意,该标记是按预期添加的):

代码语言:javascript
复制
// 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); 
       } 
    });
}

我肯定有人会问我是否配置了一个基map对象,这是在我的初始化器方法中完成的:

代码语言:javascript
复制
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);
}

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-12-02 18:10:10

我在发布的代码:Uncaught InvalidValueError: setRadius: not a number中得到了这个错误,但真正的问题是:center: marker.center,应该是center: marker.getPosition(), ( google.maps.Marker没有"center“属性)

工作代码片段:

代码语言:javascript
复制
// 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);
代码语言:javascript
复制
html,
body,
#map {
  height: 500px;
  width: 500px;
  margin: 0px;
  padding: 0px
}
代码语言:javascript
复制
<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>

票数 1
EN

Stack Overflow用户

发布于 2014-12-02 17:53:41

对于将来遇到同样问题的任何人,请确保采取API指南中未演示的额外步骤,即设置radius对象也属于的Map,在我的例子中:

代码语言:javascript
复制
search_radius.setMap(map);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27255622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档