2016-05-14 101 views
0

我想將地址轉換爲經度和緯度,但值不顯示。 我甚至想將結果存儲在數據庫中,但我無法在第一時間獲得經緯度。我想知道我出錯的地方。將地址轉換爲經度和緯度

下面是代碼:

<html> 
<head> 
<title>Theater</title> 
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&language=en" ></script> 
    <script> 
    // Called when page is loaded 
    function Init() 
    { 
    var address = Document.getElementById("address"); 
    var city = Document.getElementById("city"); 
    var state = Document.getElementById("state"); 
    var zip = Document.getElementById("zip"); 
    var country = Document.getElementById("country"); 
     // make google call to convert address to lat/lng coordinate 
     var geocoder = new google.maps.Geocoder(); 
     geocoder.geocode({ 'address': address+city+state+zip+country}, ShowLatLng); 
    } 

    // This function is the called when Google geocode() returns the result of geocoding an address 
    function ShowLatLng(results, status) 
    { 
     if (status == google.maps.GeocoderStatus.OK) 
     { 
      document.getElementById('latlng').innerHTML = results[0].geometry.location; 
     } 
    } 
</script> 
</head> 
<body> 
<table><tr> 
<td> Address &emsp; </td> 
<td><input type="text" name="address" id="address"> </td> </tr> 
<tr><td> City</td><td><input type="text" name="city" id="city"></td> 
<td>State </td><td><input type="text" name="state" id="state"></td></tr> 
<tr><td>zip</td><td><input type="text" name="zip" id="zip"></td> 
<td>Country</td><td><input type="text" name="country" id="country"></td> 
</tr></table> 
<input type="button" onclick="Init()" value="Determine Lat/Lon"> 
<label id="latlng"></label> 
+0

什麼錯誤信息,你在瀏覽器的JavaScript控制檯看到?有一個明顯的錯誤 - 「Documet」 –

+0

沒有錯誤。它只是不顯示。 @Pekka웃 – Shraddha

+0

在* var address = Documet.getElementById(「address」); *由於錯誤'Documet',* got *是錯誤的。你確定你正在尋找錯誤控制檯嗎? –

回答

0

你說你沒有看到在您的控制檯任何JS錯誤,所以我最好的猜測是你的地址不匹配。

我對這個問題有兩個想法。

首先,可能地址格式不正確。有時候,地理編碼API無法匹配地址,因爲有縮寫,缺少的字詞(如「街道」或「道路」等),因爲數字有破折號或其他中斷,或其他標點符號和拼寫可能性。做地址standardization —使地址統一—和validation —檢查地址與數據庫—以獲得正確的格式是非常有用的。我發現您使用的是Google Maps API,而Google對搜索字詞的使用確實很好,但這並不是說它們在地址標準化方面非常完美,而且它們也不會進行地址驗證。

其次,你的代碼在這裏:

geocoder.geocode({ 'address': address+city+state+zip+country}, ShowLatLng); 

可能無法正常工作。如果:

address = "123 My House Road"; 
city = "My City"; 
state = "AA"; 
zip = "12345"; 
country = "USA"; 

然後address+city+state+zip+country結果123 My House RoadMy CityAA12345USA。請注意,地址各部分之間沒有空格。這與我上面提到的基本上是相同的問題:地址不匹配—它沒有被發現。

(全面披露:我工作SmartyStreets,地址認證公司)