2012-12-03 76 views
34

我正在嘗試將Google自動填充搜索框添加到網站,以便用戶儘可能輕鬆地搜索地址。如何添加Google地圖自動填充搜索框?

我的問題是,我在這裏看到了很多關於這個以及Google Maps Javascript API v3的問題,關於這個和一些教程,他們都將自動完成功能與映射到嵌入式Google地圖上。

我不需要直觀地映射位置,現在我只需要自動填充框,不幸的是我不能確定API的哪些部分與此相關,並且我看過的每個示例都包含大量的JS for映射。

是否有人知道僅處理添加自動完成輸入功能的資源。

該網站爲here

如果您點擊'報價計算器'>>>'運輸'>>>'收集'你會看到輸入框,我想添加這個功能。

我不是問任何人爲我做我的工作,只是爲了幫助我找到我需要的東西。

在此先感謝

+0

的可能重複[如何在谷歌地圖的JavaScript API V3搜索盒集成?](http://stackoverflow.com/questions/12745469/how-to-integrate-searchbox-in-google- maps-javascript-api-v3) – givanse

回答

78

該代碼的重要部分可以消除。

HTML摘錄:

<head> 
    ... 
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script> 
    ... 
</head> 
<body> 
    ... 
    <input id="searchTextField" type="text" size="50"> 
    ... 
</body> 

的Javascript:

function initialize() { 
    var input = document.getElementById('searchTextField'); 
    new google.maps.places.Autocomplete(input); 
} 

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

您可以在http://www.redwoodtransit.org看到一個例子。

+0

嗨,你可以分享這個例子的源代碼....謝謝 –

+0

這幫助我和海報一樣的問題。謝謝! –

+0

非常感謝:) –

0
<!DOCTYPE html> 
<html> 
    <head> 
    <title>Place Autocomplete Address Form</title> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
    <meta charset="utf-8"> 
    <style> 
     html, body, #map-canvas { 
     height: 100%; 
     margin: 0px; 
     padding: 0px 
     } 
    </style> 
    <link type="text/css" rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500"> 
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=places"></script> 
    <script> 
// This example displays an address form, using the autocomplete feature 
// of the Google Places API to help users fill in the information. 

var placeSearch, autocomplete; 
var componentForm = { 
    street_number: 'short_name', 
    route: 'long_name', 
    locality: 'long_name', 
    administrative_area_level_1: 'short_name', 
    country: 'long_name', 
    postal_code: 'short_name' 
}; 

function initialize() { 
    // Create the autocomplete object, restricting the search 
    // to geographical location types. 
    autocomplete = new google.maps.places.Autocomplete(
     /** @type {HTMLInputElement} */(document.getElementById('autocomplete')), 
     { types: ['geocode'] }); 
    // When the user selects an address from the dropdown, 
    // populate the address fields in the form. 
    google.maps.event.addListener(autocomplete, 'place_changed', function() { 
    fillInAddress(); 
    }); 
} 

// [START region_fillform] 
function fillInAddress() { 
    // Get the place details from the autocomplete object. 
    var place = autocomplete.getPlace(); 

    for (var component in componentForm) { 
    document.getElementById(component).value = ''; 
    document.getElementById(component).disabled = false; 
    } 

    // Get each component of the address from the place details 
    // and fill the corresponding field on the form. 
    for (var i = 0; i < place.address_components.length; i++) { 
    var addressType = place.address_components[i].types[0]; 
    if (componentForm[addressType]) { 
     var val = place.address_components[i][componentForm[addressType]]; 
     document.getElementById(addressType).value = val; 
    } 
    } 
} 
// [END region_fillform] 

// [START region_geolocation] 
// Bias the autocomplete object to the user's geographical location, 
// as supplied by the browser's 'navigator.geolocation' object. 
function geolocate() { 
    if (navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(function(position) { 
     var geolocation = new google.maps.LatLng(
      position.coords.latitude, position.coords.longitude); 
     var circle = new google.maps.Circle({ 
     center: geolocation, 
     radius: position.coords.accuracy 
     }); 
     autocomplete.setBounds(circle.getBounds()); 
    }); 
    } 
} 
// [END region_geolocation] 

    </script> 

    <style> 
     #locationField, #controls { 
     position: relative; 
     width: 480px; 
     } 
     #autocomplete { 
     position: absolute; 
     top: 0px; 
     left: 0px; 
     width: 99%; 
     } 
     .label { 
     text-align: right; 
     font-weight: bold; 
     width: 100px; 
     color: #303030; 
     } 
     #address { 
     border: 1px solid #000090; 
     background-color: #f0f0ff; 
     width: 480px; 
     padding-right: 2px; 
     } 
     #address td { 
     font-size: 10pt; 
     } 
     .field { 
     width: 99%; 
     } 
     .slimField { 
     width: 80px; 
     } 
     .wideField { 
     width: 200px; 
     } 
     #locationField { 
     height: 20px; 
     margin-bottom: 2px; 
     } 
    </style> 
    </head> 

    <body onload="initialize()"> 
    <div id="locationField"> 
     <input id="autocomplete" placeholder="Enter your address" 
      onFocus="geolocate()" type="text"></input> 
    </div> 


    </body> 
</html> 
+2

爲什麼不直接發送鏈接到api文檔,而不是不參考它就複製它? – dft

5

只需複製並粘貼下面相同的代碼即可。

<!DOCTYPE html> 
<html> 
    <head> 
     <script src="https://maps.googleapis.com/maps/api/jsvv=3.exp&sensor=false&libraries=places"></script> 
    </head> 
    <body> 
     <label for="locationTextField">Location</label> 
     <input id="locationTextField" type="text" size="50"> 

     <script> 
      function init() { 
       var input = document.getElementById('locationTextField'); 
       var autocomplete = new google.maps.places.Autocomplete(input); 
      } 

      google.maps.event.addDomListener(window, 'load', init); 
     </script> 
    </body> 
</html> 

格式良好的代碼可以從這個鏈接找到。 http://jon.kim/how-to-add-google-maps-autocomplete-search-box/

2

使用谷歌地圖JavaScript API和地方庫在網頁中實現Google地圖自動完成搜索框。

HTML

<input id="searchInput" class="controls" type="text" placeholder="Enter a location"> 

的JavaScript

<script> 
function initMap() { 
    var input = document.getElementById('searchInput'); 
    var autocomplete = new google.maps.places.Autocomplete(input); 
} 
</script> 

完整的指南,源代碼,並現場演示可以從這裏找到 - Google Maps Autocomplete Search Box with Map and Info Window

1
// Bias the autocomplete object to the user's geographical location, 
// as supplied by the browser's 'navigator.geolocation' object. 
function geolocate() { 
    if (navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(function(poenter code heresition) { 
     var geolocation = { 
     lat: position.coords.latitude, 
     lng: position.coords.longitude 
     }; 
     var circle = new google.maps.Circle({ 
     center: geolocation, 
     radius: position.coords.accuracy 
     }); 
     autocomplete.setBounds(circle.getBounds()); 
    }); 
    } 
} 
2

要立即使用Google地圖/ Places API,您需要使用API​​密鑰。所以API URL將

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=places"></script> 

改變

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"></script> 
0

我使用jQuery在這裏得到了輸入的文本和包裝在$(文件)的所有代碼。就緒()。確保您的API密鑰已準備好用於Google Places API網絡服務。將其替換爲以下腳本文件。

<input type="text" id="location"> 
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=[YOUR_KEY_HERE]&libraries=places"></script> 
<script src="javascripts/scripts.js"></scripts> 

使用腳本文件加載自動完成類。你的scripts.js文件看起來像這樣。

// scripts.js custom js file 
$(document).ready(function() { 
    google.maps.event.addDomListener(window, 'load', initialize); 
}); 

function initialize() { 
    var input = document.getElementById('location'); 
    var autocomplete = new google.maps.places.Autocomplete(input); 
} 
0
<input id="autocomplete" placeholder="Enter your address" type="text"/> 
<script src="~/Scripts/jquery-1.10.2.min.js"></script> 
<script type="text/javascript" src="https://mapenter code heres.googleapis.com/maps/api/js?key=AIzaSyC7vPqKI7qjaHCE1SPg6i_d1HWFv1BtODo&libraries=places"></script> 
<script type="text/javascript"> 
    function initialize() { 

     new google.maps.places.Autocomplete(
     (document.getElementById('autocomplete')), { 
      types: ['geocode'] 
     }); 
    } 

    initialize(); 

</script>