2014-09-28 57 views
-1

我有以下代碼,除了google.maps.event.addListener(標記,'click',function()。以外,我將包含所有代碼以供參考。我曾嘗試註釋掉的代碼。我要的是點擊標記時地圖放大。謝謝你的幫助。谷歌地圖點擊事件不起作用

$(document).ready(function() { 
    $("#map").css({ 
     height: 700, 
     width: 800 
    }); 
    var myLatLng = new google.maps.LatLng(46.053791, -118.3131256); 
    MYMAP.init('#map', myLatLng, 11); 

    $("#showmarkers").ready(function(e){ 
     MYMAP.placeMarkers('markers.xml'); 
    }); 
}); 

var MYMAP = { 
    map: null, 
    bounds: null 
} 

MYMAP.init = function(selector, latLng, zoom) { 
    var myOptions = { 
    zoom:zoom, 
    center: latLng, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    this.map = new google.maps.Map($(selector)[0], myOptions); 
    this.bounds = new google.maps.LatLngBounds(); 
} 

MYMAP.placeMarkers = function(filename) { 
     $.get(filename, function(xml){ 
     $(xml).find("marker").each(function(){ 
      var name = $(this).find('name').text(); 
      var address = $(this).find('address').text(); 

      // create a new LatLng point for the marker 
      var lat = $(this).find('lat').text(); 
      var lng = $(this).find('lng').text(); 
      var point = new google.maps.LatLng(parseFloat(lat),parseFloat(lng)); 

      // extend the bounds to include the new point 
      MYMAP.bounds.extend(point); 

      var marker = new google.maps.Marker({ 
       position: point, 
       map: MYMAP.map 
      }); 

      var infoWindow = new google.maps.InfoWindow(); 
      var html='<strong>'+name+'</strong.><br />'+address; 
      google.maps.event.addListener(marker, 'mouseover', function() { 
       infoWindow.setContent(html); 
       infoWindow.open(MYMAP.map, marker); 

      }); 
<!-- *************** here is the code I need help with **************** -->  
      google.maps.event.addListener(marker, 'click', function() { 
       <!-- attempt 1 --> 
       <!--map.setZoom(10); --> 
       <!--map.setCenter(marker.getPosition());--> 

       <!--attempt 2 --> 
       <!--mapZoom = map.getZoom();--> 
       <!--startLocation = event.point; -->: 

       <!--attempt 3 (with and without MYMAP--> 
       <!--MYMAP.map.setZoom(10); --> 
       <!--MYMAP.map.panTo(marker.position); --> 
    }); 

      google.maps.event.addListener(marker,'mouseout', function() { 
    infoWindow.close(); 
    }); 


      MYMAP.map.fitBounds(MYMAP.bounds); 
     }); 
    }); 
} 
+0

請提供一個[最小,完整,測試和可讀的示例](http://stackoverflow.com/help/mcve)(包括任何HTML/CSS /外部JavaScript需要)。 – geocodezip 2014-09-28 22:43:51

回答

0

我認爲最好的辦法是讓當前的縮放,增加它,和變焦設置爲新值,就像這樣:

google.maps.event.addListener(marker, 'click', function() { 
    MYMAP.map.setZoom(MYMAP.map.getZoom()+1); 
}); 

http://jsfiddle.net/OxyDesign/w26fL6f7/

是你想要的嗎?