将标记放到Google地图中新的重新居中位置

时间:2018-11-19 18:05:06

标签: javascript google-maps-markers

当我在google-maps上输入新地址时,我试图添加一个标记。输入地址,地图重新居中至该位置,但标记未显示在该新位置。我在代码中缺少一些小东西吗?

function initAutocomplete() {
    // Create the autocomplete object
    autocomplete = new google.maps.places.Autocomplete(document
        .getElementById('autocomplete'));

    // When the user selects an address from the dropdown, show the place selected
    autocomplete.addListener('place_changed', onPlaceChanged);
}

function onPlaceChanged() {
    place = autocomplete.getPlace();
    if (!place.geometry) {
        // User entered the name of a Place that was not suggested and
        // pressed the Enter key, or the Place Details request failed.
        window.alert("No details available for input: '" + place.name + "'");
        return;
    }

    // If the place has a geometry, then present it on a map.
    if (place.geometry.viewport) {
        map.fitBounds(place.geometry.viewport);

    } else {
        map.setCenter(place.geometry.location);
        map.setZoom(17);  

    }

    marker.setPosition(place.geometry.location);
    marker.setVisible(true);

}

0 个答案:

没有答案