Blkc
Blkc

Reputation: 612

Circle not displaying in gmap

Here is my code:

function initMap() {

    var myLatLng = {lat: {{userInfo.lat}}, lng: {{userInfo.lng}} };

    var map = new google.maps.Map(document.getElementById('gmap'), {
        zoom: 16,
        center: myLatLng
    });

    // Add circle overlay and bind to marker
    var circle = new google.maps.Circle(document.getElementById('gmap'), {
        strokeColor: '#FF0000',
        strokeOpacity: 0.8,
        strokeWeight: 2,
        fillColor: '#FF0000',
        fillOpacity: 0.35,
        map: map,
        center: myLatLng,
        radius: 100    // 10 miles in metres
    });

}

console.log(myLatLng); showed that lat and lng is there and the map actually zoom to where I want. However circle is not appearing. Can anyone help me out?

Upvotes: 0

Views: 621

Answers (1)

duncan
duncan

Reputation: 31912

You're passing in two parameters to the Circle constructor; your circle options, and also document.getElementById('gmap'), which isn't required. That's only used for the Map constructor. See https://developers.google.com/maps/documentation/javascript/reference#Circle

You just need to do:

var circle = new google.maps.Circle({
        strokeColor: '#FF0000',
        strokeOpacity: 0.8,
        strokeWeight: 2,
        fillColor: '#FF0000',
        fillOpacity: 0.35,
        map: map,
        center: myLatLng,
        radius: 100    // 10 miles in metres
    });

code snippet:

function initMap() {

  var myLatLng = {lat: 42, lng:-72};

  var map = new google.maps.Map(document.getElementById('gmap'), {
    zoom: 16,
    center: myLatLng
  });

  // Add circle overlay and bind to marker
  var circle = new google.maps.Circle({
    strokeColor: '#FF0000',
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: '#FF0000',
    fillOpacity: 0.35,
    map: map,
    center: myLatLng,
    radius: 100 // 100 metres
  });

}
google.maps.event.addDomListener(window, 'load', initMap);
html,
body,
#gmap {
  height: 100%;
  width: 100%;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="gmap"></div>

Upvotes: 3

Related Questions