Circle overlay in Google Map V3 API js

 ̄綄美尐妖づ 提交于 2019-12-24 17:15:42

问题


I am trying to insert this circle on a google map, the map displays fine by including the following js file in the header;

/**
       * Called on the intiial page load.
       */
      function initialize() {
        var latLng = new google.maps.LatLng(50.272213,-5.054973);
        var options = {
          'zoom': 9,
          'center': latLng,
          'mapTypeId': google.maps.MapTypeId.ROADMAP
        };

        var map = new google.maps.Map(document.getElementById('map'),
            options);

      }

      // Register an event listener to fire when the page finishes loading.
      google.maps.event.addDomListener(window, 'load', initialize);

But when i add the following to include the circle it breaks and doesn't load;

 /**
       * Called on the intiial page load.
       */
      function initialize() {
        var latLng = new google.maps.LatLng(50.272213,-5.054973);
        var options = {
          'zoom': 9,
          'center': latLng,
          'mapTypeId': google.maps.MapTypeId.ROADMAP
        };

        var map = new google.maps.Map(document.getElementById('map'),
            options);

        // Add a Circle overlay to the map.
            var circle = new google.maps.Circle({
            map: map,
            center: new google.maps.LatLng(50.272213,-5.054973),
            fillColor: #00FF00,
            fillOpacity: 0.2,
            strokeColor: #00FF00,
            strokeOpacity: 0.4,
            strokeWeight: 2
            });
            circle.setRadius(18362.55489862987);

      }

      // Register an event listener to fire when the page finishes loading.
      google.maps.event.addDomListener(window, 'load', initialize);

If somebody could point out where im going wrong please...


回答1:


Try changing your listener code to this:

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

Here is a working example: http://jsfiddle.net/pVh3b/1/




回答2:


Don't call setRadius, just specify radius as a parameter of the circle options. Also given that you've already got a latlng object created with the same coordinates as the center of the circle, just reuse that.

 var circle = new google.maps.Circle({
            map: map,
            center: latLng,
            fillColor: #00FF00,
            fillOpacity: 0.2,
            strokeColor: #00FF00,
            strokeOpacity: 0.4,
            strokeWeight: 2,
            radius: 18362.55489862987
 });


来源:https://stackoverflow.com/questions/7712861/circle-overlay-in-google-map-v3-api-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!