2013-11-26 168 views
-1

lon和lat發送正確,但仍然不顯示標記。谷歌地圖的經緯度範圍

$(function() { 
     var map; 
     var markers = new Array(); 
     var mapOptions = { 
      center: new google.maps.LatLng(46.4750370, 7.2843), 
      zoom: 7, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 
     var map = new google.maps.Map(document.getElementById("map"), mapOptions); 


     function initialize() { 
      // var bounds = new google.maps.LatLngBounds(); 
      google.maps.event.addListener(map, 'zoom_changed', onClickCallback); //listen to zoom event and call function to reload markers 
      google.maps.event.addListener(map, 'drag', onClickCallback); //listen to drag/pan event and call function to reload markers 

      google.maps.event.addListener(map, 'tilesloaded', function() { 
       onClickCallback(map); 
      }); 
     } 


     function onClickCallback(map) { 
      var bounds = map.getBounds(); 

      $.getJSON('http://skiweather.eu/gmap3/markers/index.php', { 
       swLat: bounds.getSouthWest().lat(), 
       swLon: bounds.getSouthWest().lng(), 
       neLat: bounds.getNorthEast().lat(), 
       neLon: bounds.getNorthEast().lng() 
      }, function (data) { 
       $.each(data.markers, function (i, marker) { 
        $('#map').gmap('addMarker', { 
         'position': new google.maps.LatLng(marker.latitude, marker.longitude), 
         'bounds': true 
        }).click(function() { 
         $('#map').gmap('openInfoWindow', { 
          'content': marker.content 
         }, this); 
        }); 
       }); 
      }); 
     } 

     google.maps.event.addDomListener(window, 'load', initialize); 

回答

0

問題是你只是創建一個空的LatLngBounds對象,因此它爲什麼默認爲1,-1,180,-180。你想了解你的地圖的界限,而不是:

var bounds = map.getBounds(); 

你要麼需要通過地圖對象插入到onClickCallback功能,或使其成爲一個全局變量。


更新: 試試這個。你仍然想要在你的initialize函數中創建地圖。 :

$(function() { 
     var map; 
     var markers = new Array(); 

     function initialize() { 
      var mapOptions = { 
       center: new google.maps.LatLng(46.4750370, 7.2843), 
       zoom: 7, 
       mapTypeId: google.maps.MapTypeId.ROADMAP 
      }; 
      map = new google.maps.Map(document.getElementById("map"), mapOptions); 

      google.maps.event.addListener(map, 'zoom_changed', onClickCallback); //listen to zoom event and call function to reload markers 
      google.maps.event.addListener(map, 'drag', onClickCallback); //listen to drag/pan event and call function to reload markers 

      google.maps.event.addListener(map, 'tilesloaded', function() { 
       onClickCallback(map); 
      }); 
     } 


     function onClickCallback(map) { 
      var bounds = map.getBounds(); 

      $.getJSON('http://skiweather.eu/gmap3/markers/index.php', { 
       swLat: bounds.getSouthWest().lat(), 
       swLon: bounds.getSouthWest().lng(), 
       neLat: bounds.getNorthEast().lat(), 
       neLon: bounds.getNorthEast().lng() 
      }, function (data) { 
       $.each(data.markers, function (i, marker) { 
        $('#map').gmap('addMarker', { 
         'position': new google.maps.LatLng(marker.latitude, marker.longitude), 
         'bounds': true 
        }).click(function() { 
         $('#map').gmap('openInfoWindow', { 
          'content': marker.content 
         }, this); 
        }); 
       }); 
      }); 
     } 

     google.maps.event.addDomListener(window, 'load', initialize); 
+0

我該如何製作一個全局變量? – mark

+0

而不是在你的初始化函數中創建'var map;',而不是在你的初始化函數內部創建'var map;' – duncan

+0

好吧,我這樣做了,但標記是不可見的。我改變了上面的代碼。 – mark

相關問題