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

问题描述 投票:5回答:3

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

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

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

完整代码:

<!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);
        }
javascript google-maps callback
3个回答
3
投票

只需在脚本之前启动变量。在“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 ) );

我希望它有所帮助


1
投票

您可以使用Array.prototype.bind将参数预加载到函数中。我们可以使用它来存储参数,即使函数没有用any调用:

// Modify initialize to accept lat/long
function initialize(lat, long) {
    /* etc */
}

// Pre-load our arguments
var gmapsInitialize = initialize.bind(null, -34.397, 150.644);

现在你可以将回调设置为gmapsInitialize

另一种选择是创建一个更高阶的函数,该函数在使用lat / long参数调用时返回您的回调:

function initializeFactory(lat, long) {
    return function () {
       /* function body of your original initialize here; make use of lat/long arguments */
    }
}

你可以用这种方式创建不同版本的initialize

var gmapsInitialize = initializeFactory(-34.397, 150.644);

1
投票

我通过创建一个额外的函数解决了传递参数到回调:

var id = $($element).attr("id");;
var callBackName = id+"_callback";
window[callBackName] = new Function("googleMapCallback(\""+id+"\")");
script.src = "https://maps.googleapis.com/maps/api/js?&sensor=false&callback=window."+callBackName;

这样你就可以拥有一个包含所有内容的元素,然后你就可以回到它了。

© www.soinside.com 2019 - 2024. All rights reserved.