Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.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 GoogleMapAddListener问题_Javascript_Jquery_Google Maps - Fatal编程技术网

Javascript GoogleMapAddListener问题

Javascript GoogleMapAddListener问题,javascript,jquery,google-maps,Javascript,Jquery,Google Maps,我已经编写了以下代码。。在位置更改时激发Add Listener事件,但我想在页面加载时激发该事件。。这是从谷歌复制过来的,我做了一些修改 function _initMap(latlng){ // google map $map = $(".lwizard-step1-map"); _latlng = latlng || new google.maps.LatLng(41.3833333,2.1833333); if (!_STEP1_LOCATION_MAP)

我已经编写了以下代码。。在位置更改时激发Add Listener事件,但我想在页面加载时激发该事件。。这是从谷歌复制过来的,我做了一些修改

function _initMap(latlng){
    // google map
    $map = $(".lwizard-step1-map");
    _latlng = latlng || new google.maps.LatLng(41.3833333,2.1833333);
    if (!_STEP1_LOCATION_MAP) {
        _STEP1_LOCATION_MAP = new google.maps.Map($map[0],{
            center: _latlng,
            zoom:11,
            mapTypeId:google.maps.MapTypeId.ROADMAP,
            streetViewControl:false,
            scrollwheel:false
        });
    } else {
        _STEP1_LOCATION_MAP.setCenter(_latlng);
    }

    if (!_STEP1_LOCATION_MARKER) {
        _STEP1_LOCATION_MARKER = new google.maps.Marker({
            position: _latlng,
            map: _STEP1_LOCATION_MAP,
            draggable: true
        });

        google.maps.event.addListener(_STEP1_LOCATION_MARKER, "dragend", function(event) {
            var lat = event.latLng.lat(); 
            var lng = event.latLng.lng(); 
            $("#lwizard-step1-location-autocomplete").val('');
            STEP1_PLACE.latLng = [lat,lng];
            STEP1_PLACE.address = null;
            $.ajax({
                url: 'http://maps.googleapis.com/maps/api/geocode/json?latlng='+lat+','+lng+'&sensor=false',
                success: function(response){
                    if ((response) && (response.results) && (response.results.length)){
                        $(".lwizard-step1-chosen-address").parent().show();

                        var $address = $(".lwizard-step1-chosen-address");
                        $address.text(response.results[0].formatted_address);
                        $("#lwizard-step1-adjust-onmap").data("location", response.results[0].geometry.location);
                        STEP1_PLACE.address = nc.utils.convertGoogleAddressComponents(response.results[0].address_components);
                    }
                }
            })
        }); 
    } else {
        _STEP1_LOCATION_MARKER.setPosition(_latlng);
    }
}

// autocomplete
$input = $("#lwizard-step1-location-autocomplete");


_STEP1_LOCATION_GA = new google.maps.places.Autocomplete($input[0],{ types: [] });  
google.maps.event.addListener(_STEP1_LOCATION_GA, 'place_changed', function () {

    var place = _STEP1_LOCATION_GA.getPlace();
    console.log(place)
    var $where = $(".lwizard-step1-chosen-address");
    var found = ((place) && (place.formatted_address) && (place.geometry) && (place.id));
    if (place && found) {
        $("#lwizard-step1-adjust-onmap").data("location", place.geometry.location);
        STEP1_PLACE.address = nc.utils.convertGoogleAddressComponents(place.address_components);
        STEP1_PLACE.latLng = [place.geometry.location.lat(),place.geometry.location.lng()];
        STEP1_PLACE.location = place.geometry.location;
        $where.parent().show();
        $where.text(place.formatted_address);
        $("#lwizard-step1-right-1").show();
        $("#lwizard-step1-right-2").hide();
        _initMap(place.geometry.location);
    } else {
        $where.parent().hide();
        $("#lwizard-step1-right-1").show();
        $("#lwizard-step1-right-2").hide();
        _initMap();
    }
});
我有一个名为lwizard-step1-location-autocomplete的文本框,它在页面加载时显示名为SessionLocation的会话的值。 但我还想在页面加载时显示textbox中指定位置的地图

但问题是,addlistener仅在文本框更改时触发表示位置更改事件。
请。提出一些建议或新的方法

您可以在加载时调用change事件,然后触发它

$('#lwizard-step1-location-autocomplete').trigger("change");
或使用js

document.getElementById('lwizard-step1-location-autocomplete').fireEvent("onchange");

您可以在加载时调用change事件,然后触发它

$('#lwizard-step1-location-autocomplete').trigger("change");
或使用js

document.getElementById('lwizard-step1-location-autocomplete').fireEvent("onchange");