Javascript 谷歌地图API-地理编码国家然后点击缩放到国家边界

Javascript 谷歌地图API-地理编码国家然后点击缩放到国家边界,javascript,google-maps,google-maps-api-3,geocoding,Javascript,Google Maps,Google Maps Api 3,Geocoding,我有以下代码,它使用地理编码API将国家标记放置在谷歌地图上 这很好,但我现在想放大到该国的边界,当我点击标记,但我完全卡住了 有什么想法吗 Javascript: var country = []; var bounds; var geocoder; var marker; $(document).ready(function () { $("select[id*='countryList']").find("option").each(function () { c

我有以下代码,它使用地理编码API将国家标记放置在谷歌地图上

这很好,但我现在想放大到该国的边界,当我点击标记,但我完全卡住了

有什么想法吗

Javascript:

var country = [];
var bounds;
var geocoder;
var marker;

$(document).ready(function () {
    $("select[id*='countryList']").find("option").each(function () {
        country.push([$(this).val(), $(this).text()]);
    });
    initialize();
});

function getCountry(country, countryTotal, theMap) {
    geocoder.geocode( { 'address': country }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var marker = new google.maps.Marker({
                map: theMap,
                title: ""+results[0].geometry.viewport,
                position: new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng())
            });
            marker.setIcon('http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld='+countryTotal+'|B22222|FFFFFF') ;
            google.maps.event.addDomListener(marker, 'click', function () {
                // Zoom into the bounds of the country that has been clicked on...
            });

            bounds.extend(new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng()));
            theMap.fitBounds(bounds);

        } else if(status === google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
            setTimeout(function() {
                getCountry(country, countryTotal, theMap)
            }, 10);
        }
    });
}

function initialize() {
    bounds = new google.maps.LatLngBounds();
    geocoder = new google.maps.Geocoder();
    var mapOptions = {
        scrollwheel: false,
    }
    var map = new google.maps.Map(document.getElementById('map'), mapOptions);
    for (var i = 0; i < country.length; i++) {
        var countries = country[i];
        getCountry(countries[1], countries[0], map);
    }
}
var国家=[];
var界;
var地理编码器;
var标记;
$(文档).ready(函数(){
$(“选择[id*='countryList'])。查找(“选项”)。每个(函数(){
country.push([$(this.val(),$(this.text());
});
初始化();
});
函数getCountry(国家/地区、国家/地区合计、地图){
geocoder.geocode({'address':country},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
var marker=new google.maps.marker({
地图:主题地图,
标题:“+结果[0]。geometry.viewport,
位置:新建google.maps.LatLng(结果[0].geometry.location.lat(),结果[0].geometry.location.lng())
});
marker.setIcon('http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=“+countryTotal+”| B22222 | FFFFFF');
google.maps.event.addDomListener(标记,'click',函数(){
//放大已单击国家的边界。。。
});
extend(新的google.maps.LatLng(结果[0].geometry.location.lat(),结果[0].geometry.location.lng());
theMap.fitBounds(边界);
}else if(status==google.maps.GeocoderStatus.OVER\u QUERY\u LIMIT){
setTimeout(函数(){
getCountry(国家、国家总计、地图)
}, 10);
}
});
}
函数初始化(){
bounds=新的google.maps.LatLngBounds();
geocoder=新的google.maps.geocoder();
变量映射选项={
滚轮:错误,
}
var map=new google.maps.map(document.getElementById('map'),mapOptions);
对于(变量i=0;i
HTML:


奥地利
巴利阿里群岛
加那利群岛
法国
意大利
马德拉
葡萄牙
西班牙
土耳其
英国

地理编码器结果还返回包含边界的地址的几何图形。使用边界作为映射的
fitBounds
-方法的参数:

google.maps.event.addDomListener(marker, 'click', function () {
   this.getMap().fitBounds(results[0].geometry.bounds)
});

geocoder结果还返回包含边界的地址的几何体。使用边界作为映射的
fitBounds
-方法的参数:

google.maps.event.addDomListener(marker, 'click', function () {
   this.getMap().fitBounds(results[0].geometry.bounds)
});