Javascript 从索引页获取搜索位置&;缩放到地图页上的位置

Javascript 从索引页获取搜索位置&;缩放到地图页上的位置,javascript,google-maps,Javascript,Google Maps,我正在尝试设置从索引页到地图页的重定向,因此,如果用户使用Places Autocomplete库搜索地址,他们将被重定向到地图页,其中包含他们搜索的位置。索引页不包括地图,只包括带有Places Autocomplete库的搜索栏 我在索引页上设置的功能如下: var place; function indexSearch() { // Get Address Field and Attach Places Autocomplete var input = document.g

我正在尝试设置从索引页到地图页的重定向,因此,如果用户使用Places Autocomplete库搜索地址,他们将被重定向到地图页,其中包含他们搜索的位置。索引页不包括地图,只包括带有Places Autocomplete库的搜索栏

我在索引页上设置的功能如下:

var place;
function indexSearch() {
    // Get Address Field and Attach Places Autocomplete
    var input = document.getElementById('autocomplete');
    var autocomplete = new google.maps.places.Autocomplete(input);

    // Get Place When Place_Changed Event if Fired
    google.maps.event.addListener(autocomplete, 'place_changed', function () {
        place = autocomplete.getPlace();
        if (typeof place !== 'undefined') {
            window.location.href = 'map';
            // Don't know what to do here
            // as I can't use map.panTo(place.geometry.location) because
            // map does not exist on index page.
        }
    });

    // Erase Previous Address if Search Bar is Clicked
    inputIndex.onclick = function () {
        inputIndex.value = '';
    } 
}
是否需要在负责设置映射的
initialize()
函数中包含一些内容

function initialize() {
    var mapOptions = {
        zoom: 17,
        center: {
            lat: -33.8666,
            lng: 151.1958
        },
        disableDefaultUI: true
    };
    var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    // Catch place and pan map to its location
}
编辑:至少提供否决投票的理由


我正在研究使用
localStorage
sessionStorage
。将lat和lng传递给其中一个,并在地图页面加载时检索位置。这是最佳解决方案吗?一旦我有了解决方案,我将更新答案。

以下是我最后做的事情。这对我有用。希望它能帮助将来的人

indexSearch()
函数中,在
sessionStorage
中设置一个键以存储搜索位置的lat和lng。在触发
place\u事件时捕获此信息

var place;
function indexSearch() {
    // Get Address Field and Attach Places Autocomplete
    var input = document.getElementById('autocomplete');
    var autocomplete = new google.maps.places.Autocomplete(input);

    // Get Place When Place_Changed Event if Fired
    google.maps.event.addListener(autocomplete, 'place_changed', function () {
        place = autocomplete.getPlace();
        if (typeof place !== 'undefined') {
            window.location.href = 'map';
            // Set key in sessionStorage
            sessionStorage.myLat = place.geometry.location.lat();
            sessionStorage.myLat = place.geometry.location.lng();
        }
    });

    // Erase Previous Address if Search Bar is Clicked
    inputIndex.onclick = function () {
        inputIndex.value = '';
    } 
}
然后在
initialize()
函数中,检查该键是否存在。如果有,请将用户带到该位置

function initialize() {
    var mapOptions = {
        zoom: 17,
        center: {
            lat: -33.8666,
            lng: 151.1958
        },
        disableDefaultUI: true
    };
    var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

    // Get place from sessionStorage if its exists & pan map to its location
    var placeLat = sessionStorage.getItem('myLat');
    var placeLng = sessionStorage.getItem('myLng');
    if (placeLat && placeLng) {
         var searchedPlace = new google.maps.LatLng(placeLat, placeLng);
         map.setCenter(searchedPlace);
         map.setZoom(15);
    }
}

我以前用过
localStorage
sessionStorage
解决您的情况,我认为这是最好的方法,兄弟。试试这个谢谢。我在下面发布了我的解决方案。