如何检查在Google Maps v3中选择的地图类型

时间:2014-12-03 22:36:33

标签: javascript google-maps-api-3

我在页面上有一个谷歌地图。我需要检查用户选择的地图类型。

有人可以告诉我如何执行此检查吗?



//set map options
var mapOptions = {
                center: newCenter,
                zoom: newZoom,
                styles: stylesArray,
                mapTypeControlOptions: {
                    mapTypeIds: [google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.TERRAIN, google.maps.MapTypeId.SATELLITE, google.maps.MapTypeId.HYBRID],
                }
            };

//create map
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

if (map.getCurrentMapType() == google.maps.MapTypeId.ROADMAP) {
  //set the current map type  
  map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
}




1 个答案:

答案 0 :(得分:1)

map.getMapTypeId()返回当前的mapTypeId。

document.getElementById('maptype').value=map.getMapTypeId();

working fiddle

工作代码段:



var mapOptions = {
  center: {
    lat: 45,
    lng: -112
  },
  zoom: 4,
  // styles: stylesArray,
  mapTypeControlOptions: {
    mapTypeIds: [google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.TERRAIN, google.maps.MapTypeId.SATELLITE, google.maps.MapTypeId.HYBRID],
  }
};

//create map
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

if (map.getCurrentMapType() == google.maps.MapTypeId.ROADMAP) {
  //set the current map type  
  map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
}

html,
body,
#map-canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}

<script src="https://maps.googleapis.com/maps/api/js"></script>
<input type="button" value="get map type" onclick="document.getElementById('maptype').value=map.getMapTypeId();" />
<input type="text" value="" id="maptype" />
<div id="map-canvas"></div>
&#13;
&#13;
&#13;