How to change Google Map Center by clicking a button

前端 未结 2 936
野性不改
野性不改 2020-12-24 15:40

I am using Google Map API v3 and jQuery 1.11.0.

I have a Google Map in the following div:

2条回答
  •  清酒与你
    2020-12-24 15:51

    Here is the code for ur problem:

    var map;
    function initialize()
    {
      map = new google.maps.Map(document.getElementById('map-canvas'), {
        center: new google.maps.LatLng(48.1293954,12.556663),//Setting Initial Position
        zoom: 10
      });
    }
    
    function newLocation(newLat,newLng)
    {
    	map.setCenter({
    		lat : newLat,
    		lng : newLng
    	});
    }
    
    google.maps.event.addDomListener(window, 'load', initialize);
    
    //Setting Location with jQuery
    $(document).ready(function ()
    {
        $("#1").on('click', function ()
        {
    	  newLocation(48.1293954,11.556663);
    	});
      
    	$("#2").on('click', function ()
        {
    	  newLocation(40.7033127,-73.979681);
    	});
      
        $("#3").on('click', function ()
        {
    	  newLocation(55.749792,37.632495);
    	});
    });
    html, body, #map-canvas {
            height: 100%;
            margin: 0px;
            padding: 0px
          }
    
    
    
    
    
    
    

    Live demo is here if u need more :).

提交回复
热议问题