谷歌地图api类型没有选择形式submition

时间:2017-10-16 05:31:56

标签: javascript forms google-maps google-maps-api-2

使用google map api时,一切正常。但它允许用户在基础上输入选项列表框将显示在下方。但是如果没有选择,则允许用户在键入的关键字上提交表单。如何让用户限制选择一个选项并提交,如果他没有选择任何选项,则错误显示他选择一个地址。

var autocomplete_mobile;
var autocomplete;
window.onload = function initialize_gm(){
    autocomplete = new google.maps.places.Autocomplete(
    /** @type {!HTMLInputElement} */
    (document.getElementById('autocomplete')), {
        type: ['address'],
        componentRestrictions: {
            country: "us"
        },
        strictBounds: true
    }
    /* {type: ['establishment'],componentRestrictions: {country: "us"}} */
    );
    var autocompleteLsr = google.maps.event.addListener(autocomplete, 'place_changed', function() {});
    if (navigator.geolocation) {
        var startPos;
        var geoOptions = {
            enableHighAccuracy: true,
            timeout: 30000
        }
        var geoSuccess = function(position) {
            google.maps.event.removeListener(autocompleteLsr);
            google.maps.event.clearInstanceListeners(autocomplete);
            jQuery(".pac-container").remove();
            startPos = position;
            var latitude = startPos.coords.latitude;
            var longitude = startPos.coords.longitude;
            var center = new google.maps.LatLng(latitude, longitude);
            var circle = new google.maps.Circle({
                center: center,
                radius: 50000
            });
            autocomplete = new google.maps.places.Autocomplete(
                /** @type {!HTMLInputElement} */
                (document.getElementById('autocomplete')), {
                    type: ['address']
                });
            autocomplete.setBounds(circle.getBounds());
        };
        var geoError = function(error) {
        if (error.code == 1) {
            //  bootbox.alert('You\'ve decided not to share your position.');
            } else if (error.code == 2) {
                //bootbox.alert('The network is down or the positioning service can\'t be reached.');
            } else if (error.code == 3) {
                //bootbox.alert('The attempt timed out.');
            } else {
                //bootbox.alert('Location detection failed.');
            }
        };
     }
}

0 个答案:

没有答案