【问题标题】:Leaflet Control Gecoder - Exception: "Nominatim is not a constructor"Leaflet Control Gecoder - 例外:“Nominatim 不是构造函数”
【发布时间】:2016-02-02 06:17:49
【问题描述】:

我在将传单控制 Gecoder 插件用于传单地图 (leaflet-control-geocoder) 时遇到了一些问题,我一定是个白痴,因为我只是尝试复制我在此页面上找到的相同示例 demo-plugin。 问题是当我尝试初始化控件时,出现异常“这个'GeoCoder'不是构造函数”。示例代码如下:

//Set leaflet Map, with markercluster and other basic functionality
//Set variables
var geoCoderGoogle,geoCoderControl;
var btn,selection,marker,selector;
var geocoders = {
    'Nominatim': L.Control.Geocoder.nominatim(),
    'Bing': L.Control.Geocoder.bing(bingAPIKey),
    'Mapbox': L.Control.Geocoder.mapbox(mapBoxAPIKey),
    'Google': L.Control.Geocoder.google(googleAPIKey),
    'Photon': L.Control.Geocoder.photon()
};
//Set geocoders on ready of the document
jQuery( document ).ready(function() {
    //Init the leafletMap if  is not setted
    //invoke the geocoder plugin on the leaflet map
    addPluginGeoCoder();
    //implement select of the geocoder.
    for (var name in geocoders) {
        btn = L.DomUtil.create('button', 'leaflet-bar', selector);
        btn.innerHTML = name;
        (function(n) {
            L.DomEvent.addListener(btn, 'click', function() {
                select(geocoders[n], this);
            }, btn);
        })(name);
        if (!selection) select(geocoders[name], btn);
    }


});

function select(geocoder, el) {
    if (selection) L.DomUtil.removeClass(selection, 'selected');
    geoCoderControl.options.geocoder = geocoder;
    L.DomUtil.addClass(el, 'selected');
    selection = el;
}

//Now add the control for the Geocoder with a function
function addPluginGeoCoder() {
    alert("Compile addPluginGeoCoder...");
    try {
        if (jQuery.isEmptyObject(geoCoderControl)) {
            alert("1");
            selector = L.DomUtil.get('geocode-selector');
            alert("2")
            geoCoderControl = new L.Control.Geocoder({ geocoder: null });
            alert("3")
            geoCoderControl.addTo(map);
            alert("4")
        } else {
            map.addControl(geoCoderControl);
        }
        alert("...compiled addPluginGeoCoder");
    }catch(e){
        alert("Exception:addPluginGeoCoder->"+e.message);
    }
}

现在,当我运行这段代码时,我会收到以下警报:'1','2','Exception:addPluginGeoCoder-> Nominatim 不是构造函数'。 即使我从“地理编码器”列表中删除了 Nominatim 地理编码器,我也有同样的问题。

无论如何,我的最终目标是使用带有 google api 的传单 GeoCoder 插件并转换坐标中的地址,获取坐标并将它们保存在数组中以供更多用途。所以在设置了 GeoCoderController 之后,我可能会调用这样的函数:

function saveInfo(){ 
 geoCoderControl.options.geocoder.geocode(address, function(results) {
                var latLng= new L.LatLng(results[0].center.lat, results[0].center.lng);
                marker = new L.Marker (latLng);
                map.addlayer(marker);
                addressVar = address;
                otherVar = result.html;
                alert("add marker:" + result.name + "," + result.center.lat + "," + result.center.lng + "," + otherVar);
                array.push(result.name, otherVar, result.center.lat, result.center.lng,addressVar);
            });
}

欢迎任何帮助。

更新:我在 jsfiddle 示例中创建了相同的问题:link example

Firefox 浏览器说:“Nominatim 不是构造函数”。
Internet Explorer 说:“对象不支持此操作”。

【问题讨论】:

    标签: javascript google-maps leaflet


    【解决方案1】:

    嗯,我猜您使用的是旧版本,您看到的问题已在此处修复:https://github.com/perliedman/leaflet-control-geocoder/commit/423610e7f63fd381357f44b2bc9853bd919c3e88

    升级到 1.3.1,据我所知,这应该得到修复。

    顺便说一句:您真的应该考虑在 Chrome 中使用开发者工具,它比通过alerts 进行调试要强大得多。

    【讨论】:

    • 事实上它适用于 1.3.1 版本,我可以在你的 github 项目的 README 文件中建议,下载链接“下载最新版本”链接到 1.3.1 版本吗?无论如何,非常感谢这个插件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 2021-01-11
    • 2015-12-04
    • 2018-11-29
    • 2016-12-04
    相关资源
    最近更新 更多