javascript - 围绕中心点绘制多边形

标签 javascript google-maps-api-3

我尝试了很多,但无法找出问题所在。我想围绕特定的纬度、经度绘制一个多边形。多边形将由特定半径内的 13 个坐标组成。

  1. 在文本框中输入地址和半径。
  2. 地理代码获取该地址的经纬度
  3. 将 map 居中放置在那里。
  4. 以该中心点为半径绘制多边形
  5. 多边形应由 13 个坐标组成

代码

function showAddress(address, miles) {
            var geocoder = new google.maps.Geocoder();
            geocoder.geocode({
                address : address
            }, function(results, status) {
                if(status == google.maps.GeocoderStatus.OK) {
                    //searchLocationsNear(results[0].geometry.location);
                    var cordinate = results[0].geometry.location;
                    //alert(cordinate);
                    var mapOptions = {
                        center : cordinate,
                        zoom : 8,
                        mapTypeId : google.maps.MapTypeId.ROADMAP,
                        overviewMapControl : true,
                        overviewMapControlOptions : {
                            opened : true,
                            position : google.maps.ControlPosition.BOTTOM_LEFT
                        }

                    };

                    //
                    //var address = document.getElementById("address").value;
                    var radius = 1;
                    var latitude = 23.1793013;
                    var longitude = 75.78490970000007;
                    //Degrees to radians
                    var d2r = Math.PI / 180;

                    //  Radians to degrees
                    var r2d = 180 / Math.PI;

                    // Earth radius is 3,963 miles
                    var cLat = (radius / 3963) * r2d;

                    var cLng = cLat / Math.cos(latitude * d2r);

                    //Store points in array
                    var points = [];
                    alert("declare array");

                    var bounds = new google.maps.LatLngBounds();

                    // Calculate the points
                    // Work around 360 points on circle
                    for(var i = 0; i < 13; i++) {

                        var theta = Math.PI * (i / 180);

                        // Calculate next X point
                        circleY = longitude + (cLng * Math.cos(theta));
                        //console.log("CircleY:"+circleY);
                        // Calculate next Y point
                        circleX = latitude + (cLat * Math.sin(theta));
                        //console.log("circleX:"+circleX);
                        // Add point to array
                        var aPoint = new google.maps.LatLng(circleX, circleY);
                        points.push(aPoint);
                        bounds.extend(aPoint);

                    }
                    points.push(points[0]);
                    //console.log(points);
                    //to complete circle

                    var colors = ["#CD0000", "#2E6444", "#003F87"];

                    var Polyline_Path = new google.maps.Polyline({
                        path : points,
                        strokeColor : colors[0],
                        // color of the outline of the polygon
                        strokeOpacity : 1,
                        // between 0.0 and 1.0
                        strokeWeight : 1,
                        // The stroke width in pixels
                        fillColor : colors[1],
                        fillOpacity : 0
                    });
                    var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
                    Polyline_Path.setMap(map);


                } else {
                    alert(address + ' not found');
                }
            });
        }

最佳答案

替换i<13;i++通过

i<360;i+=360/13

这会起作用

谢谢

编辑:不需要最后一点,因为 gmap 会自动关闭它

关于javascript - 围绕中心点绘制多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19691765/

相关文章:

javascript -\n 在 Javascript 中破坏 PHP 代码

javascript - 如何将 x 名玩家多次随机分成 2 队,每次都不同?

google-maps - 将我的 HTML Google MAP API 版本 2 迁移到版本 3

php - 一个巨大的 XML 文件是否包含长 SQL 语句?

javascript - 如何在输入地址时发送新的地理定位请求并刷新 Google map ?

google-maps - 印度的谷歌地图 API

javascript - 在 WordPress 帖子中为所有图像设置样式

javascript - Angularjs:ng-model 绑定(bind)在第一个 AJAX 请求后不起作用

javascript - 在 Google Maps API V3 中使水变得透明

javascript - 一些 jsPlumb 问题