Make the Most of the Google Maps JavaScript API: Symbols and Strokeweights

Make the Most of the Google Maps JavaScript API: Symbols and Strokeweights

  
Published in Switched On: The Bowdark Blog -
Enterprise Integration
Enterprise Mobility

This Google Maps JavaScript API tip comes hot from a Bowdork in the trenches.

Recently, I was working with a supply chain optimizer customer to create a Geospatial Map with the Google Maps JavaScript API to visualize quantities of an item from a specific location. I had initially used Google’s Circle class to display this info, and when we decided we needed these circles to scale with the zoom level of the map, I figured I would do this with the Zoom event. However, it became increasingly difficult to manage the original size and scale when zooming in and out. It also caused some performance issues as I would have to resize any number of these at a time.

After digging through the Google Maps API, I found that this could actually be done by using the Marker class and its ‘icon’ property instead. ‘icon’ lets you add an Icon or a Symbol to the map in place of the default Marker image. Using the Icon class lets you use an image by providing an image URL. The Symbol class asks you provide either a SymbolPath provided by Google or use a custom SVG path notation. Doing either of these allows the marker to resize using the default Marker code and allows the developer to make use of all available Marker events. In my case, I used a Symbol and one of the Maps API’s default SymbolPaths, CIRCLE.

plaintext
// Snip - we've created a map object above
// Snip - we're looping on an array of items with "quantity" 
// properties and setting Marker objects for each item
new google.maps.Marker({
  position: map.getCenter(),
  icon: {
    // Make the shape a nice, clean circle
    path: google.maps.SymbolPath.CIRCLE,
    // Setting the 'scale' to the quantity gives a great
    // visual size indication
    scale: item.quantity 
  },
  draggable: false, // they stay where the actual items are!
  map: map,
});

The Symbol property to focus on is ‘scale’. This sets the amount the symbol is scaled in size. In other words, this is how large your symbol will appear on the screen next to others — a perfect way to demonstrate relative quantities visually. By default, this is set to the Symbol’s ‘strokeweight’ property or 1. What I instead did was set the item quantity to the ‘scale’ and let the Marker class do the scaling by default. As an example in the image below, the blue circle is at a scale of 10, while the red circle is at a scale of 20:

One thing to note: If using this, I would recommend any number set for ‘scale’ be under 50 and especially not over 100. Otherwise, you risk some massive performance issues, a struggling component, and a completely blocked map no matter how far you zoom. :)

About the Author

Paul Modderman headshot
Paul Modderman

Paul Modderman loves creating things and sharing them. His tech career has spanned web applications with technologies like .NET, Java, Python, and React to SAP solutions in ABAP, OData and SAPUI5, to cloud technologies in Google Cloud Platform, Amazon Web Services, and Microsoft Azure. He was principal technical architect on Mindset's certified solutions CloudSimple and Analytics for BW. He's an SAP Developer Hero, honored in 2017. Paul is the author of two books: Mindset Perspectives: SAP Development Tips, Tricks, and Projects, and the SAP Press published SAPUI5 and SAP Fiori: The Psychology of UX Design.

An error has occurred. This application may no longer respond until reloaded. Reload 🗙