Dela via


Image and Canvas Pushpin Example

Note

Bing Maps Web Control SDK retirement

Bing Maps Web Control SDK is deprecated and will be retired. Free (Basic) account customers can continue to use Bing Maps Web Control SDK until June 30th, 2025. Enterprise account customers can continue to use Bing Maps Web Control SDK until June 30th, 2028. To avoid service disruptions, all implementations using Bing Maps Web Control SDK will need to be updated to use Azure Maps Web SDK by the retirement date that applies to your Bing Maps for Enterprise account type. For detailed migration guidance, see Migrate from Bing Maps Web Control SDK and Migrate Bing Maps Enterprise applications to Azure Maps with GitHub Copilot.

Azure Maps is Microsoft's next-generation maps and geospatial services for developers. Azure Maps has many of the same features as Bing Maps for Enterprise, and more. To get started with Azure Maps, create a free Azure subscription and an Azure Maps account. For more information about azure Maps, see Azure Maps Documentation. For migration guidance, see Bing Maps Migration Overview.

Its fairly easy to create a custom pushpin using an HTML5 canvas, however if you are trying to draw an image on your canvas it can be a bit tricky. Images load asynchronously, as such you need to load them before you can draw them to the canvas. This code example shows how to create custom colored pushpins that use an image as a template. The template image looks like this:

Icon of a pushpin with a partially transparent white color.   TransparentPushpin.png

Note that the circular part of the pushpin has some transparency. By drawing a circle filled with a desired color on a canvas and then overlaying this image on top we end up with a nice easy way to create custom colored pushpins that fit an image template.

<!DOCTYPE html>
<html>
<head>
   <title></title>
   <meta charset="utf-8" />
	<script type='text/javascript'>
   function GetMap() {
       var map = new Microsoft.Maps.Map('#myMap', {});

       createColoredPushpin(map.getCenter(), 'red', function (pin) {
           map.entities.push(pin);
       });        
   }

   function createColoredPushpin(location, color, callback) {
       var img = new Image();
       img.onload = function () {
           var c = document.createElement('canvas');
           c.width = img.width;
           c.height = img.height;

           var context = c.getContext('2d');

           //Draw a colored circle behind the pin
           context.beginPath();
           context.arc(13, 13, 11, 0, 2 * Math.PI, false);
           context.fillStyle = color;
           context.fill();

           //Draw the pushpin icon
           context.drawImage(img, 0, 0);

           var pin = new Microsoft.Maps.Pushpin(location, {
               //Generate a base64 image URL from the canvas.
               icon: c.toDataURL(),
               anchor: new Microsoft.Maps.Point(12, 39)
           });

           if (callback) {
               callback(pin);
           }
       };

       img.src = 'images/TransparentPushpin.png';
   }
   </script>
   <script type='text/javascript' src='http://www.bing.com/api/maps/mapcontrol?callback=GetMap&key=[YOUR_BING_MAPS_KEY]' async defer></script>
</head>
<body>
   <div id="myMap" style="position:relative;width:600px;height:400px;"></div>
</body>
</html>

Here is what this pushpin looks like on the map.

Screenshot of a Bing map showing a red button in the middle of the transparent pushpin that is in the center of the map.