javascript - 将参数传递给 JavaScript 中的 url 回调函数

标签 javascript google-maps callback

我有以下代码,当用户点击提交按钮时显示 map 。

目前函数 initialize() 不带任何参数, map 以固定的纬度和经度为中心。我希望能够将纬度和经度作为参数,以便 map 以这些参数为中心。

我已经有了纬度和经度,所以获取这些参数不是问题;我的问题是我不知道如何将它们传递给函数。

完整代码:

<!DOCTYPE html>
<html>
    <head>
      <link rel="stylesheet" type="text/css" href="stylesheet.css">
      <title>Simple Map</title>
      <meta name="viewport" content="initial-scale=1.0">
      <meta charset="utf-8">
    </head>
<body>
    <div id="map">

        <button type="button" onclick="loadScript()">submit</button>

    </div>


    <script>

        function initialize() {
            var latlng = new google.maps.LatLng(-34.397, 150.644);
            var myOptions = {
                zoom: 10,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            var map = new google.maps.Map(document.getElementById("map"),
        myOptions);
        }


        function loadScript() {
            var myKey = "myAPIKey";
            var script = document.createElement('script');
            script.type = 'text/javascript';
            script.src = "https://maps.googleapis.com/maps/api/js?key=" + myKey + "&sensor=false&callback=initialize";
            document.body.appendChild(script);
        }

    </script>

</body>

单独的 JavaScript:

        function initialize() {
            var latlng = new google.maps.LatLng(-34.397, 150.644);
            var myOptions = {
                zoom: 10,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            var map = new google.maps.Map(document.getElementById("map"),
        myOptions);
        }


        function loadScript() {
            var myKey = "myAPIKey";
            var script = document.createElement('script');
            script.type = 'text/javascript';
            script.src = "https://maps.googleapis.com/maps/api/js?key=" + myKey + "&sensor=false&callback=initialize";
            document.body.appendChild(script);
        }

最佳答案

只需在脚本之前初始化变量。 在“loadScript”函数中更新变量并在“初始化”函数中使用它们。对我来说,它有效。

<script>
    var string = "";   // initialize variables here. You need var lat and var lng 

    function initialize() {
        var latlng = new google.maps.LatLng(-34.397, 150.644);
        console.log(string);  // just checking, if it does work in "initialize" function
        var myOptions = { 
            zoom: 10,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map"),
    myOptions);
    }


    function loadScript() {
        var myKey = "myAPIKey";
        var script = document.createElement('script');
        script.type = 'text/javascript';
        string = "hey it works";   //////////// here take the coordinates
        script.src = "https://maps.googleapis.com/maps/api/js?&sensor=false&callback=initialize";
        document.body.appendChild(script);
    }

</script>

此外,我认为稍后可以使用以下命令更改位置:

map.setCenter(new google.maps.LatLng( 45, 19 ) );

希望对你有帮助

关于javascript - 将参数传递给 JavaScript 中的 url 回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36063089/

相关文章:

java - 如何将回调函数从Java传递到Kotlin类

javascript - 如何将现有的回调 API 转换为 Promise?

javascript - 如何修改文本输入框以使其仅接受某些字符?

javascript - angularjs搜索过滤器问题

javascript - React Native 将状态设置为 true 一秒钟

java - 结合 Google Maps API 和 Google Maps Data API

google-maps - 谷歌地图认为我正在发送自动请求

javascript - 如何简洁下面的jquery代码片段

javascript - geoXML3,如何加载文件夹中的所有KML文件?

node.js - 有没有办法克服 Node.js 中的回调 if(err) 样板?