Google地图的经度和纬度范围

时间:2013-11-26 16:23:52

标签: google-maps google-maps-api-3

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);

1 个答案:

答案 0 :(得分: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);