none
How to push pin right after bing map loaded RRS feed

  • Question

  • I want to have pin on my map by default. My code is like this, but I cannot see the pin.

     

    <script type="text/javascript">
          var map = null;

    function getMap()
          {
            map = new Microsoft.Maps.Map(document.getElementById('myMap'), {credentials: '.....'});
            map.setView({zoom: 4,
                         center: new Microsoft.Maps.Location(60, -95),
                         mapTypeId : Microsoft.Maps.MapTypeId.road
                         });
            addDefaultPushpin();
          }
         
          function addDefaultPushpin()
          {
            var pushpin= new Microsoft.Maps.Pushpin(map.getCenter(), null);
            map.entities.push(pushpin);
          }

    </script>



    • Edited by hairinwind Saturday, January 14, 2012 12:49 AM
    • Moved by Ricky_Brundritt Saturday, March 10, 2012 12:50 PM (From:Bing Maps: Map Control and Web services Development)
    Saturday, January 14, 2012 12:48 AM

Answers

  • Your original code works, but the map is likely still moving as the default pushpin is added which is likely appearing else where as you are adding it to the center of the map. If you want to set the view you should do it in the map options. Alternatively you can add a pushpin using the view change end event.
    http://rbrundritt.wordpress.com
    Tuesday, January 17, 2012 11:37 AM

All replies

  • Add a MapResolved event to your map, when event occurs, call yours addDefaultPushpin() method.

    Best Regards

    Saturday, January 14, 2012 9:22 PM
  • which browser are you trying?  below just works fine for me in IE & chrome.

     

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
       <head>
          <title>Add default pushpin</title>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
          <script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
          <script type="text/javascript">
          var map = null;
                
          function getMap()
          {
            map = new Microsoft.Maps.Map(document.getElementById('myMap'), {center: new Microsoft.Maps.Location(60, -95), zoom:4, credentials: 'Your Bing Maps Key'});
      addDefaultPushpin();    }
          
          function addDefaultPushpin()
          {
            var pushpin= new Microsoft.Maps.Pushpin(map.getCenter(), null); 
            map.entities.push(pushpin);
          }
          </script>
       </head>
       <body onload="getMap();">
          <div id='myMap' style="position:relative; width:400px; height:400px;"></div>
         </body>
    </html>


    MSFT
    Hemant Goyal
    Sunday, January 15, 2012 7:36 AM
  • I tried in both IE8 and Firefox 8.

    Eventually I found if I removed the code below, it worked.

    map.setView({zoom: 4,
                         center: new Microsoft.Maps.Location(60, -95),
                         mapTypeId : Microsoft.Maps.MapTypeId.road
                         });

    Monday, January 16, 2012 5:12 PM
  • Strange, That should not happen. Could you try placing all options in setView() in map constructor as below: 

     var pushpin= new Microsoft.Maps.Pushpin(map.getCenter(), {credentials: 'xxx', zoom: 4, center: new Microsoft.Maps.Location(60, -95),                                                                                                           mapTypeId : Microsoft.Maps.MapTypeId.road
                                                                                                      });


    MSFT
    Hemant Goyal
    Monday, January 16, 2012 5:19 PM
  • Your original code works, but the map is likely still moving as the default pushpin is added which is likely appearing else where as you are adding it to the center of the map. If you want to set the view you should do it in the map options. Alternatively you can add a pushpin using the view change end event.
    http://rbrundritt.wordpress.com
    Tuesday, January 17, 2012 11:37 AM