2015-11-21 27 views
-1

我正在製作一張地圖,我想用數據庫中的值作爲javascript中的座標。請看下面:從mysql數據庫中檢索值作爲javascript中的值使用

我想通過從數據庫中獲取數據來改變經度和緯度的默認值,但我不知道如何。

<html> 
    <head> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
    <title>Google Maps</title> 
    <script src="http://maps.google.com/maps/api/js?sensor=false"  t  type="text/javascript"></script> 
    <script type="text/javascript"> 
    function LatiPoint(options) 
    { 
     this.UserName = ""; 
     this.JobType = ""; 
     this.UserMood = ""; 
     this.Latitude = ""; 
     this.Longitude = ""; 
     this.Pic = ""; 
     this.Color = "red"; 
     this.OpenInfoWindowInitial = false; 

     if (options != null) 
     { 
      this.UserName = options.UserName; 
      this.JobType = options.JobType; 
      this.UserMood = options.UserMood; 
      this.Latitude = options.Latitude; 
      this.Longitude = options.Longitude; 
      this.Pic = options.Pic; 
      this.Color = (options.Color == null ? "red" : options.Color); 
      this.OpenInfoWindowInitial = (options.OpenInfoWindowInitial == null ? false : options.OpenInfoWindowInitial); 
     } 
    } 
    </script> 

    <script type="text/javascript"> 
     var LatiPts = new Array(); 
     LatiPts[0] = new LatiPoint({UserName:'Dukot',JobType:'Balulang Cagayan  De Oro',UserMood:'drinking beer',Latitude:8.4542363,Longitude:124.63189769999997,Color:'yellow',OpenInfoWindowInitial:true,Pic:''}); 
     LatiPts[1] = new LatiPoint({UserName:'Alot',JobType:'Cagayan De Oro',UserMood:'with classmates',Latitude:8.458353831903118,Longitude:124.63627706511227,Color:'yellow',OpenInfoWindowInitial:true,}); 
     LatiPts[2] = new LatiPoint({UserName:'Lindongan',JobType:'SM Cagayan De Oro',UserMood:'feeling bored',Latitude:8.456188949479728,Longitude:124.62177167875973,Color:'yellow',OpenInfoWindowInitial:true,}); 
     LatiPts[3] = new LatiPoint({UserName:'Galal',JobType:'Carmen Cagayan De Oro',UserMood:'beer',Latitude:8.467607505884205,Longitude:124.62271581633297,Color:'yellow',OpenInfoWindowInitial:true,}); 
     LatiPts[4] = new LatiPoint({UserName:'Belen',JobType:'Cagayan De Oro',UserMood:'beer',Latitude:8.46332028090713,Longitude:124.63537584288326,Color:'yellow',OpenInfoWindowInitial:true,}); 


    </script> 

    <script type="text/javascript"> 
    //<![CDATA[ 
     var infoWindows = []; 
     var markers = []; 

     // A function to create the marker and set up the event window 
     function createMarker(map, point, title, icon) { 
      var marker = new google.maps.Marker(); 
      marker.setMap(map); 
      marker.setPosition(point); 
      marker.setTitle(title); 
      marker.setIcon(icon) 

      google.maps.event.addListener(marker, 'click', function() { 
       for (var i = 0; i < infoWindows.length; i++) 
        infoWindows[i].setZIndex(0); 

       infoWindows[marker.counter].setZIndex(1); 
       infoWindows[marker.counter].open(marker.getMap(), marker); 
      });    

      return marker; 
     } 

     function createInfoWindow(html) 
     { 
      var infoWindow = new google.maps.InfoWindow(); 
      infoWindow.setContent(html); 
      infoWindow.setZIndex(0);   

      return infoWindow; 
     } 

     function initialize() { 

      // Create the Google Map 
      var map = new google.maps.Map(document.getElementById("map")); 
      map.setMapTypeId(google.maps.MapTypeId.ROADMAP); 
      map.setCenter(new google.maps.LatLng(0,0)); 

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

      for (var i = 0; i < LatiPts.length; i++) { 
       var lpt = LatiPts[i]; 
       var point = new google.maps.LatLng(lpt.Latitude, lpt.Longitude); 
       var html = "<span style='font-family:Arial;font-size:9pt'>"; 

       html += "<span style='font-size:12pt;font-weight:bold'>" + lpt.UserName + "</span><br />"; 
       html += lpt.UserMood.substring(0, 30) + "<br/>"; 
       html += lpt.JobType.substring(0, 30) + "<br/>"; 


       html += "</span>"; 

       var imgPath 
       if(lpt.Pic != "") { 
        imgPath = lpt.Pic 
       } else { 
        imgPath = "http://maps.gstatic.com/intl/en_ALL/mapfiles/marker.png" 
       } 
       //alert("imgPath = " + imgPath + " Pic: " + lpt.Pic) 
       var icon = new google.maps.MarkerImage(imgPath); 
       //icon.shadowSize = GSize.ZERO; 

       var infoWindow = createInfoWindow(html); 
       infoWindow.open(); 

       var marker = createMarker(map, point, lpt.UserName, icon); 
       marker.setZIndex(0); 
       marker.counter = i; 


      if (LatiPts[i].OpenInfoWindowInitial) 
       { 
        infoWindow.open(marker.getMap(), marker); 
       } 
       infoWindows.push(infoWindow); 

       markers.push(marker); 


       bounds.extend(point); 

          } 

      map.setCenter(bounds.getCenter()); 
      map.fitBounds(bounds); 

     } 

     function handleNoFlash(code) 
     { 
      if (code == GStreetviewPanorama.ErrorValues.FLASH_UNAVAILABLE) 
       alert('You need flash player to view the panorama.'); 
     } 

     function convertLatLngToString(latlng) 
     { 
      var hour = parseInt(latlng) 
      var min = parseInt((latlng - hour) * 60) 
      var second = (((latlng - hour) * 60) - min) * 60 

      return (hour + "&deg; " + min + "&#39; " + second.toFixed(2) + "&#34; "); 
     } 

    //]]> 
    </script>  

    <style type="text/css"> 
     /*give the body height:100% so that its child 
     elements can have percentage heights*/ 

     body{ margin:0;padding:0;height:100% } 

     div.fullscreen{ 
      display:block; 


      position:absolute; 
      top:0; 
      left:0; 


      width:100%; 
      height:100%; 
      z-index: 9999; 
      margin: 0; 
      padding: 0; 
      background: inherit; 
     } 

    </style> 
</head> 
<body onload="initialize()"> 
    <div id="map" class="fullscreen" style="width:100%; height:100%"></div> 
    <div id="streetview" style="width: 650px; height: 400px; display: none;"></div> 
    <noscript> 
     <b>JavaScript must be enabled in order for you to use Google Maps.</b> 
    </noscript> 

</body> 
</html> 
+2

請出示你的PHP代碼! –

+1

你說你正在製作地圖,但代碼只顯示一個功能。如果你可以用完整的代碼來完成這個問題,它將很容易幫助你。 –

+0

我試圖發佈所有的代碼,但它不會允許我。它說錯誤。無論如何擴大我的問題。我想自定義緯度和經度的值,就像你在代碼的最後兩行看到的那樣。我設置的值有默認值。我想要的是從數據庫中獲取價值。應該獲取數據庫中的任何其他輸入,並且應該創建一個循環來創建上面類似代碼的新行(最後兩行)。我希望我有道理:)謝謝! – jack

回答

0

你可以做這樣的事情:

 
    <?php /* read from database into variable $results*/ ?> 
    this.UserName = "<?php echo $results; ?>"; 
希望它能幫助:)

+0

謝謝法魯克,我會試試這個:) – jack

+0

但是,如果我從我的數據庫中使用JavaScript的同一頁面獲取。人們可以看到我的登錄信息正確嗎?我該怎麼解決這個問題?謝謝! – jack

+0

如果你想在JS中使用某些東西,它將對用戶可見,所以不要在JS中混淆有價值的東西。不,用戶不可見,它將在頁面加載時在服務器上解析。用戶只會看到this.UserName =「User007」; –

相關問題