如何触发Google的place_changed事件会在页面加载时自动完成

时间:2017-04-17 14:15:14

标签: jquery google-maps google-maps-api-3

我正在使用google map api,我在页面加载时从查询字符串填充位置,但是打开选择列表以手动选择它(屏幕:http://prnt.sc/exg338)。 我只想在设置message_city的值(来自查询字符串)后触发pageload上的place_changed事件,以便其他函数可以调用(手动选择位置后调用)

geocoder = new google.maps.Geocoder();

cityac = new google.maps.places.Autocomplete( (document.getElementById('message_city')), { types: ['geocode'], componentRestrictions: {country:"GB"} });

google.maps.event.addListener(cityac, 'place_changed', function() { city_select(); });

function city_select() {
        var city = jQuery("#message_city").val();

        var place = cityac.getPlace();
        if ((place.address_components[1].short_name == 'London') || (jQuery('#message_city').val() == 'London, United Kingdom')) {
            jQuery('#result_info').val('<br />London Not Covered ');
            jQuery('#result').val('no');
        } else {
            geocoder.geocode({'address': city}, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    var search = results[0].geometry.location;
                    var mindist = parseInt(10000);
                    var closest = 0;
                    var town = '';

                    for (var i = 0; i < locations.length; i++) {
                        var exp = locations[i].split('#');
                        var loc = new google.maps.LatLng(exp[1], exp[2])
                        var distance = calcDistance(loc, search);
                        var miles = parseInt((distance * 0.621371192).toFixed(2));

                        if (mindist > miles) {
                            mindist = miles;
                            closest = i;
                            nohour = exp[3];
                            town = exp[0];
                            radius = exp[5];

                            if (radius == '') {
                                radius = 30;
                            }
                            else {
                                radius = parseInt(radius);
                            }
                        }

                    }
                    if (mindist <= radius) {
                        jQuery('#result_info').val('Closest Location is ' + town + ' | ' + mindist + ' miles');
                        jQuery('#result_city').val(town);
                        jQuery('#result').val('yes');

                        if (nohour == '1') {
                            //alert('This location has a minimum 2 hour time');
                            jQuery("#serviceHrs option[value='1']").remove();
                            jQuery("#serviceHrs").parent().after("<div class='infomsg' style='color:#ff0099;margin-bottom: 0;'>This location has a minimum of 2 hours.</div>");
                        }
                        else if (nohour == '0') {
                            jQuery(".infomsg").remove();
                            if (jQuery("#serviceHrs option:contains('1')").length == 0) {
                                jQuery("#serviceHrs").prepend(new Option("1 Hour", "1"));
                            }
                        }
                    } else {
                        jQuery('#result_info').val('<br />Closest Location is ' + town + ' | ' + mindist + ' miles, outside the ' + radius + ' mile zone of any current.');
                        jQuery('#result').val('no');
                    }
                }
            })
        }
    }

我正在尝试下面的代码,但它没有正常工作。

google.maps.event.trigger(cityac, 'place_changed');

1 个答案:

答案 0 :(得分:0)

我不确定我是否完全理解您的需求,但如果您想触发某个事件,则可以使用google.maps.event.trigger(autocomplete, 'place_changed');。这应该够了吧。

如果有帮助,请告诉我。