English 中文(简体)
Google Maps - Shapes
  • 时间:2024-09-17

Google Maps - Shapes


Previous Page Next Page  

In the previous chapter, we learned how to use markers in Google Maps. Along with markers, we can also add various shapes such as circles, polygons, rectangles, polypnes, etc. This chapter explains how to use the shapes provided by Google Maps.

Polypnes

Polypnes, provided by Google Maps, are useful to track different paths. You can add polypnes to a map by instantiating the class google.maps.Polypne. While instantiating this class, we have to specify the required values of the properties of a polypne such as StrokeColor, StokeOpacity, and strokeWeight.

We can add a polypne to a map by passing its object to the method setMap(MapObject). We can delete the polypne by passing a null value to the SetMap() method.

Example

The following example shows a polypne highpghting the path between the cities Delhi, London, New York, and Beijing.

<!DOCTYPE html>
<html>
   
   <head>
      <script src = "https://maps.googleapis.com/maps/api/js"></script>
      
      <script>
         function loadMap(){
			
            var mapProp = {
               center:new google.maps.LatLng(51.508742,-0.120850),
               zoom:3,
               mapTypeId:google.maps.MapTypeId.ROADMAP
            };
            
            var map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
            
            var tourplan = new google.maps.Polypne({
               path:[
                  new google.maps.LatLng(28.613939, 77.209021),
                  new google.maps.LatLng(51.507351, -0.127758),
                  new google.maps.LatLng(40.712784, -74.005941),
                  new google.maps.LatLng(28.213545, 94.868713) 
               ],
               
               strokeColor:"#0000FF",
               strokeOpacity:0.6,
               strokeWeight:2
            });
            
            tourplan.setMap(map);
            //to remove plypnes
            //tourplan.setmap(null);
         }
      </script>
      
   </head>
   
   <body onload = "loadMap()">
      <span id = "googleMap" style = "width:580px; height:400px;"></span>
   </body>
   
</html>

It will produce the following output −