如何在Google地图上单击一下,添加圆圈并获取圆的坐标和半径?

时间:2014-04-15 06:00:19

标签: javascript google-maps

我想在地图上单击一下创建一个圆圈,我也想收集圆圈的Lat / Lon信息及其半径。请建议我如何使用JavaScript。

此致 马杜

1 个答案:

答案 0 :(得分:0)

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Circles</title>
<style>
  html, body, #map-canvas {
    height: 100%;
    margin: 0px;
    padding: 0px
  }
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
// This example creates circles on the map with fixed radius of 5km

var cityCircle;

function initialize() {
// Create the map.
var mapOptions = {
zoom: 14,
center: new google.maps.LatLng(37.09024, -95.712891),//can use your center
mapTypeId: google.maps.MapTypeId.TERRAIN
};

var map = new google.maps.Map(document.getElementById('map-canvas'),
  mapOptions);

google.maps.event.addListener(map, "click", function (e) {

//lat and lng is available in e object
var latLng = e.latLng;
// Construct the circleOptions
var circleOptions = {
  //optional to modify
 /* strokeColor: '#FF0000',
  strokeOpacity: 0.8,
  strokeWeight: 2,
  fillColor: '#FF0000',
  fillOpacity: 0.35,*/
  map: map,
  center: latLng,
  radius: 5000 //in meters apporx.
};
// Add the circle for this city to the map.
cityCircle = new google.maps.Circle(circleOptions);
});

}

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

</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>