javascript - 谷歌将圆圈映射到折线坐标数组

标签 javascript google-maps geometry polyline

如何从 google.maps.Circle 的对象中获取折线坐标数组

enter image description here

没有关于那个的 api 文档条目

最佳答案

google.maps.Circle 不包含坐标数组。如果您想要一个形状像圆形的 google.maps.Polygon,则需要制作一个。

function drawCircle(point, radius, dir) { 
  var d2r = Math.PI / 180;   // degrees to radians 
  var r2d = 180 / Math.PI;   // radians to degrees 
  var earthsradius = 3963; // 3963 is the radius of the earth in miles

  var points = 32; 

  // find the raidus in lat/lon 
  var rlat = (radius / earthsradius) * r2d; 
  var rlng = rlat / Math.cos(point.lat() * d2r); 

  var extp = new Array(); 
  if (dir==1) {
     var start=0;
     var end=points+1; // one extra here makes sure we connect the path
  } else {
     var start=points+1;
     var end=0;
  }
  for (var i=start; (dir==1 ? i < end : i > end); i=i+dir)  
  { 
     var theta = Math.PI * (i / (points/2)); 
     ey = point.lng() + (rlng * Math.cos(theta)); // center a + radius x * cos(theta) 
     ex = point.lat() + (rlat * Math.sin(theta)); // center b + radius y * sin(theta) 
     extp.push(new google.maps.LatLng(ex, ey)); 
  } 
  return extp;
}

var circle = new google.maps.Polygon({
               map: map,
               paths: [drawCircle(new google.maps.LatLng(-33.9,151.2), 100, 1)],
               strokeColor: "#0000FF",
               strokeOpacity: 0.8,
               strokeWeight: 2,
               fillColor: "#FF0000",
               fillOpacity: 0.35
});

Example

代码片段:

function drawCircle(point, radius, dir) {
  var d2r = Math.PI / 180; // degrees to radians 
  var r2d = 180 / Math.PI; // radians to degrees 
  var earthsradius = 3963; // 3963 is the radius of the earth in miles

  var points = 32;

  // find the raidus in lat/lon 
  var rlat = (radius / earthsradius) * r2d;
  var rlng = rlat / Math.cos(point.lat() * d2r);


  var extp = new Array();
  if (dir == 1) {
    var start = 0;
    var end = points + 1
  } // one extra here makes sure we connect the
  else {
    var start = points + 1;
    var end = 0
  }
  for (var i = start;
    (dir == 1 ? i < end : i > end); i = i + dir) {
    var theta = Math.PI * (i / (points / 2));
    ey = point.lng() + (rlng * Math.cos(theta)); // center a + radius x * cos(theta) 
    ex = point.lat() + (rlat * Math.sin(theta)); // center b + radius y * sin(theta) 
    extp.push(new google.maps.LatLng(ex, ey));
    bounds.extend(extp[extp.length - 1]);
  }
  // alert(extp.length);
  return extp;
}

var map = null;
var bounds = null;

function initialize() {
  var myOptions = {
    zoom: 10,
    center: new google.maps.LatLng(-33.9, 151.2),
    mapTypeControl: true,
    mapTypeControlOptions: {
      style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
    },
    navigationControl: true,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  map = new google.maps.Map(document.getElementById("map_canvas"),
    myOptions);

  bounds = new google.maps.LatLngBounds();

  var donut = new google.maps.Polygon({
    paths: [drawCircle(new google.maps.LatLng(-33.9, 151.2), 100, 1),
      drawCircle(new google.maps.LatLng(-33.9, 151.2), 50, -1)
    ],
    strokeColor: "#0000FF",
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: "#FF0000",
    fillOpacity: 0.35
  });
  donut.setMap(map);

  map.fitBounds(bounds);
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map_canvas {
  width: 100%;
  height: 100%;
  padding: 0px;
  margin: 0px;
}
<script src="https://maps.google.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map_canvas"></div>

关于javascript - 谷歌将圆圈映射到折线坐标数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14664685/

相关文章:

javascript - Extjs Store to Array 获取对象属性

javascript - URL参数表达IN关键字

javascript - scrollIntoView 影响我的监听器

javascript - 检索谷歌地图标记的属性值

3D线平面相交,与简单平面

ruby-on-rails - Rails Ruby Geometry Gem 投入生产

javascript - 数据数组作为 extjs 中的模型属性

ios - Google Maps 为点击和未选择状态绘制不同的标记图像

c# - 根据经度和纬度计算最近位置的算法

python - 使用 Python turtle 制作没有 circle 函数的圆