2017-06-17 58 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

是的,在第一次加載印度地圖時,當我放置合適的邊界時,地圖未正確初始化 –

相關問題