Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Putting a Street View Button on a Google Map

How can I put a Street View button on a fairly typical Google Map so that it is inline with the standard Map/Satellite/Hybrid buttons in the top right corner? I've seen one example of this that I can't find any longer. So, I know it's possible.

like image 851
ssorrrell Avatar asked Mar 03 '09 14:03

ssorrrell


1 Answers

Yes indeed, one can specify control positions in the map's options--look for control positioning in the online docs.

How to specify Google maps controls positions

For positioning the street view control in the top right corner add

  streetViewControlOptions: {
    position: google.maps.ControlPosition.TOP_RIGHT
  }

to your map's options.

Check this demo (that uses the Google maps API version 3):

var myLatlng = new google.maps.LatLng(-33, 151);
var myOptions = {
  center: myLatlng,
  zoom: 5,
  streetViewControl: true,
  streetViewControlOptions: {
    position: google.maps.ControlPosition.TOP_RIGHT
  }
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
#map_canvas {
  width: 300px;
  height: 200px;
}
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<div id="map_canvas"></div>

Here's an another example for control positioning in the official documentation.

Control placement identifiers

These are all possible positions for Google maps controls (from TOP_LEFT to BOTTOM_RIGHT):

  +----------------+ 
  + TL    TC    TR + 
  + LT          RT + 
  +                + 
  + LC          RC + 
  +                + 
  + LB          RB + 
  + BL    BC    BR + 
  +----------------+

For a full list of positions and how they work see https://developers.google.com/maps/documentation/javascript/3.exp/reference#ControlPosition

Changing a control position programmatically

You can also change the position dynamically after creating the map using the SetOptions method of the google.maps.Map class. In this example I create a map as above with streetViewControl in position TOP_RIGHT and then change it to LEFT_BOTTOM:

var myLatlng = new google.maps.LatLng(-33, 151);
var myOptions = {
  center: myLatlng,
  zoom: 5,
  streetViewControl: true,
  streetViewControlOptions: {
    position: google.maps.ControlPosition.TOP_RIGHT
  }
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

// now change position to LEFT_BOTTOM changing streetViewControlOptions
// with setOptions
map.setOptions({
  streetViewControlOptions: {
    position: google.maps.ControlPosition.LEFT_BOTTOM
  }
});
#map_canvas {
  width: 300px;
  height: 200px;
}
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<div id="map_canvas"></div>
like image 113
user2314737 Avatar answered Nov 08 '22 08:11

user2314737