how to create mutiple rectangle on gmaps? Ask Question

I want to create multiple rectangle on gmaps but I don’t know where must I put the code.

This is my sample code

 function initMap() {
    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 5,
      center: {lat: 37.0924, lng: -119.4179324},
      mapTypeId: 'terrain'
    });

	var contentString = '<div id="content">'+
        '<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large 
        '</div>';
		
	  var infowindow = new google.maps.InfoWindow({
      content: contentString
    });

    var rectangle = new google.maps.Rectangle({
      strokeColor: '#FF0000',
      strokeOpacity: 0.8,
      strokeWeight: 2,
      fillColor: '#FF0000',
      fillOpacity: 0.35,
      map: map,
      bounds: 
      new google.maps.LatLngBounds
  <?php 
	  $con = mysqli_connect("localhost","root","","test");
	  
	  $query=mysqli_query($con,"SELECT * FROM map");
	  $cc=mysqli_fetch_array($query);
	  ?>
      (
        new google.maps.LatLng (<?php echo $cc['nelat']; ?>, <?php echo $cc['nelng']; ?>),
		new google.maps.LatLng (<?php echo $cc['swlat']; ?>, <?php echo $cc['swlng']; ?>),
        ),

      
    });
	  rectangle.addListener('click', function() {
      infowindow.open(map, rectangle);
    });
  }
    #map {
        height: 100%;
      }
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
  <meta charset="utf-8">
  <title>Rectangles</title>
   
  </head>
  <body>
    <div id="map"></div>
    <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAogXD-AHrsmnWinZIyhRORJ84bgLwDPpg&callback=initMap">
  </script>
</body>
</html>

One thought on “how to create mutiple rectangle on gmaps? Ask Question”

  1. Add a separate rectangle variable:

    function initMap() {
            var map = new google.maps.Map(document.getElementById('map'), {
              zoom: 5,
              center: {lat: 37.0924, lng: -119.4179324},
              mapTypeId: 'terrain'
            });
    
            var rectangle = new google.maps.Rectangle({
              strokeColor: '#FF0000',
              strokeOpacity: 0.8,
              strokeWeight: 2,
              fillColor: '#FF0000',
              fillOpacity: 0.35,
              map: map,
              bounds: 
              new google.maps.LatLngBounds
    
              (
                new google.maps.LatLng (37.778261, -119.4179324),new google.maps.LatLng (36.255123, -115.2383485),
                ),
    
              
            });
            var rectangle2 = new google.maps.Rectangle({
              strokeColor: '#FF0000',
              strokeOpacity: 0.8,
              strokeWeight: 2,
              fillColor: '#FF0000',
              fillOpacity: 0.35,
              map: map,
              bounds: 
              new google.maps.LatLngBounds
    
              (
                new google.maps.LatLng (30.778261, -119.4179324),new google.maps.LatLng (29.255123, -115.2383485),
                ),
    
              
            });
          }
        #map {
            height: 100%;
          }
          html, body {
            height: 100%;
            margin: 0;
            padding: 0;
          }
    <!DOCTYPE html>
    <html>
    <head>
      <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
      <meta charset="utf-8">
      <title>Rectangles</title>
       
      </head>
      <body>
        <div id="map"></div>
        <script async defer
        src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAogXD-AHrsmnWinZIyhRORJ84bgLwDPpg&callback=initMap">
      </script>
    </body>
    </html>

Leave a Reply

Your email address will not be published. Required fields are marked *