title: Street view API 3 (add custom minimap) close button Street view and Pegman
body: <p>In my script, when you drop the pegman on to the map, it loads Street View with a custom minimap. You can see the minimap with the pegman and move it for Street View, and it works fine. </p>
  
  <p>However, when you click the close button on the street view, the minimap and Street View close fine but the pegman doesn't return to its default position. It stays on the map, and can't be re-dropped.</p>
  
  <p>I want to move the pegman back to default position when Street View is closed, and if you drop it on map again to load Street View like the first time.</p>
  
  <p><strong>online code</strong> <a href="http://jsbin.com/ayejim/edit#preview" rel="nofollow">http://jsbin.com/ayejim/edit#preview</a></p>
  
  <p>This is my function <code>initialize</code>:</p>
  
  <pre><code>    function initialize() {
  var lifestyle = [{}];
    var myOptions = {
      zoom: CITY_MAP_ZOOMING_FACT,
      center: new google.maps.LatLng(CITY_MAP_CENTER_LAT, CITY_MAP_CENTER_LNG),
      mapTypeId: google.maps.MapTypeId.&lt;?php echo $maptype;?&gt;
  
    }
     map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
     map.setOptions({styles: lifestyle});
  
      /////////////////////////////
      var g = google.maps;
      var pano, mini;
      var mapdiv = document.getElementById("map_canvas");
  
      pano = map.getStreetView();
  
  
    /** Listen to panorama's visibility changes to detect
    * when pegman is dropped onto the map
    * and when Street view is closed
    */
  
    g.event.addListener(pano, "visible_changed", function() {
     // Street view activated by dropping pegman
     if (!mini &amp;&amp; pano.getVisible()) {
      // Container for mini map and close button
  
      var c = document.createElement("div");
      c.id = "minimap";
      c.style.visibility = "visible";
  
      var d = document.createElement("div");
      d.id = "closebutton";
      d.onclick = function() {
      // Toggles button icon and moves copyright notice
       var terms = document.getElementById("note");
  //    var terms = mapdiv.childNodes[1].childNodes[2];
        if (c.style.visibility == "visible") {
          c.style.visibility = "hidden";
          d.className = "closed";
          terms.style.marginRight = "24px";
        }
        else {
         c.style.visibility = "visible";
         d.className = "";
         terms.style.marginRight = "170px";
        }
      };
  
      c.appendChild(d);
      mapdiv.appendChild(c);
      mapdiv.appendChild(d);
  
      mini = new g.Map(c, {
        center: pano.getPosition(),
        zoom: 15,
        mapTypeId: "roadmap",
        disableDefaultUI: true,
        streetViewControl: true,
        streetView: pano,
        styles: lifestyle
  
      });
  
      /** Moves the 'Terms' notice to the left
      * to ensure it's not covered up.
      * There are two such notices in the document.
      * Make sure to catch the right ones.
      */
      g.event.addListener(mini, "tilesloaded", function() {
       var terms = mini.getDiv().firstChild.childNodes[2];
       terms.style.marginRight = "24px";
       var sv_terms = mapdiv.childNodes[1].childNodes[2];
       sv_terms.id = "note";
       sv_terms.style.marginRight = "170px";
       sv_terms.style.zIndex = "1";
     });
      // Binds mini map's center to pano position
      mini.bindTo("center", pano, "position");
  
      // Street view finished by click on Street view close button
     } else if (mini instanceof g.Map &amp;&amp; !pano.getVisible()) {
       mapdiv.removeChild(document.getElementById("minimap"));
      **//initialize(); if try call initialize again pegman back default position but map back to default position too like the first time.and markers hide , i don't know why this happens**
     }
   });
   /////////////////////////
     mgr = new MarkerManager( map );
     google.maps.event.addListener(mgr, 'loaded', function() {
        if (markers) {
           for (var level in markers) {
              google.maps.event.addDomListener( document.getElementById( level ), 'click', function() {
                 setCategoryVisiblity( this.id, this.checked );
              });
              for (var i = 0; i &lt; markers[level].length; i++) {
  
                 var details = markers[level][i];
                 var image = new google.maps.MarkerImage(details.icons,new google.maps.Size(PIN_POINT_ICON_WIDTH, PIN_POINT_ICON_HEIGHT));
                 var myLatLng = new google.maps.LatLng(details.location[0], details.location[1]);
                 &lt;?php if(get_current_city_set_zooming_opt() == '1') { ?&gt;
                   multimarkerdata[i]  = new google.maps.LatLng(details.location[0], details.location[1]);
                 &lt;?php } ?&gt;
                 markers[level][i] = new google.maps.Marker({
                    title: details.name,
                    position: myLatLng,
                    icon: image,
                    clickable: true,
                    draggable: false,
                    flat: true,
                    animation: google.maps.Animation.DROP
                 });
  
  
              attachMessage(markers[level][i], details.message);
              }
              mgr.addMarkers( markers[level], 0 );
           }
            &lt;?php if(get_current_city_set_zooming_opt() == '1') { ?&gt;
               var latlngbounds = new google.maps.LatLngBounds();
  
              for ( var j = 0; j &lt; multimarkerdata.length; j++ )
                  {
                   latlngbounds.extend( multimarkerdata[ j ] );
                  }
                 map.fitBounds( latlngbounds );
            &lt;?php } ?&gt;
           mgr.refresh();
        }
     });
  
      // but that message is not within the marker's instance data 
      function attachMessage(marker, msg) {
        var myEventListener = google.maps.event.addListener(marker, 'click', function() {
        if (pano.getVisible()) { 
          infowindow.open(pano, marker); 
        } else { 
        infowindow = new google.maps.InfoWindow(
            { content: String(msg) 
            });
          infowindow.open(map, marker); 
        } 
           if (infowindow) infowindow.close();
          infowindow = new google.maps.InfoWindow(
            { content: String(msg) 
            });
           infowindow.open(map,marker);
        });
      }
  
  }
  
  google.maps.event.addDomListener(window, 'load', initialize);
  </code></pre>