10

I need to rearrange the default controls in the Google Map (v3, 3.4) API.

Everything is in place and works, but when I add the mapTypeControl, zoomControl, the streetViewControl and the panControl to the same ControlPosition (i.e. google.maps.ControlPosition.RIGHT_TOP), I can't define the order of how they are rendered.

mapTypeControl has a default of TOP_RIGHT, but changing it to RIGHT_TOP (the default of the other ones mentioned above) add's it to the bottom in this location:

google maps controls

Here's the code for the map options (nevermind the added OSM layer):

var mapOptions = {
zoom: 12,
center: def_center, // is set before this snippet    
mapTypeControl: true,
mapTypeId: user_maptype, // is set before this snippet
mapTypeControlOptions: {
    position: google.maps.ControlPosition.RIGHT_TOP,
    mapTypeIds: [google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.SATELLITE, google.maps.MapTypeId.HYBRID, google.maps.MapTypeId.TERRAIN, "openstreetmap"]
},
zoomControl: true,
zoomControlOptions: {
    style: google.maps.ZoomControlStyle.DEFAULT,
    position: google.maps.ControlPosition.RIGHT_TOP
},
streetViewControl: true,
streetViewControlOptions: {
        position: google.maps.ControlPosition.RIGHT_TOP
},
panControl: false,
panControlOptions: {
        position: google.maps.ControlPosition.RIGHT_TOP
},
scaleControl: false,
scaleControlOptions: {
        position: google.maps.ControlPosition.BOTTOM_RIGHT
}
};

I need the mapTypeControl to be the first element to be rendered (above the streetViewControl). Any idea how to do this?

1
  • 2
    did you ever find a solution to reordering this? i added a custom control and can put it above or below with index 1/0, but I can't update the index of the default controls (specifically panControl)
    – tester
    Oct 6, 2011 at 21:19

2 Answers 2

6

This code works fine in the google code editor @ http://code.google.com/apis/ajax/playground/#control_position_v3

in the end the solution was to use TOP_RIGHT instead of RIGHT_TOP

Additionally the index property can be changed via setAttribute just like a majority of the other properties.

function initialize() {
   var mapDiv = document.getElementById('map-canvas');
   var map = new google.maps.Map(mapDiv, {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      mapTypeControl: true,
      mapTypeControlOptions: { 
         position: google.maps.ControlPosition.TOP_RIGHT
      }, 
      zoomControl: true, 
      zoomControlOptions: { 
         style: google.maps.ZoomControlStyle.DEFAULT, 
         position: google.maps.ControlPosition.RIGHT_TOP 
      }, 
      streetViewControl: true, 
      streetViewControlOptions: { 
         position: google.maps.ControlPosition.RIGHT_TOP 
      }, 
      panControl: false, 
      panControlOptions: { 
         position: google.maps.ControlPosition.RIGHT_TOP 
      }, 
      scaleControl: false, 
      scaleControlOptions: { 
         position: google.maps.ControlPosition.BOTTOM_RIGHT 
      } 
   });
}
5
  • That's pretty much the default layout. I still can't get the mapTypeControl above the pan tool. If you would like to give an example of that, that would be perfect.
    – Andy Chase
    Jun 5, 2012 at 1:24
  • If you went to the playground and pasted by code and pressed run you should have seen the map type selection in the top right and it was above all the other controls... what do you want a screen shot?
    – Jay
    Jun 5, 2012 at 3:34
  • Although the hack proposed by Jay is limited in possible applications (as you can only set the order by dividing all controls in two groups) it definitely does work and might get the job done :) . Jun 5, 2012 at 19:38
  • Thanks Dave, and I didn't think it was a hack !P And why didn't I get my 50 Bounty?!?! It's a conspiracy I tell you!
    – Jay
    Jun 6, 2012 at 0:29
  • 1
    Oh, I see what you did. That hack doesn't work for me because I have custom controls and they push the map type control to the left. I think the best answer to this question is that google controls cannot be custom ordered.
    – Andy Chase
    Jun 9, 2012 at 0:17
2

I don't realy have a solution unfortunately but acording to this page Google Map API V3 Controls user defined controls can be placed before the default ones by changing their index in DOM. Might this be the same problem, that the zoom-control has a lower index than the map/satelite control?
Not sure how you would go about changing the index but it might be an avenue to find a solution.

Oh and if you do find out how to solve it please paste your solution here. Might come in handy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.