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.
Yes indeed, one can specify control positions in the map's options--look for control positioning in the online docs.
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.
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
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With