【问题标题】:'myFunction is not a function', JS & Google Maps v 3?'myFunction 不是函数',JS & Google Maps v 3?
【发布时间】:2010-10-22 09:29:24
【问题描述】:

您好,我不知道为什么会出现此错误。

我有一个输入列表,其中的值包含 lat/long/name/address。

在地图上打印标记工作正常,使用 infowin 工作正常,直到我意识到它只在所有窗口中打开相同的信息。 Soo,显然我需要将this(我刚刚单击的标记)附加到 eventListner。

但是,执行此操作时,我收到一条错误消息view.createMarkerHTML is not a function

问题:如何在正确的标记上附加要打开的正确信息?

view = {
        map: {
            init: function init (){
                view.map.initMap();
            },

            initMap: function initMap() {
                if(navigator.geolocation) {
                    function hasPosition(position) {
                        var point = new google.maps.LatLng(position.coords.latitude, position.coords.longitude),     
                        myOptions = {
                            zoom: 12,
                            center: point,
                            mapTypeId: google.maps.MapTypeId.ROADMAP
                        },   
                        mapDiv = document.getElementById("map"),
                        map = new google.maps.Map(mapDiv, myOptions);                        
                        view.map.handleMarkers(map);

                    }                    
                    navigator.geolocation.getCurrentPosition(hasPosition);

                } 
            },


            handleMarkers: function handleMarkers(map) {
                var list = $('#pois input'),
                    l = list.length,
                    i = 0, lat = '', long = '', marker = {}, theName = '', address = '', info = {};



                 for (i = 0; i < l; i += 1) {
                    info = $(list[i]).val().split('|');
                    lat = parseFloat(info[0], 10);
                    long = parseFloat(info[1], 10);
                    theName = info[2];
                    address = info[3];

                    marker = new google.maps.Marker({
                        position: new google.maps.LatLng(lat, long),
                        map: map,
                        icon: icon
                    }); 



                    google.maps.event.addListener(marker, 'click', function() {
                        currentMarker = this;view.map.createMarkerHTML(map, theName, address);
                    });                  
                 } 
            } ,

             createMarkerHTML: function createMarkerHTML(map, theName, address) {

                var contentString =
                    '<div id="gMapInfoWin">' +
                        '<h1>' + theName + '</h1>' +
                        '<ul>' +
                            '<li>' + address + '</li>' +
                        '</ul>' +
                    '</div>'
                ;

                currentMarker.infowindow = new google.maps.InfoWindow({
                    content: contentString
                });

                currentMarker.infowindow.open(map, currentMarker);
            } 
       }
};

【问题讨论】:

  • 我在任何地方都看不到view 的定义,如果没有这个,这个问题非常很难回答。不过,如果想玩,请尝试view.map.createMarkerHTML(map, theName, address);
  • 尼克,你应该把它作为答案发布,因为这就是答案。
  • 当然是 view.map.function() 我完全忘记了地图对象。很容易对自己的代码视而不见,嘿。非常感谢。
  • 但是,主要问题仍然存在,我将更改我的代码以便更容易理解,谢谢

标签: javascript jquery google-maps-api-3


【解决方案1】:

你不应该使用闭包来保持你在循环中设置的变量的值吗?

(function(theName, address) {
    google.maps.event.addListener(marker, 'click', function() {
        currentMarker = this;view.map.createMarkerHTML(map, theName, address);
    });
}(theName, address));

您甚至可以在参数中添加标记而不是使用currentMarker

【讨论】:

  • 你看不到的部分代码是一个闭包,但无论如何谢谢
  • 嗯...但是即使这段代码在一个闭包中...我认为您仍然需要另一个来保存变量theNameaddress 的值。你试过了吗?
【解决方案2】:

我敢打赌,这是一个范围界定问题。无法访问您的库我无法确定这是否可行,但请尝试替换:

google.maps.event.addListener(marker, 'click', function() {
    currentMarker = this;view.map.createMarkerHTML(map, theName, address);
});

用这个:

  google.maps.event.addListener(marker, 
                                'click', 
                                createClickListener(  this, 
                                                      map, 
                                                      theName, 
                                                      address ) );

然后在程序的其他地方添加这个函数:

function createClickListener( cm, map, theName, address )
{
    return function() {
        currentMarker = cm;
        view.map.createMarkerHTML(map, theName, address);
    }
}

【讨论】:

  • 谢谢,但我使用的是 google.maps.event.addListener(),这是 google maps v 3 中的标准
  • 好的,但这并不意味着它不可能是范围界定问题。
【解决方案3】:
for (i = 0; i < l; i += 1) {
                    info = $(list[i]).val().split('|');
                    lat = parseFloat(info[0], 10);
                    long = parseFloat(info[1], 10);

                    marker = new google.maps.Marker({
                        position: new google.maps.LatLng(lat, long),
                        map: map,
                        icon: icon
                    }); 

                   marker.theName = info[2];
                   marker.address = info[3];

                    google.maps.event.addListener(marker, 'click', function() {
                        currentMarker = this;   
                        m_name = this.theName;    
                        m_address = this.address        
                        view.map.createMarkerHTML(map, m_name, m_address);
                    });                  
                 } 

marker.theName = info[2];
marker.address = info[3];
currentMarker = 这个;
m_name = this.theName;
m_address = this.address

...是灵魂解决方案!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-31
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 2016-08-10
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多