Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何通过单击按钮更改google地图类型?_Javascript_Html_Angularjs_Google Maps Api 3_Angularjs Directive - Fatal编程技术网

Javascript 如何通过单击按钮更改google地图类型?

Javascript 如何通过单击按钮更改google地图类型?,javascript,html,angularjs,google-maps-api-3,angularjs-directive,Javascript,Html,Angularjs,Google Maps Api 3,Angularjs Directive,我为google map做了一个指令,允许传入mapType和地址值: .directive('map', function(){ return { restrict: 'E', replace: true, template: '<div></div>', scope: { 'address': "@", 'mapType': "@" }, link: function(scope, ele

我为google map做了一个指令,允许传入mapType和地址值:

.directive('map', function(){
return {
    restrict: 'E',
    replace: true,
    template: '<div></div>',
    scope: {
        'address': "@",
        'mapType': "@"
    },
    link: function(scope, element, attrs) {
        console.log(scope, attrs);
        var geoCoder = new google.maps.Geocoder();
        geoCoder.geocode(
            {'address': scope.address},
            function(result, status){
                console.log(result, status);
                if(status==="OK"){
                    map.setCenter(result[0].geometry.location);
                    marker.setPosition(result[0].geometry.location);
                }
            }
        );
        var mapOptions = {
            zoom: 16,
            mapTypeId: eval(scope.mapType),
            draggable: false
        };
        if(scope.mapType === undefined || scope.mapType === ""){
           mapOptions.mapTypeId = google.maps.MapTypeId.ROADMAP; 
        }
        var map = new google.maps.Map(document.getElementById(attrs.id), mapOptions);
        var marker = new google.maps.Marker({
            map: map,
        })
    }
  };
})
这是查看地图的视图层:

<div class="row noPadding">
    <div ng-click="select(option)" class="col textCenter mapOption" ng-class="{'mapOptionActived': option == selected}" ng-repeat="option in mapModules">
          {{option.text}}
    </div>
</div>
<map map-type="{{mapType}}" id="schoolMap" style="height: 200px; width:100%" address="2140 w 41st Vancouver, BC"></map>

{{option.text}
第一次加载页面时一切正常,但是当我单击地图模块按钮时,地图类型没有改变。因此,我正在寻找一种解决方案,允许用户在单击选项按钮时更改地图类型


谢谢。

您可以这样做:

var mapType = "ROADMAP";
map.setMapTypeId(google.maps.MapTypeId[mapType]);
var mapType = "ROADMAP";
map.setMapTypeId(google.maps.MapTypeId[mapType]);