【问题标题】:TypeError: map is undefined google api类型错误:地图未定义 google api
【发布时间】:2014-01-14 12:40:56
【问题描述】:

我一直在参考一个找到的教程,并在使用 google maps api 时偶然发现了一个错误。我得到一个错误,告诉我一个变量实际上是没有定义的。我不确定是什么问题。请帮忙。

    <title></title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> 
<script type="text/javascript" 
    src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=true">
</script>

    <script type="text/javascript">

    var map;
    var service;

    function handleSearchResults (results, status)
    {
         console.log(results);
    }
    function performSearch ()
    {
        var request = {
            bounds: map.getBounds(),
            name: "McDonald's" // within the bounds find the given name
        }
        service.nearbySearch(request, handleSearchResults);
    }

    function initialize (location) //initializing geolocation function
    {
            console.log(location);

        var currentLocation = new google.maps.LatLng(location.coords.latitude, location.coords.longitude)
            var mapOptions = {
                center: currentLocation,
                zoom: 13
            };
            var map = new google.maps.Map(document.getElementById("map-canvas"),
                mapOptions);
                // To add the marker to the map, use the 'map' property
            var marker = new google.maps.Marker({
                position: currentLocation,
                map: map // referencing google map from variable map above
             });

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

      // wait until map bounds are initialized before performing search
      google.maps.event.addListenerOnce(map, 'bounds_changed', performSearch);
    }

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

    });

    </script>





</html>

【问题讨论】:

  • 看起来您从未调用过您的 initialize 函数
  • 好吧,初始化是在 $(document).ready(function() 中调用的。

标签: javascript


【解决方案1】:

您似乎遇到了该映射变量的范围问题,因为您是在 $(document.ready() 回调的范围内调用 initialize 函数。我在下面的 jsfiddle 中对其进行了一些调整。这能解决问题吗?

jsfiddle

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

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

function initialize (location) //initializing geolocation function
{
    console.log(location);

    var currentLocation = new google.maps.LatLng(location.coords.latitude, location.coords.longitude)
    var mapOptions = {
        center: currentLocation,
        zoom: 13
    };

    map = new google.maps.Map(document.getElementById("map-canvas"),
                              mapOptions);
    // To add the marker to the map, use the 'map' property
    var marker = new google.maps.Marker({
        position: currentLocation,
        map: map // referencing google map from variable map above
    });

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

    // wait until map bounds are initialized before performing search
    google.maps.event.addListenerOnce(map, 'bounds_changed', performSearch);
}   

$(document).ready(function() {
    var map;
    var service;

    navigator.geolocation.getCurrentPosition(initialize);    
})

【讨论】:

  • 我只是没有包括那部分,因为 stackoverflow 对太多代码和解释大惊小怪
  • 哦,明白了。这是您偶然遇到的相同错误吗? stackoverflow.com/questions/2832636/…
  • 嗯不完全...我已经用谷歌地图事件监听器处理了这个问题。我遇到了一个与名为 map 的变量有关的不同问题
  • 我刚刚更新了我的答案。让我知道这是否有效。
  • 对,实际上没有必要将所有函数都包装在document.ready 的范围内,但我认为您只需要将map 变量放在同一范围内即可。另一种解决方案是这样做:jsfiddle.net/qu6gt/6
猜你喜欢
  • 1970-01-01
  • 2013-11-11
  • 1970-01-01
  • 2019-09-14
  • 2022-01-17
  • 1970-01-01
  • 2018-02-17
  • 2011-04-26
  • 1970-01-01
相关资源
最近更新 更多