2013-10-22 72 views
0

我創建了以下代碼以向我展示GMaps上的一些標記。如果我刪除代碼多標記GMaps PHP SQLSERVER

var locations = [ 
<?=implode(',',$locations); // De in PHP gemaakte array samenvoegen tot een regel gescheiden door komma's ?> 

]。

Gmaps顯示出來。

如果我添加代碼頁面返回空白。主要目標是運行一個查詢結果爲經緯度信息。這些信息已在GMaps

在創建一個標記

完整代碼:

<!DOCTYPE html > 
    <head> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
    <title>Agents</title> 
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3&key=AIzaSyBPjiZ-mcRgK6jV00orSHefk6L40gsznkk&sensor=false&language=nl&region=nl"></script> 
     <style> 
     html, body, #map-canvas { 
     height: 100%; 
     margin: 0px; 
     padding: 0px 
     } 
    </style> 
    <?php 
include ("dblogin.php") 
// Alle klanten ophalen 
$query = "SELECT MachineId as name, [value17] as lat ,[value18] as lng, ast.online as state FROM [ksubscribers].[dbo].[vAuditMachineSummary] left join ksubscribers.dbo.agentState as ast on ast.agentGuid = vAuditMachineSummary.agentGuid where [value17] is not null"; 
$data = mssql_query($query); 


// Door alle klanten lopen 
while($row = mssql_fetch_array($data)) 
{ 
    // Een array opbouwen met de klant naam en coördinaten 
    // (welke nog even van een komma naar een punt veranderd worden) 
    $location[] = '['.$row['name'].'","'.($row['lat']).'","'.($row['lng']).'"","'.($row['state']).']'; 

} 
?> 

<script type="text/javascript"> 

// De array met gegevens in Javascript 
var locations = [ 
    <?=implode(',',$locations); // De in PHP gemaakte array samenvoegen tot een regel gescheiden door komma's ?> 
]; 


// Nieuwe cq mooiere visuele weergave gebruiken 
google.maps.visualRefresh = true; 

// Functie die aangesproken wordt zodra de pagina geladen is 
function initialize() 
{ 
    // Kaart opties 
    var mapOptions = { 
     center: new google.maps.LatLng(52.23, 4.55), // Coördinaten van centraal Nederland 
     zoom: 8, // Inzoomen zodat Nederland goed zichtbaar is 
     mapTypeId: google.maps.MapTypeId.ROADMAP // De wegenkaart gebruiken 
    }; 

    // Kaart laden in div met het ID map-canvas 
    var map = new google.maps.Map(document.getElementById("map-canvas"),mapOptions); 

    // De "markers" plaatsen 
    setMarkers(map,locations); 
} 

// De array met gegevens in Javascript 
//var loc = [ 
// <?php=implode(',',$location);?> 
//]; 

// Functie die de "markers" plaatst op de kaart 
function setMarkers(map,locations) 
{ 
    // De afbeelding voor de klanten op de kaart 
    var image = 'http://icons.iconarchive.com/icons/icons-land/vista-people/24/Office-Customer-Male-Light-icon.png'; 

    // Door alle klanten lopen 
    for (var i = 0; i < locations.length; i++) 
    { 
     // De coördinaten ophalen 
     var loc = locations[i]; 
     var myLatLng = new google.maps.LatLng(loc[1], loc[2]); 

     // De "marker" plaatsen 
     var marker = new google.maps.Marker({ 
      position: myLatLng, 
      map: map, 
      icon: image, 
      title: loc[0] 
     }); 
    } 
} 

// Zodra de pagina is geladen de initialize functie aanspreken 
google.maps.event.addDomListener(window, 'load', initialize); 

    </script> 
    </head> 

    <body> 
    <div id="map-canvas"></div> 

    </body> 
</html> 

回答

1

(未測試)的問題很可能是這一行:

$location[] = '['.$row['name'].'","'.($row['lat']).'","'.($row['lng']).'"","'.($row['state']).']'; 

在這裏你$行後,關閉一個字符串['名字']你永遠不會打開。

還需要很紙條給PHP函數json_encode(http://nl3.php.net/json_encode),這一個,確保您有效的JavaScript(如果例如$行[「名稱」]中包含一個「字符數限制)。

+0

你什麼意思?我看到一些奇怪的東西,並將它改爲 '$ locations [] ='['。$ row ['name']。'「,」'。$ row ['lat']。'「,」'。$ row ['lng']。'「」,「'。$ row ['state']。']';' 但是頁面仍然保持空白。 –

+0

如果$ row ['name']等於「Sander」(不含引號),則完整的字符串(連接後)將爲: '[Sander],「123」,「456」,「state]' 無效javascript –

+0

你好Peter, 你說的字符串有問題,改成'$ location [] ='['''。$ row ['name']。'','。$ row ['lat' ]。','。$ row ['lng']。','。$ row ['state']。']'; 還有一個sql連接問題。 –