mauzzamali
mauzzamali

Reputation: 73

How to limit gmap search autocomplete to limited country

I am developing a site where i am implementing Gmap with to and from facility. Everything is working good but right now i just wanted it's auto complete to limit to one specific country. Here is My code..

$(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();
 }

Upvotes: 0

Views: 245

Answers (1)

Android Enthusiast
Android Enthusiast

Reputation: 4950

You can use Google Maps JavaScript API Places library. The function in the Google Places JavaScript library enable your application to search for places contained within a defined area, such as the bounds of a map or around a fixed point.

Here's a sample code how to restrict by country:

function initialize() {

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

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

Upvotes: 1

Related Questions