2017-04-13 155 views
-1
<!DOCTYPE html> 
<?php 
    /* lat/lng data will be added to this array */ 
    try{$work=$_GET["service"];}catch(Exception $e){echo 'Authorization Failed.Map may Misbehave or Buggy.<a href="submitbug.php">Click here to report this</a>';} 
    $locations=array(); 
    $uname="root"; 
    $pass=""; 
    $servername="localhost"; 
    $dbname="bcremote"; 
    $db=new mysqli($servername,$uname,$pass,$dbname); 
    $query = $db->query('SELECT * FROM location'); 
    while($row = $query->fetch_assoc()){ 
     $name = $row['uname']; 
     $longitude = $row['longitude'];        
     $latitude = $row['latitude']; 
     $link=$row['link']; 
     /* Each row is added as a new array */ 
     $locations[]=array('name'=>$name, 'lat'=>$latitude, 'lng'=>$longitude, 'lnk'=>$link); 
    } 
    //echo $locations[0]['name'].": In stock: ".$locations[0]['lat'].", sold: ".$locations[0]['lng'].".<br>"; 
    //echo $locations[1]['name'].": In stock: ".$locations[1]['lat'].", sold: ".$locations[1]['lng'].".<br>"; 
?> 
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAfFN8NuvYyNkewBVMsk9ZNIcUWDEqHg2U&callback=initMap()" 
    async defer></script> 
<script> 
     //var myLatLng = {lat: -25.363, lng: 131.044}; 
     function initMap() { 
     var myLatLng = {lat: -25.363, lng: 131.044}; 

     var map = new google.maps.Map(document.getElementById('map'), { 
      center: myLatLng, 
      scrollwheel: false, 
      zoom: 10 
     }); 

     <?php for($i=0;$i<sizeof($locations);$i++) 
     { ?> 
     var marker = new google.maps.Marker({ 
      map: map, 
      position: {lat: <?php echo $locations[$i]['lat']?>,lng: <?php echo $locations[$i]['lng']?>}, 
      title: 'Service', 
      url: 'https://<?php echo $locations[$i]['lnk']?>' 
     }); 


     google.maps.event.addListener(marker, 'click', function() { 
     window.location.href = marker.url; 
     }); 
     <?php } ?> 
     } 
</script> 
<body> 
<div id="map"></div> 
</body> 

我有這樣的代碼。我認爲在代碼方面沒有任何錯誤可能在邏輯方面。請幫我過來這個。我需要在接下來的2天內提交這個項目。我可以知道如何解決這個問題嗎?谷歌地圖API不工作

憑藉先進的感謝, Kavin

+0

什麼問題?請提供一個證明你的問題的[mcve]。 – geocodezip

+0

對不起現在我已經修復了... –

回答

0

看來這個問題是與谷歌地圖的關鍵,而我處理的方式。現在它完美地工作。一旦我更新了它,它就開始工作。該代碼也可以通過GitHub存儲庫的幫助進行更改。

<?php 
     $locations=array(); 
     $work=$_GET["service"]; 
     $uname="root"; 
     $pass=""; 
     $servername="localhost"; 
     $dbname="bcremote"; 
     $db=new mysqli($servername,$uname,$pass,$dbname); 
     $query = $db->query("SELECT * FROM location where work='$work'"); 
     //$number_of_rows = mysql_num_rows($db); 
     //echo $number_of_rows; 

     while($row = $query->fetch_assoc()){ 
      $name = $row['uname']; 
      $longitude = $row['longitude'];        
      $latitude = $row['latitude']; 
      $link=$row['link']; 
      /* Each row is added as a new array */ 
      $locations[]=array('name'=>$name, 'lat'=>$latitude, 'lng'=>$longitude, 'lnk'=>$link); 
     } 


     //echo $locations[0]['name'].": In stock: ".$locations[0]['lat'].", sold: ".$locations[0]['lng'].".<br>"; 
     //echo $locations[1]['name'].": In stock: ".$locations[1]['lat'].", sold: ".$locations[1]['lng'].".<br>"; 
    ?> 
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=AIzaSyAfFN8NuvYyNkewBVMsk9ZNIcUWDEqHg2U"></script> 
    <script type="text/javascript"> 
    var map; 
    var Markers = {}; 
    var infowindow; 
    var locations = [ 
     <?php for($i=0;$i<sizeof($locations);$i++){ $j=$i+1;?> 
     [ 
      'AMC Service', 
      '<p><a href="<?php echo $locations[0]['lnk'];?>">Book this Person Now</a></p>', 
      <?php echo $locations[$i]['lat'];?>, 
      <?php echo $locations[$i]['lng'];?>, 
      0 
     ]<?php if($j!=sizeof($locations))echo ","; }?> 
    ]; 
    var origin = new google.maps.LatLng(locations[0][2], locations[0][3]); 

    function initialize() { 
     var mapOptions = { 
     zoom: 9, 
     center: origin 
     }; 

     map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 

     infowindow = new google.maps.InfoWindow(); 

     for(i=0; i<locations.length; i++) { 
      var position = new google.maps.LatLng(locations[i][2], locations[i][3]); 
      var marker = new google.maps.Marker({ 
       position: position, 
       map: map, 
      }); 
      google.maps.event.addListener(marker, 'click', (function(marker, i) { 
       return function() { 
        infowindow.setContent(locations[i][1]); 
        infowindow.setOptions({maxWidth: 200}); 
        infowindow.open(map, marker); 
       } 
      }) (marker, i)); 
      Markers[locations[i][4]] = marker; 
     } 

     locate(0); 

    } 

    function locate(marker_id) { 
     var myMarker = Markers[marker_id]; 
     var markerPosition = myMarker.getPosition(); 
     map.setCenter(markerPosition); 
     google.maps.event.trigger(myMarker, 'click'); 
    } 

    google.maps.event.addDomListener(window, 'load', initialize); 

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