2014-09-14 2 views
0

Я попытался написать код для автозаполнения адресов с помощью google map api.Как ограничить карту google api автозаполнением jQuery в стране

Сначала я инициализировал маркер карты и геокодер.

var geocoder; 
var map; 
var marker; 

function initialize(){ 
//MAP 
    var latlng = new google.maps.LatLng(34.016827, -6.835604); 
    var options = { 
    zoom: 15, 
    center: latlng, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 

    map = new google.maps.Map(document.getElementById("map_canvas"), options); 

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

    marker = new google.maps.Marker({ 
    map: map, 
    draggable: true 
    }); 

} 

Тогда автозаполнения JQuery Я

$(document).ready(function() { 

    initialize(); 

    $(function() { 
    $("#address").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) { 
     $("#latitude").val(ui.item.latitude); 
     $("#longitude").val(ui.item.longitude); 
     var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude); 
     marker.setPosition(location); 
     map.setCenter(location); 
     } 
    }); 
    }); 

Теперь я хотел бы ограничить автозаполнение в одной стране, я сделал некоторые поиск, но я нашел this

Но когда я попытался делайте то же самое в моем коде, который я знал.

Я надеюсь, что я найду помощь и спасибо

+0

В чем проблема? – hennes

ответ

0

Вы должны использовать Component Filtering полностью ограничить результаты поиска

Итак, в соответствии с этим, измените свой код следующим образом:

$(document).ready(function() { 

    initialize(); 

    $(function() { 
    $("#address").autocomplete({ 
    //This bit uses the geocoder to fetch address values 
    source: function(request, response) { 
    geocoder.geocode( 
    { 
     'address': request.term, 
     'componentRestrictions':{'country':'IT'} //any country code... 
    }, 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) { 
    $("#latitude").val(ui.item.latitude); 
    $("#longitude").val(ui.item.longitude); 
    var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude); 
    marker.setPosition(location); 
    map.setCenter(location); 
    } 
    }); 
}); 
0

я нашел Как ограничить исследование Это только я сростить Adresse с городом и страной

geocoderar.geocode({'address': request.term + ' Rabat, MA' } 

в моем случае Рабат является город и МА страна есть код для каждой страны это link for country code

Смежные вопросы