开发者

Circle overlay in Google Map V3 API js

开发者 https://www.devze.com 2023-04-11 18:47 出处:网络
I am trying to insert this circle on a google map, the map displays fine by including the following js file in the header;

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({
  开发者_Python百科          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...


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
 });


Try changing your listener code to this:

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号