2017-10-07 41 views
0

嗨,我嘗試更改輸出結果,如鄰居,城市和國家。我的意圖是,當用戶輸入他的地址時,他只返回街道。如何格式化Google Maps API的輸出結果自動完成

我想刪除的數據的示例。 enter image description here

這是我的Google Maps API腳本。

<script> 
    function initAutocomplete() { 
     var map = new google.maps.Map(document.getElementById('map'), { 
     center: { 
      lat: -23.69389, 
      lng: -46.565 
     }, 
     zoom: 7, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
     }); 

     var marker = new google.maps.Marker({ 
     position: { 
      lat: 34.3630003, 
      lng: -84.332207 
     }, 
     map: map, 
     animation: google.maps.Animation.DROP, 
     draggable: true 
     }); 

     var searchBox = new google.maps.places.SearchBox(document.getElementById('pac-input')); 

     google.maps.event.addListener(searchBox, 'places_changed', function() { 
     var places = searchBox.getPlaces(); 
     var bounds = new google.maps.LatLngBounds(); 
     var i, place; 

     for (i = 0; place = places[i]; i++) { 
      bounds.extend(place.geometry.location); 
      marker.setPosition(place.geometry.location); 
     } 

     map.fitBounds(bounds); 
     map.setZoom(14); 
     }); 
    } 

    var map = document.getElementById("map"); 
    google.maps.event.trigger(map, 'resize'); 

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

這是我的搜索框。

<div class="input-field col s6"> 
     <i class="material-icons prefix">home</i> 
     <input placeholder="Defina um endereço para seu fornecedor" id="pac-input" name="addressRouteTransporter" type="text" class="validate"> 
     <label for="addressRouteTransporter">Endereço:</label> 
    </div> 

感謝您的幫助的xD

+0

你指的是改變你想點擊自動完成建議後顯示什麼? – henrisycip

+0

@henrisycip點擊建議 – Gabriel

回答

2

您的使用案例其實更適合使用商家信息自動填充功能,而不是地方搜索盒是。你要做的就是過濾選中的地方的address_components數組,並且只能得到routepremise等等,這就是「地址」由什麼組成。 例:

componentForm = { 
    street_number: 'short_name', 
    route: 'long_name', 
    sublocality_level_1: 'long_name', 
    premise: 'long_name' 
}; 

然後,只需輸入字段的值替換到您選擇組分的組合。 inputField.value = addressString;

我建議在這裏閱讀更多關於它:

「您可根據需要選擇不同的組件以便與不同國家的郵政地址格式 保持一致。「

我也想提一提你提供的代碼是非常不完整,我試圖創建一個最小的,可覈查的,完整的例子了它向你展示如何將基礎知識使用自動完成和地址組件。

工作的jsfiddle鏈接:https://jsfiddle.net/yy3p7hh9/1/

附加代碼片段如下:

var autocomplete, 
 
marker, 
 
map, 
 
inputField = document.getElementById('pac-input'), 
 
// this is the type of address components you would like to get. You will filter through this object and 
 
// check if the place result has any 1 of these address components. 
 
// Notice how type 'locality' will not be retrieved so big, general places without these properties will not 
 
// show 
 
componentForm = { 
 
    street_number: 'short_name', 
 
    route: 'long_name', 
 
    sublocality_level_1: 'long_name', 
 
    premise: 'long_name' 
 
}; 
 

 
function initAutocomplete() { 
 
    map = new google.maps.Map(document.getElementById('map'), { 
 
    center: { 
 
     lat: -23.69389, 
 
     lng: -46.565 
 
    }, 
 
    zoom: 7, 
 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
 
    }); 
 

 
    marker = new google.maps.Marker({ 
 
    position: { 
 
     lat: 34.3630003, 
 
     lng: -84.332207 
 
    }, 
 
    map: map, 
 
    animation: google.maps.Animation.DROP, 
 
    draggable: true 
 
    }); 
 

 
    // Create the autocomplete object, restricting the search to geographical 
 
    // location types. 
 
    autocomplete = new google.maps.places.Autocomplete(
 
    /** @type {!HTMLInputElement} */ 
 
    (inputField), { 
 
     types: ['geocode'] 
 
    }); 
 

 
    // When the user selects an address from the dropdown, populate the address 
 
    // fields in the form. 
 
    autocomplete.addListener('place_changed', fillInAddress); 
 
} 
 

 
function fillInAddress() { 
 
    var place = autocomplete.getPlace(); 
 
    var bounds = new google.maps.LatLngBounds(); 
 
    var addressString = ''; 
 

 
    if (place.geometry) { 
 
    bounds.extend(place.geometry.location); 
 
    marker.setPosition(place.geometry.location); 
 
    } 
 

 
    // 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++) { 
 
    // warning this only gets the first type of the component. E.g. 'sublocality_level_1' 
 
    var addressType = place.address_components[i].types[0]; 
 
    if (componentForm[addressType]) { 
 
     var val = place.address_components[i][componentForm[addressType]]; 
 
     addressString += ' ' + val + ','; 
 
    } 
 
    } 
 
    // just remove the last comma 
 
    addressString = addressString.replace(/,\s*$/, ""); 
 
    inputField.value = addressString; 
 
    console.log(addressString); 
 
    map.fitBounds(bounds); 
 
    map.setZoom(14); 
 
}
#map { 
 
    height: 100%; 
 
} 
 

 

 
/* Optional: Makes the sample page fill the window. */ 
 

 
html, 
 
body { 
 
    height: 100%; 
 
    margin: 0; 
 
    padding: 0; 
 
}
<div class="input-field col s6"> 
 
    <i class="material-icons prefix">home</i> 
 
    <input placeholder="Defina um endereço para seu fornecedor" id="pac-input" name="addressRouteTransporter" type="text" class="validate"> 
 
    <label for="addressRouteTransporter">Endereço:</label> 
 
</div> 
 
<div id="map"></div> 
 
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCD0rWgXRVBa7PgaTWXVp_gU51pgHGviYY&libraries=places&callback=initAutocomplete" async defer></script>

+0

後工作完美,我可以擴展功能?例如在輸入字段中添加郵政編碼輸出? – Gabriel

+0

當然!請繼續閱讀[Google文檔](https://developers.google.com/maps/documentation/javascript/places-autocomplete#address_forms),您會發現只需將'componentForm'中的屬性添加/刪除到看看你想從結果出來的地方走出去。然後,將字符串附加到輸入字段。 – henrisycip