【问题标题】:How to add circles around markers on Google Maps API如何在 Google Maps API 上的标记周围添加圆圈
【发布时间】:2015-11-22 20:10:53
【问题描述】:

这是我用来在谷歌地图的几个地方显示标记的代码。 我想对此进行补充,并在标记周围添加一个 5 公里的圆圈。

我尝试添加这段代码但没有运气:

var circle = new google.maps.Circle({
center:position, 
radius: radius, 
fillColor: "#0000FF", 
fillOpacity: 0.1, 
map: map, 
strokeColor: "#FFFFFF", 
strokeOpacity: 0.1, 
strokeWeight: 2 
});

这是下面的完整代码,这里有一个jsfiddle供参考

var center = "Vilnius, Lithuania";
var locations = [
  ['Tuskulėnų g. 20, Vilnius 09211, Lithuania', "some info"],
  ['Stumbrų g. 21, Vilnius 08101, Lithuania', "more information"],
  ['Kalvarijų g. 55, Vilnius 09317, Lithuania', "blah, blah"],
  ['Birželio 23-iosios g. 6, Vilnius 03204, Lithuania', "other information"],
  ['Teatro g. 6, Vilnius 03107, Lithuania', "to be determined"]
];


var geocoder;
var map;
var infoWin = new google.maps.InfoWindow();

function initialize() {
  geocoder = new google.maps.Geocoder();
  map = new google.maps.Map(
    document.getElementById("map_canvas"));
  // center and zoom map on "center" address
  geocoder.geocode({
    address: center
  }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      map.fitBounds(results[0].geometry.bounds);
    } else {
      alert("Geocode was not successful for the following reason: " + status);
    }
  });
  for (var i = 0; i < locations.length; i++) {
    codeAddress(locations[i], i);
  }

}

function codeAddress(location, i) {
  geocoder.geocode({
    'address': location[0]
  }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      map.setCenter(results[0].geometry.location);
      var marker = new google.maps.Marker({
        map: map,
        title: "marker " + i,
        position: results[0].geometry.location
      });
      google.maps.event.addListener(marker, 'click', function(evt) {
        infoWin.setContent(location[0] + "<br>" + location[1]);
        infoWin.open(map, this);
      })


//i added this block
var circle = new google.maps.Circle({
center:position, 
radius: radius, 
fillColor: "#0000FF", 
fillOpacity: 0.1, 
map: map, 
strokeColor: "#FFFFFF", 
strokeOpacity: 0.1, 
strokeWeight: 2 
});
//end of block addition

    } else {
      alert("Geocode was not successful for the following reason: " + status);
    }
  });
}

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

更新

这是我从控制台得到的

radius is not defined

【问题讨论】:

  • 我没有看到任何尝试在发布的代码中添加圆圈(或就此而言的小提琴)。你试过什么没用?
  • @geocodezip 嗨,我在最后一个}else{ 之前添加了代码块,以便在i 的循环中
  • 在发布的代码中仍然看不到它。只需添加它并适当调整变量对我有用,您是否收到 javascript 错误(例如未定义 position,未定义 radius)?
  • @geocodezip 我已经更新了我的问题,并添加了我从控制台返回的内容。 radius is not defined
  • 您是否尝试定义半径?

标签: javascript jquery google-maps


【解决方案1】:

您只需定义radius 和position 变量,例如:

var radius = 1000;
var position = marker.getPosition();

对于缩放,您可以使用map.setZoom();,例如在setCenter 之后添加它:

map.setCenter(results[0].geometry.location);
map.setZoom(5);

Working fiddle.


代码 sn-p:

var center = "Vilnius, Lithuania";
var locations = [
  ['Tuskulėnų g. 20, Vilnius 09211, Lithuania', "some info"],
  ['Stumbrų g. 21, Vilnius 08101, Lithuania', "more information"],
  ['Kalvarijų g. 55, Vilnius 09317, Lithuania', "blah, blah"],
  ['Birželio 23-iosios g. 6, Vilnius 03204, Lithuania', "other information"],
  ['Teatro g. 6, Vilnius 03107, Lithuania', "to be determined"]
];


var geocoder;
var map;
var infoWin = new google.maps.InfoWindow();

function codeAddress(location, i) {
  geocoder.geocode({
    'address': location[0]
  }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      map.setCenter(results[0].geometry.location);
      map.setZoom(5);

      var marker = new google.maps.Marker({
        map: map,
        title: "marker " + i,
        position: results[0].geometry.location
      });
      google.maps.event.addListener(marker, 'click', function(evt) {
        infoWin.setContent(location[0] + "<br>" + location[1]);
        infoWin.open(map, this);
      })

      var radius = 1000;
      var position = marker.getPosition();

      var circle = new google.maps.Circle({
        center: position,
        radius: radius,
        fillColor: "#0000FF",
        fillOpacity: 0.1,
        map: map,
        strokeColor: "#FFFFFF",
        strokeOpacity: 0.1,
        strokeWeight: 2
      });

    } else {
      alert("Geocode was not successful for the following reason: " + status);
    }
  });
}

google.maps.event.addDomListener(window, "load", function() {
  geocoder = new google.maps.Geocoder();
  map = new google.maps.Map(
    document.getElementById("map_canvas"));
  // center and zoom map on "center" address
  geocoder.geocode({
    address: center
  }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      google.maps.event.addListenerOnce(map, 'bounds_changed', function() {
        map.setZoom(map.getZoom() + 3);
      });
      map.fitBounds(results[0].geometry.bounds);

    } else {
      alert("Geocode was not successful for the following reason: " + status);
    }
  });

  for (var i = 0; i < locations.length; i++) {
    codeAddress(locations[i], i);
  }
});
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
}
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>

【讨论】:

  • 嘿真的谢谢你,但我想使用职位名称,而不是 latlng。并且在所有标记周围都有一个圆圈,而不是一个通用的......
  • 不客气,检查我更新的答案(也更新了小提琴)。
  • 我接受了你的回答,非常感谢,真的很有帮助。在第 22 行,就在 map:map 下方,我添加了一个 zoom:5 没有运气
  • 不客气,一般情况下应该添加到map = new google.maps.Map(document.getElementById("map_canvas"), {zoom: 20});。
  • 您好,您可以帮我使用缩放功能吗?
猜你喜欢
  • 1970-01-01
  • 2013-12-26
  • 1970-01-01
  • 1970-01-01
  • 2013-02-23
  • 1970-01-01
  • 2013-03-06
  • 1970-01-01
  • 2015-12-18
相关资源
最近更新 更多