lundi 1 mai 2017

Google Map API keep view inside map issue

I want to prevent dragging map to blank area ( user can't drag map farther than 90 north or -90 south). My current code is.

   function prevent(maxLat){
      if(map.getCenter().lat()>maxLat){
        map.setCenter(new google.maps.LatLng(maxLat,map.getCenter().lng()));
      }
      else if(map.getCenter().lat()< -maxLat){
        map.setCenter(new google.maps.LatLng(-maxLat,map.getCenter().lng()));
      }
    }       

// on center_changed event
    google.maps.event.addListener(map, 'center_changed', function() {
    switch(map.getZoom()){
      case 2: prevent(47); break; // case zoom =2 set max center lat =47
      case 3: prevent(75); break; 
      case 4: prevent(81); break;
      case 5: prevent(83); break;
      case 6: prevent(84); break;
      case 7: prevent(84.5); break;
      default: prevent(84.5); break;  // zoom >7
          }
    });

It working ok but i got 1 issue. When zoom =2 , dragging map cause "white flash". It very quick but really annoying. Anyone got solution for that? or another code that work is also OK. THANK. PS: No such issue on maps.google.com but i dont know how they did it.




Aucun commentaire:

Enregistrer un commentaire