javascript - 在标记中心添加圆圈

标签 javascript html google-maps google-maps-api-3

我想在 map 上添加一个圆圈,在我从谷歌地图获得的当前标记的位置可以吗?我对html和javascript的了解非常有限。 这是我的代码..

    <!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Reverse Geocoding</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"></script>
    <script>


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


 function initializeMap() {

     if (navigator.geolocation) {
         navigator.geolocation.getCurrentPosition(function (position) {
             revereGeoCode(position.coords.latitude, position.coords.longitude);
         });
     } else {
         console.log("er")
         // make sure to handle the failure
     }

     var mapOptions = {
         zoom: 20,
         center: new google.maps.LatLng(38.078111, 23.734414),
         mapTypeId: 'roadmap'
     }
     map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
 }

 function revereGeoCode(lat, lng) {
     geocoder = new google.maps.Geocoder();
     var latlng = new google.maps.LatLng(lat, lng);
     geocoder.geocode({
         'LatLng': LatLng
     }, function (results, status) {
         if (status == google.maps.GeocoderStatus.OK) {
             if (results[1]) {
              // place your marker coding
                map.setZoom(20);

                // Define circle options
                var circleOptions = {
                    strokeColor: '#FF0000',
                    strokeOpacity: 0.8,
                    strokeWeight: 2,
                    fillColor: '#FF0000',
                    fillOpacity: 0.35,
                    map: map,
                    center: latlng,
                    radius: 20
                };

                // Add the circle to the map.
                var markerCircle = new google.maps.Circle(circleOptions);

                marker = new google.maps.Marker({
                    position: latlng,
                    map: map

                });



                 infowindow.setContent(results[0].formatted_address);
                 infowindow.open(map, marker);
             } else {
                 alert('No results found');
             }
         } else {
             alert('Geocoder failed due to: ' + status);
         }


     });
 }




 google.maps.event.addDomListener(window, 'load', initializeMap);
   </script>

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

html 和 javascript 非常有限。这是我的代码..

最佳答案

只需创建圆圈并使用与标记相同的坐标定义其中心即可。

// Define circle options
var circleOptions = {
    strokeColor: '#FF0000',
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: '#FF0000',
    fillOpacity: 0.35,
    map: map,
    center: latlng,
    radius: 20
};

// Add the circle to the map.
var markerCircle = new google.maps.Circle(circleOptions);

就是这样。

JSFiddle demo

关于javascript - 在标记中心添加圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25192737/

相关文章:

javascript - 创建在 IE 中工作的文件上传

html - 我需要创建一个比我的普通表更难的表

javascript - 谷歌地图/gmap3 - 绘制从用户地理位置到已知目的地的路线 - 需要帮助

javascript - 我的 ajax 调用数据部分中的 php 文件有什么问题?

javascript - 如何在提交表单时关闭模态弹出窗口?

javascript - 如何在 NodeJS 中使用 ldapjs 连接 LDAP

安卓谷歌室内地图

javascript - mithriljs 组件状态不会更新回调或 promise

html - 如何使用 css 让子 div 单独响应?

javascript - 使用 Laravel 自动完成谷歌地图