2015-12-13 187 views
0

新的編碼,但有一個想法,使我的工作更輕鬆的工作 - 也許我太雄心勃勃!php和谷歌地圖api

我發現了一些代碼,允許通過變量在代碼中放置的谷歌地圖上顯示推針 - 郵編 - 我需要將郵編從一個Excel CSV文件傳入代碼,然後顯示在地圖。

原始代碼不是我的,但我粘貼在這裏顯示我的查詢的基礎。 這不是PHP,但覺得我想用PHP鏈接到Excel - 如果這是錯誤的,請隨時指出我在正確的方向!

<!DOCTYPE html> 
<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>Google Maps JavaScript API v3 Example: Geocoding Simple</title> 
<link href="http://code.google.com/apis/maps/documentation/javascript/examples/standard.css" rel="stylesheet" type="text/css" /> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
<script type="text/javascript"> 
    var geocoder; 
    var map; 
    var markers = []; 
    function initialize() { 
     geocoder = new google.maps.Geocoder(); 
     var latlng = new google.maps.LatLng(40.768505,-111.853244); 
     var myOptions = { 
      zoom: 8, 
      center: latlng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     } 
     map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
      addPostCode ('BA2 3RA'); 
     addPostCode ('BN1 1AB'); 
     addPostCode ('BA3 3JG'); 
      } 

    function addPostCode(zip) { 
     geocoder.geocode({ 'address': zip}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) 
     { 
      map.setCenter(results[0].geometry.location); 
      var marker = new google.maps.Marker({ 
      map: map, 
      position: results[0].geometry.location, 
      name: zip 
     }); 
     markers.push(marker); 
     } else { 
      alert("Geocode was not successful for the following reason: " + status); 
     } 
     }); 
    } 

    function checkZip(zip) 
    { 
     var distance = Number.MAX_VALUE; 
     var index = 0; 
     geocoder.geocode({ 'address': zip}, function(results, status) 
     { 
      if (status == google.maps.GeocoderStatus.OK) 
      { 
       for(ix=0; ix< markers.length; ix++) 
       { 
        var tmp = getDistance(results[0].geometry.location, markers[ix].position); 
        if (tmp < distance) 
        { 
         distance = tmp; 
         index = ix; 
        } 
       } 
       alert('nearest zipcode is :' + markers[index].name); 
      } 
     }); 
    } 

    function getDistance(latlng1, latlng2) 
    { 
     var R = 6371; // Radius of the earth in km 
     var dLat = (latlng2.lat()-latlng1.lat()) * Math.PI/180; // Javascript functions in radians 
     var dLon = (latlng2.lng()-latlng1.lng()) * Math.PI/180; 
     var a = Math.sin(dLat/2) * Math.sin(dLat/2) + 
      Math.cos(latlng1.lat() * Math.PI/180) * Math.cos(latlng2.lat() * Math.PI/180) * 
      Math.sin(dLon/2) * Math.sin(dLon/2); 
     var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
     var d = R * c; // Distance in km 
     d = d * 0.621371192; // convert to miles 
     return d; 
    } 
</script> 
</head> 
<body onload="initialize()"> 
    <div> 
    <input id="address" type="textbox" value=""> 
    <input type="button" value="Geocode" onclick="checkZip(getElementById('address').value)"> 
    </div> 
<div id="map_canvas" style="height:90%"></div> 
</body> 
</html> 
+0

1)excel文件的擴展名爲.xsl或.xsls。 csv是另一種在Excel中可讀的文件類型。 2)要訪問csv數據,請使用ajax函數來使用您所使用的php代碼。要在php中檢索csv數據,在php中有一個名爲fgetcsv()的函數,請在此處閱讀更多信息:http://www.w3schools.com/php/func_filesystem_fgetcsv.asp 3)不要期望我們編寫代碼你告訴我們你做了什麼,除了在這裏複製別人的代碼,並告訴我們結果和你的問題。我很樂意提供幫助。 4)您在這裏沒有問題,AFAI可以看到。 –

+0

道歉,如果我聽起來像我只是想你提供一個完整的解決方案 - 這不是我的意圖 - 我已經適應了上述代碼以適應我的需要,但很難閱讀包含約10個郵政編碼的csv文件 - 我需要此代碼顯示addPostCode('BN1 1AB');它從csv文件中獲取郵編,假設它是可能的,因爲大多數情況都是這樣,但是很難得到答案 - 我會繼續堅持下去:) – PaulyboyUK

+0

同樣,目前還不清楚,是否要替換'addPostCode(' BN1 1AB');'來自您的CSV文件數據的結果? –

回答

0

我看到你已經得到CSV數據到一個PHP數組。所以下一步就是將它傳遞給JavaScript數組。要做到這一點,我們依賴於這樣一個事實:在包含HTML標記和PHP代碼混合的任何.php文件中(HTML當然不包括<?php .... ?>標記和其中的PHP),在運行時,PHP解釋器會遍歷文件FIRST並處理PHP。所以,如果你在PHP中有一個echo語句,它將會迴應所有被回顯到HTML中的東西,然後將HTML解析爲HTML標記,並將其寫入任何值。請注意,我們甚至可以圍繞HTML(或者在這種情況下嵌入在HTML中的Javacript)進行PHP循環,這完全是因爲PHP在其他任何事情之前得到了處理!

因此,在以.php腳本做:

<!DOCTYPE html> 
<?php 

include "getCsvData.php"; // this is where you put the data into a PHP array called $dataArray; 
// now we exit the PHP and do the HTML including our Javascript... 

?> 
[HTML head code here] 
<body> 
[more HTML markup here, we usually put javascripts at the end] 

<script> 
var markers = []; // your javascript array 

<?php 
// start a loop in the PHP 
for($i=0; $i < 10; $i++) 
{ 
// then back to the javascript... 
?> 
arr[<?php echo $i;?>] = <?php echo $dataArray[$i]; ?>; // note the final semi-colon ; here 

<?php } ?> 
</script> 

所以這是它(希望我沒有得到任何錯別字出現,道歉,如果我有,你需要對其進行測試)。如果您在瀏覽器中顯示網頁並右鍵單擊它,然後選擇瀏覽器的「源代碼」選項,則可以看到<腳本>腳本奇蹟般地增加了10行JavaScript代碼,添加了數組值。 (在這個例子中,爲了方便起見,我使用了10,你需要找出PHP數據數組使用count())有多大。

不要忘記最後一行php關閉php循環 - 它只是PHP標籤中的一個簡單的關閉花括號。另請注意,doctype行必須是文件中的第一行。

最後,由於您剛開始使用Google地圖,並且剛剛找到可以使用的代碼,請務必訪問Google API文檔https://developers.google.com/maps/documentation/javascript/tutorial--這很清楚。是的,正如你所說,你當然有雄心壯志,但這很好,這也將成爲PHP編碼和谷歌地圖的絕佳介紹。一切順利吧!

+0

GuyMany感謝你和你的鼓勵它的話真的很感激 - 我會報告回來! :) – PaulyboyUK

0

這是我的工作 - 不知道它是否會幫助任何人(需要整理!!),但希望它有一天!

<!DOCTYPE html> 

<?php 
include "getCsvData.php"; 
?> 

<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>Google Maps JavaScript API v3 Example: Geocoding Simple</title> 
    <link href="http://code.google.com/apis/maps/documentation/javascript/examples/standard.css" rel="stylesheet" type="text/css" /> 
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
    <script type="text/javascript"> 
     var geocoder; 
     var map; 
     var markers = []; 

     function initialize() { 
      geocoder = new google.maps.Geocoder(); 
      var latlng = new google.maps.LatLng(40.768505,-111.853244); 
      var myOptions = { 
       zoom: 8, 
       center: latlng, 
       mapTypeId: google.maps.MapTypeId.ROADMAP 
      }; 
      map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 

      <?php 
     for($i = 0, $j = count($dataArray); $i < $j-1 ; $i++) { 
     ?> 
      addPostCode(<?php echo str_replace(array('[', ']'), '', json_encode($dataArray[$i])); ?>); 
      <?php } ?> 
     } 
     function addPostCode(zip) { 
      geocoder.geocode({ 'address': zip}, function(results, status) { 
       if (status == google.maps.GeocoderStatus.OK) 
       { 
        map.setCenter(results[0].geometry.location); 
        var marker = new google.maps.Marker({ 
         map: map, 
         position: results[0].geometry.location, 
         name: zip 
        }); 
        markers.push(marker); 
       } else { 
        alert("Geocode was not successful for the following reason: " + status); 
       } 
      }); 
     } 

     function checkZip(zip) 
     { 
      var distance = Number.MAX_VALUE; 
      var index = 0; 
      geocoder.geocode({ 'address': zip}, function(results, status) 
      { 
       if (status == google.maps.GeocoderStatus.OK) 
       { 
        for(ix=0; ix< markers.length; ix++) 
        { 
         var tmp = getDistance(results[0].geometry.location, markers[ix].position); 
         if (tmp < distance) 
         { 
          distance = tmp; 
          index = ix; 
         } 
        } 
        alert('nearest zipcode is :' + markers[index].name); 
       } 
      }); 
     } 

     function getDistance(latlng1, latlng2) 
     { 
      var R = 6371; // Radius of the earth in km 
      var dLat = (latlng2.lat()-latlng1.lat()) * Math.PI/180; // Javascript functions in radians 
      var dLon = (latlng2.lng()-latlng1.lng()) * Math.PI/180; 
      var a = Math.sin(dLat/2) * Math.sin(dLat/2) + 
       Math.cos(latlng1.lat() * Math.PI/180) * Math.cos(latlng2.lat() * Math.PI/180) * 
       Math.sin(dLon/2) * Math.sin(dLon/2); 
      var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
      var d = R * c; // Distance in km 
      d = d * 0.621371192; // convert to miles 
      return d; 
     } 
    </script> 
</head> 
<body onload="initialize()"> 
<div id="map_canvas" style="height:90%"></div> 
</body> 
</html>