Advertisement
dammarpol

InitializeMap with Overlay ver. 2

Sep 2nd, 2014
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var overlay;
  2. USGSOverlay.prototype = new google.maps.OverlayView();
  3. var info = new google.maps.InfoWindow();
  4.  
  5. function initialize() {
  6.   var mapOptions = {
  7.     zoom: 0,
  8. disableDefaultUI: true,
  9. streetViewControl: false,
  10. navigationControl: false,
  11. zoomControl: false,
  12. scaleControl: false,
  13. scrollwheel: false,
  14. disableDoubleClickZoom: true,
  15.     center: new google.maps.LatLng(28.70, -127.50),
  16.     mapTypeId: google.maps.MapTypeId.ROADMAP
  17.    
  18.   };
  19.  
  20.   var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
  21.  
  22. var marker = new google.maps.Marker({
  23.     position: map.getCenter(),
  24.     map: map
  25.   });
  26.  
  27.   var swBound = new google.maps.LatLng(62.281819, -150.287132);
  28.   var neBound = new google.maps.LatLng(62.400471, -150.005608);
  29.   var bounds = new google.maps.LatLngBounds(swBound, neBound);
  30.     var allowedBounds = new google.maps.LatLngBounds(
  31.                   new google.maps.LatLng(28.70, -127.50),
  32.                   new google.maps.LatLng(48.85, -55.90)
  33.                 );
  34.                
  35.                 var boundLimits = {
  36.                     maxLat : allowedBounds.getNorthEast().lat(),
  37.                     maxLng : allowedBounds.getNorthEast().lng(),
  38.                     minLat : allowedBounds.getSouthWest().lat(),
  39.                     minLng : allowedBounds.getSouthWest().lng()
  40.                 };
  41.  
  42.                 var lastValidCenter = map.getCenter();
  43.                 var newLat, newLng;
  44.                 google.maps.event.addListener(map, 'center_changed', function() {
  45.                     center = map.getCenter();
  46.                     if (allowedBounds.contains(center)) {
  47.                        
  48.                         lastValidCenter = map.getCenter();
  49.                         return;
  50.                     }
  51.                     newLat = lastValidCenter.lat();
  52.                     newLng = lastValidCenter.lng();
  53.                     if(center.lng() > boundLimits.minLng && center.lng() < boundLimits.maxLng){
  54.                         newLng = center.lng();
  55.                     }
  56.                     if(center.lat() > boundLimits.minLat && center.lat() < boundLimits.maxLat){
  57.                         newLat = center.lat();
  58.                     }
  59.                     map.panTo(new google.maps.LatLng(newLat, newLng));
  60.                    
  61.                 });
  62.  
  63.   var srcImage = 'assets/pics/floorImage.jpg';
  64.  
  65.   overlay = new USGSOverlay(bounds, srcImage, map);
  66. }
  67.  
  68. function USGSOverlay(bounds, image, map) {
  69.  
  70.       this.bounds_ = bounds;
  71.       this.image_ = image;
  72.       this.map_ = map;
  73.  
  74.       this.div_ = null;
  75.  
  76.       this.setMap(map);
  77.     }
  78.  
  79.     USGSOverlay.prototype.onAdd = function() {
  80.  
  81.       var div = document.createElement('div');
  82.       div.style.borderStyle = '0';
  83.       div.style.borderWidth = '100%';
  84.       div.style.position = 'b';
  85.  
  86.       var img = document.createElement('img');
  87.       img.src = this.image_;
  88.       img.style.width = '800px';
  89.       img.style.height = '500px';
  90.       img.style.position = 'absolute';
  91.       div.appendChild(img);
  92.  
  93.       this.div_ = div;
  94.  
  95.       var panes = this.getPanes();
  96.       panes.overlayLayer.appendChild(div);
  97.     };
  98.  
  99.     USGSOverlay.prototype.draw = function() {
  100.  
  101.       var overlayProjection = this.getProjection();
  102.  
  103.       var sw = overlayProjection.fromLatLngToDivPixel(this.bounds_.getSouthWest());
  104.       var ne = overlayProjection.fromLatLngToDivPixel(this.bounds_.getNorthEast());
  105.  
  106.       var div = this.div_;
  107.       div.style.left = sw.x + '10';
  108.       div.style.top = ne.y + '10';
  109.       div.style.width = (ne.x - sw.x) + '';
  110.       div.style.height = (sw.y - ne.y) + '50';
  111.     };
  112.  
  113.     USGSOverlay.prototype.onRemove = function() {
  114.       this.div_.parentNode.removeChild(this.div_);
  115.       this.div_ = null;
  116.     };
  117.    
  118.  
  119.     google.maps.event.addDomListener(window, 'load', initialize);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement