Display world map with no repeats

后端 未结 3 816
自闭症患者
自闭症患者 2020-11-29 10:31

I\'m currently using the Google Maps API for the first time. Essentially I wish to have the map zoomed out so that the whole world is displayed with no overlap (e.g. bits of

3条回答
  •  -上瘾入骨i
    2020-11-29 11:23

    Here's a function worldViewFit I like to use:

    function initMap() {
    	var mapOptions = {
    		center: new google.maps.LatLng(0, 0),
    		zoom: 1,
    		minZoom: 1
    	};
    	map = new google.maps.Map(document.getElementById('officeMap'), mapOptions);
    	google.maps.event.addListenerOnce(map, 'idle', function() {
    		//Map is ready
    		worldViewFit(map);
    	});
    }
    
    function worldViewFit(mapObj) {
    	var worldBounds = new google.maps.LatLngBounds(
    		new google.maps.LatLng(70.4043,-143.5291),	//Top-left
    		new google.maps.LatLng(-46.11251, 163.4288)	 //Bottom-right
    	);
    	mapObj.fitBounds(worldBounds, 0);
    	var actualBounds = mapObj.getBounds();
    	if(actualBounds.getSouthWest().lng() == -180 && actualBounds.getNorthEast().lng() == 180) {
    		mapObj.setZoom(mapObj.getZoom()+1);
    	}
    }
    
    google.maps.event.addDomListener(window, 'load', initMap);
    html,
    body {
      height: 100%;
      margin: 0;
      padding: 0;
    }
    
    #officeMap {
      height: 512px;
      width: 512px;
    }
    
    

提交回复
热议问题