简体   繁体   English

无法使用html按钮元素控制Google Maps API

[英]Failing to control Google Maps API with html button elements

I am trying to follow some sample code snippets from an eBook about the google maps API, but I am unable to use some button tags with appropriate IDs to change some of the maps' properties using javascript. 我正在尝试遵循关于google maps API的电子书中的一些示例代码片段,但是我无法使用带有适当ID的某些button标签来使用javascript更改某些地图的属性。

Is there any reason why the following code is unable to change these properties when the buttons are clicked? 是否有任何原因导致单击按钮时以下代码无法更改这些属性? In addition, is there a reason why sometimes when I move the line of JavaScript that calls buttonEvents() on the page load the map fails to load all together? 另外,为什么有时在我移动页面加载时调用buttonEvents()的JavaScript buttonEvents() ,地图无法全部加载在一起的原因?

// global variable of map
var map;
// the name initMap is just convention when using the Maps API
function initMap() {
    // initialising the map's visual settings and theme
    google.maps.visualRefresh = true;
    // initial app settings
    var mapOptions = {
        center: new google.maps.LatLng(39.9078, 32.8252),
        zoom: 10,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    // retrieving element in DOM where map will be
    var mapElement = document.getElementById("map");
    // creating map on this element
    map = new google.maps.Map(mapElement, mapOptions);
} // end initMap()

function zoomToIstanbul() {
    var istanbulCenter = new google.maps.LatLng(41.0579. 29.0340);
    map.setCenter(istanbulCenter);
} // end zoomToIstanbul()

function disableDrag() {
    map.setOptions({
        draggable: false
    });
} // end disableDrag()

function setMaxZoom() {
    map.setOptions({
        maxZoom: 12
    });
} // end setMaxZoom()

function setMinZoom() {
    map.setOptions({
        minZoom: 10
    });
} // end setMinZoom()

function disableScroll() {
    map.setOptions({
        scrollwheel: false
    });
} // end disableScroll()

// this listens to button clicks from the user to change
// particular options and settings of the map
function buttonEvents() {
    // calls zoomToIstanbul()
    var buttonIstanbul = document.getElementById("zoom-to-istanbul");
    buttonIstanbul.addEventListener("click", function() {
        zoomToIstanbul();
    });

    // calls disableDrag()
    var buttonDisableDrag = document.getElementById("disable-drag");
        buttonDisableDrag.addEventListener("click", function() {
        disableDrag();
    });

    // calls setMaxZoom()
    var buttonMaxZoom = document.getElementById("max-zoom");
    buttonMaxZoom.addEventListener("click", function() {
        setMaxZoom();
    });

    // calls setMinZoom()
    var buttonMinZoom = document.getElementById("min-zoom");
    buttonMinZoom.addEventListener("click", function() {
        setMinZoom();
    });
} // end buttonEvents()

// this calls initMap when the page loads
google.maps.event.addDomListener(window, "load", initMap);
google.maps.event.addDomListener(window, "load", buttonEvents);

The issue here is that you are calling the events on page load. 这里的问题是您在页面加载时调用事件。 What you need to do is something like this: 您需要做的是这样的:

<script type="text/javascript">
    function initMap() {
        // Load map and set events here
    }
</script>

<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"></script>

This makes sure that the initMap function is called just after the Google Maps API is loaded. 这样可确保在加载Google Maps API之后initMap调用initMap函数。

Source: https://developers.google.com/maps/documentation/javascript/tutorial 来源: https//developers.google.com/maps/documentation/javascript/tutorial

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM