2013-09-25 47 views
0

我在的OpenLayers爲什麼map.setCenter不工作?

<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()。當我嘗試在我的代碼中使用它時,我的地圖突然消失了。 我也得到了follwoing錯誤在我的控制檯窗口:

Cannot call method 'containsLonLat' of null 

我試過這個網站上每一個可能的方法,但沒有達成任何地方。什麼可以是問題嗎?

+1

不要在多個StackExchange站點上發佈相同的問題。 [(討論和解釋在這裏)](http://meta.stackexchange.com/questions/64068/is-cross-posting-a-question-on-multiple-stack-exchange-sites-permitted-if-thequ ) – kryger

+0

當然..我只是渴望知道答案..我從現在開始謹慎.. :) – writeToBhuwan

回答

0

我想你把一個額外的支架放在你的​​代碼中,嘗試map.setCenter(新的OpenLayers.LonLat(77,28),4);看看它是否有效。

+0

謝謝JSC ..但它是一個錯字錯誤..沒有幫助.. :( – writeToBhuwan