将数据传递给另一个功能 [英] Passing data to another function

查看:98
本文介绍了将数据传递给另一个功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Json抓取数据并在queryLat/queryLng中包含数组,然后创建了另一个函数initMap还将其绑定到Google脚本.但是我很难及时将queryLat和queryLng传递到initMap中.弹出"queryLat未定义".我如何将那些传递给initMap.

I scraped data from Json and containing arrays in queryLat/queryLng after that I create another function initMap also bind it to google script. But I having hard to time passing queryLat and queryLng into initMap. "queryLat is not defined" pops up. How I can pass those to initMap.

    var queryLat = [];
    var queryLng = [];

    @foreach($estates as $est)
        var result = $.getJSON({
                url: 'https://maps.googleapis.com/maps/api/geocode/json?address={{$est->address}}&key={{env('GOOGLE_MAPS_API')}}'
            });

        result.done(function(data) {

            queryLat = data.results[0].geometry.location.lat;
            queryLng = data.results[0].geometry.location.lng;
        });
    @endforeach


    function initMap()
    {
        var options =
            {
                zoom : 10,
                center : {lat:34.652500, lng:135.506302}
            }

        var map  = new
            google.maps.Map(document.getElementById("map"), options);


        for (var i = 0; i < queryLat.length; i++)
        {
            var newMarker = new google.maps.Marker
            ({
                position: {lat: queryLat[i], lng: queryLng[i]} ,
                map: map
            });
        }

    }

推荐答案

对于多个标记,如果要全局定义数组,则必须将lat和long值推送到数组中,还需要更新marker变量以显示不同的标记..希望它能帮助您获得多个标记.

For multiple markers if you are defining arrays globally then you have to push your lat and long values in array and also need to update the marker variable to display diferent markers.. Hope it helps you to get the multiple markers.

var queryLat = []; var queryLng = [];

var queryLat = []; var queryLng = [];

@foreach($estates as $est)
    var result = $.getJSON({
            url: 'https://maps.googleapis.com/maps/api/geocode/json?address={{$est->address}}&key={{env('GOOGLE_MAPS_API')}}'
        });

    result.done(function(data) {

        queryLat.push(data.results[0].geometry.location.lat);
        queryLng.push(data.results[0].geometry.location.lng);
    });
@endforeach

function initMap()
{
    var options =
        {
            zoom : 10,
            center : {lat:34.652500, lng:135.506302}
        }

    var map  = new
        google.maps.Map(document.getElementById("map"), options);


    for (var i = 0; i < queryLat.length; i++)
    {
        var new_marker_str = "newMarker"+i;
        new_marker_str = new google.maps.Marker
        ({
            position: {lat: queryLat[i], lng: queryLng[i]} ,
            map: map
        });
    }

}

这篇关于将数据传递给另一个功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆