0

我在openlayers中有以下代码

<script defer="defer" type="text/javascript">
    var map = new OpenLayers.Map('map');
    var wms = new OpenLayers.Layer.WMS("OpenLayers WMS",
        "http://vmap0.tiles.osgeo.org/wms/vmap0", { layers: 'basic' });
    var dm_wms = new OpenLayers.Layer.WMS(
        "Canadian Data",
        "http://www2.dmsolutions.ca/cgi-bin/mswms_gmap",
        {
            layers: "bathymetry,land_fn,park,drain_fn,drainage," +
                    "prov_bound,fedlimit,rail,road,popplace",
            transparent: "true",
            format: "image/png"
        }, { isBaseLayer: true }
    );

    var vectorLayer = new OpenLayers.Layer.Vector("Overlay");
    map.setBaseLayer(vectorLayer);
    OpenLayers.Geometry.Point(77.391026, 28.535516), { some: 'data' }, { externalGraphic: 'Image/map-icons/Banquet_and_Marriage_halls.png', graphicHeight: 21, graphicWidth: 21 });

    var center = new OpenLayers.Geometry.Point(77.391026, 28.535516);

    center.transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
    map.setCenter(new OpenLayers.LonLat(77, 28), 4);
    var markers = new OpenLayers.Layer.Markers("Markers");


    var ll = new OpenLayers.LonLat(77.391026, 28.535516);
    var popupClass = OpenLayers.Popup.Anchored;
    var popupContentHTML = '<div style="height:40px;">Hello Noida</div>';
    addMarker(ll, popupClass, popupContentHTML, true);
    ll = new OpenLayers.LonLat(74.613647, 30.914007);
    popupClass = OpenLayers.Popup.Anchored;
    popupContentHTML = '<div style="height:40px;">Hello Punjab</div>';
    addMarker(ll, popupClass, popupContentHTML, true);

    map.addLayers([wms, dm_wms, markers]);


    var feature;
    var popup = null;
    function addMarker(ll, popupClass, popupContentHTML, closeBox, overflow) {

        feature = new OpenLayers.Feature(markers, ll);
        feature.closeBox = closeBox;
        feature.popupClass = popupClass;
        feature.data.popupContentHTML = popupContentHTML;
        feature.data.overflow = (overflow) ? "auto" : "hidden";

        var marker = feature.createMarker();

        var markerClick = function (evt) {
            //alert(popup);
            if (popup != null)
                popup.hide();
            popup = this.createPopup(this.closeBox);
            popup.contentSize = new OpenLayers.Size(10, 20);
            map.addPopup(popup);
            popup.show();

        };
        marker.events.register("mousedown", feature, markerClick);

        markers.addMarker(marker);
    }

</script>

如果我删除 map.setCenter(),一切都会正常工作。但是当我尝试在我的代码中使用它时,我的地图突然消失了。我的控制台窗口中也出现以下错误:

Cannot call method 'containsLonLat' of null 

我在这个网站上尝试了所有可能的方法,但没有到达任何地方.. 可能是什么问题?

4

2 回答 2

0

我认为您在代码中添加了一个额外的括号,请尝试 map.setCenter(new OpenLayers.LonLat(77, 28), 4); 看看它是否有效。

于 2013-09-25T16:58:57.970 回答
0

使用leaflet https://leafletjs.com/

var map = L.map('mapid').setView([77.391026, 28.535516], 16);
        L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
            attributionControl: true
        }).addTo(map);

参考:https ://leafletjs.com/examples/quick-start/

于 2019-12-24T11:16:02.657 回答