Google地图未在搜索按钮点击时初始化

时间:2014-08-09 20:20:32

标签: jquery google-maps google-maps-api-3 google-maps-markers google-maps-api-2

地图正在加载查找作为默认页面加载,但如果我正在尝试在搜索框事件上加载地图。它不起作用。请查看以下代码

         var address = 'Dubai';
         var neighborhoods = [
           new google.maps.LatLng(25.23247465817403, 55.30191340351564),
           new google.maps.LatLng(25.244586082480332, 55.29822268391115),
           new google.maps.LatLng(25.230844181976337, 55.32225527668459),
           new google.maps.LatLng(25.224787936110832, 55.28526224995119)
         ];
         var markers = [];
         var iterator = 0;
         var map;
         var geocoder = new google.maps.Geocoder();
         function initialize() {
         markers = [];
         iterator = 0;
             var mapOptions = {
                 zoom: 12,
                 //center: berlin
             };
             map = new google.maps.Map(document.getElementById('mapCanvas'),
                     mapOptions);
             geocoder.geocode({
                 'address': address
             },
             function (results, status) {
                 if (status == google.maps.GeocoderStatus.OK) {
                     map.setCenter(results[0].geometry.location);
                 }
             });
            drop();
         }

         function drop() {
             for (var i = 0; i < neighborhoods.length; i++) {
                 setTimeout(function () {
                     addMarker();
                 }, i * 200);
             }
         }
function addMarker() {
     var image = 'img/flagred.png';
     var marker = new google.maps.Marker({
         position: neighborhoods[iterator],
         map: map,
         icon: image,
         title:"Click Here to Add this Property",
         draggable: false,
         animation: google.maps.Animation.DROP
     });
     markers.push(marker);
     google.maps.event.addListener(marker, 'click', function() {
         // your magic goes here
         alert(this.position);
     });
     iterator++;
 }

按钮点击事件

    $("#lbl_save").click(function () {
        var city = $('#txt_srch_city').val();
        var region = $('#txt_srch_region').val();
        if (city != "" && region != "") {
            var merge = city + "," + region;
            address = merge;
            initialize();
        }
        else {
            alert("Please enter valid location!!");
        }
    });

1 个答案:

答案 0 :(得分:0)

在按钮是DOM的一部分(在页面onload事件上)之后,您需要调用代码来设置按钮单击侦听器:

// one option.  You can use the JQuery equivalent also.
google.maps.event.addDomListener(window, 'load', function(){
         $("#lbl_save").click(function () {
             var city = $('#txt_srch_city').val();
             var region = $('#txt_srch_region').val();
             if (city != "" && region != "") {
                 var merge = city + "," + region;
                 address = merge;
                 initialize();
             } else {
                 alert("Please enter valid location!!");
             }
         });
});

working fiddle