无法读取属性'getBounds'。使用addListenerOnce

时间:2015-06-09 15:14:13

标签: javascript google-maps

以下是我的代码。我添加了google.maps.event.addListenerOnce但仍然收到此错误。不确定如何解决。感谢。

'未捕获的TypeError:无法读取未定义'

的属性'getBounds'
var map;
var service;

function handleSearchResults(results, status)
{
    console.log(results);
}

function performSearch()
{
var request = {
    bounds: map.getBounds(),
    name: "McDonald's",
    } 
service.nearbySearch(request, handleSearchResults); 
}

function initialise(location)
{
    console.log(location);

    var currentLocation = new google.maps.LatLng(location.coords.latitude, location.coords.longitude);

    var mapOptions = {
    center: currentLocation,
    zoom: 12,
    mapTypeID: google.maps.MapTypeId.ROADMAP    
    };

    var map = new google.maps.Map(document.getElementById("map-canvas"),
    mapOptions);

    var marker = new google.maps.Marker({
        position: currentLocation,
        map: map            
    });

    service = new google.maps.places.PlacesService(map);

    google.maps.event.addListenerOnce(map, 'bounds_changed', performSearch);

}


$(document).ready(function()
{
    navigator.geolocation.getCurrentPosition(initialise);       
});

1 个答案:

答案 0 :(得分:1)

我不能说流利的js,但似乎"地图" var in"初始化"函数与全局函数不同(由两者前面的" var"关键字设置)。