Leaflet Control Gecoder - 例外:“Nominatim不是构造函数”

时间:2015-11-02 11:17:13

标签: javascript google-maps leaflet

我有一些真正的问题,使用传单控制Gecoder插件的传单地图(leaflet-control-geocoder),我必须是一个非常白痴,因为我只是尝试复制我在此页面上找到的相同示例{{3} }。 问题是当我尝试初始化控件时,我有例外“这个'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','异常:addPluginGeoCoder-> Nominatim不是构造函数。 即使我从“地理编码器”列表中删除了Nominatim Geocoder,我也有同样的问题。

无论如何,我的最终目标是使用带有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示例中创建了相同的问题:demo-plugin

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

1 个答案:

答案 0 :(得分:1)

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

升级到1.3.1,据我所知,这应该是固定的。

BTW:你应该真正考虑在Chrome中使用开发者工具,它比通过alert进行调试更加强大。