如何将gmap搜索自动填充限制为有限的国家/地区

时间:2016-04-21 13:12:51

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

我正在开发一个网站,我正在与设施实施Gmap。一切都运作良好,但现在我只是希望它自动完成限制到一个特定的国家。这是我的代码..

$(document).ready(function() {
initialize_direction_map();
setDestination();
$("#start").autocomplete({ 
    source: function(request, response) {
        geocoder.geocode( {'address': request.term }, function(results, status) {
            response($.map(results, function(item) {
                return {
                    label:  item.formatted_address,
                    value: item.formatted_address,
                    latitude: item.geometry.location.lat(),
                    longitude: item.geometry.location.lng()
                }
            }));
        })
    },
    //This bit is executed upon selection of an address
    select: function(event, ui) {
        $("#s_latitude").val(ui.item.latitude);
        $("#s_longitude").val(ui.item.longitude);
        var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude);
        map.setCenter(location);
        map.setZoom(12);
        var marker = new google.maps.Marker({
          position: location,
          map: map
        });
        document.getElementById('direction_box').style.display='block';
        calcRoute();
    }
});

$("#end").autocomplete({
    //This bit uses the geocoder to fetch address values
    source: function(request, response) {

        geocoder.geocode( {'address': request.term }, function(results, status) {
            response($.map(results, function(item) {
                return {
                    label:  item.formatted_address,
                    value: item.formatted_address,
                    latitude: item.geometry.location.lat(),
                    longitude: item.geometry.location.lng()
                }
            }));
        })
    },
    //This bit is executed upon selection of an address
    select: function(event, ui) {
        $("#e_latitude").val(ui.item.latitude);
        $("#e_longitude").val(ui.item.longitude);
        var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude);

        calcRoute();
        $("table.adp-directions").css("width","300px");
    }
   });
});

function initialize_direction_map(){
//MAP
directionsDisplay = new google.maps.DirectionsRenderer();
var latlng = new google.maps.LatLng(20.5937,78.9629);
var options = {
    zoom: 5,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
};

map = new google.maps.Map(document.getElementById('map-canvas'), options);
directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById('directions-panel'));

//GEOCODER
geocoder = new google.maps.Geocoder();
 }

1 个答案:

答案 0 :(得分:1)

您可以使用Google Maps JavaScript API Places库。 Google Places JavaScript库中的功能使您的应用程序可以搜索定义区域中包含的位置,例如地图边界或固定点周围。

以下是如何按国家/地区限制的示例代码:

function initialize() {

var options = {
types: ['(cities)'],
componentRestrictions: {country: "us"}
};

var input = document.getElementById('searchTextField');
var autocomplete = new google.maps.places.Autocomplete(input, options);
}
相关问题