2017-06-17 56 views
2

我已经写了一个代码,用于从数据库中检索数据并显示在google map.i上,但是到目前为止,我无法在数据加载时放大标记的位置。请帮助确定这一点out.am获得在提前JSON格式由于数据...如何在加载时将地图缩放到标记?

function initMap() { 
     // Create the map. 
     var infoWindow = new google.maps.InfoWindow(); 
     var map = new google.maps.Map(document.getElementById('mapView'), { 
      zoom: 5, 
      center: {lat: 20.5937, lng: 78.9629}, 
      mapTypeId: 'roadmap' 
     }); 
    var type=document.getElementById("type1").innerHTML; 
    var region=document.getElementById("reg").innerHTML; 
    // var region=$('#reg').html(); 
    var hq=document.getElementById("hq1").innerHTML; 
    var division=document.getElementById("div").innerHTML; 
    // alert(type); 
    // alert(region); 
    // alert(hq); 
    // alert(division); 

     $.getJSON('data.php', 
      { 
     region: region, 
     hq: hq, 
     division: division, 
     type:type 
    }, 

     function(data){ 

      var marker = []; 
      var infowindow = []; 
      var contentString = []; 
      for(var sd in data){ 
       // alert(data); 
       contentString[sd] = '<div id="content">'+ 
        '<div id="siteNotice">'+ 
        '</div>'+ 
        '<h1 id="firstHeading" class="firstHeading">'+data[sd].name+'</h1>'+ 
        '<div id="bodyContent">'+ 
         '<p><b>Address: </b>'+data[sd].address+'</p>'+ 
         '<p><b>Area: </b>'+data[sd].area+'</p>'+ 
         '<p><b>Mobile: </b>'+data[sd].mobile+'</p>'+ 
         '</div>'+ 
       '</div>'; 

       infowindow[sd] = new google.maps.InfoWindow({ 
        content: contentString[sd] 
       }); 
       if(data[sd].type == 1){ 
        marker[sd] = new google.maps.Marker({ 
         icon: 'http://maps.google.com/mapfiles/ms/icons/red-dot.png', 
         position: data[sd].center, 
         map: map, 
         infowindow: infowindow[sd] 
        }); 
       } 
       if(data[sd].type == 2){ 
        marker[sd] = new google.maps.Marker({ 
         icon: 'http://maps.google.com/mapfiles/ms/icons/red-dot.png', 
         position: data[sd].center, 
         map: map, 
         infowindow: infowindow[sd] 
        }); 
       } 

       google.maps.event.addListener(marker[sd], 'click', function() { 


        this.infowindow.open(map, this); 
        // alert(this.infowindow); 


       }); 


      } 
     }); 
     } 

{ “名”: “耳鼻喉诊疗”, “地址”: “Chandanagar”, “区域”: “CHANDANAGAR” , “mobile”:“9848524297”, “type”:“1”, “center”:{ “lat”:17.5236717, “lng”:78.2970363 }

回答

2

检查文档: https://developers.google.com/maps/documentation/javascript/reference 您必须使用fitBounds()方法。根据每个点

var bounds = new google.maps.LatLngBounds(); 

在循环内部,在它的端部,延伸的范围:

环路之前,初始化的边界可变

bounds.extend(marker[sd].position); 

在循环之后(=所有标记加载后),请使用fitBounds()方法:

map.fitBounds(bounds); 
+0

我已经这样做了,但地图不是完全加载显示不同的l经度和纬度 –

+0

你是什么意思不同的经度和纬度? 是从数据库中正确显示的数据? – treecon

+0

是的,在第一次加载印度地图时,当我放置合适的边界时,地图未正确初始化 –

相关问题