2012-05-07 26 views
6

我正在編程一個地圖頁面,我需要捕獲地圖上點擊/點擊的位置並存儲座標。我正在使用OpenLayers js。在桌面瀏覽器(IE/FF/Chrome)上,這工作正常。在移動設備上,水龍頭會在默認Android瀏覽器(無論是在真實設備還是仿真器上)正確捕獲。OpenLayers緯度在Webkit移動瀏覽器中不準確捕獲

但是,在移動webkit瀏覽器(iPhone Safari & Android Chrome Beta)中,我們遇到了水龍頭在實際水龍頭的更高几個像素(朝北)捕捉的問題。錯誤是不固定的(所以,我不能只加100到事件的XY重新校準頂部)。

這裏是我使用的函數clickhandler代碼:

OpenLayers.Control.ClickHandler = OpenLayers.Class(OpenLayers.Control, {     
    defaultHandlerOptions: { 
     'single': true, 
     'double': false, 
     'pixelTolerance': 0, 
     'stopSingle': false, 
     'stopDouble': false 
    }, 

    initialize: function(options) { 
     this.handlerOptions = OpenLayers.Util.extend(
      {}, this.defaultHandlerOptions 
     ); 
     OpenLayers.Control.prototype.initialize.apply(
      this, arguments 
     ); 
     this.handler = new OpenLayers.Handler.Click(
      this, { 
       'click': this.trigger 
      }, this.handlerOptions 
     ); 
    }, 

    trigger: function(e) { 
     that.inputLonLat_EPSG4326 = null; 

     var lonlat = that.inputMap.getLonLatFromViewPortPx(e.xy); 

     that.logMessage("XY " + e.xy); 
     that.logMessage("LonLoat " + lonlat); 

     that.inputMarkers.clearMarkers(); 


    that.inputMarkers.addMarker(new OpenLayers.Marker(lonlat,that.icons["green"].clone())); 

    lonlat.transform(that.inputMap.getProjectionObject(), new OpenLayers.Projection("EPSG:4326")); 

    that.inputLonLat_EPSG4326 = lonlat; 
    // For the moderation sections 
    $('#alertLatitude').val(that.inputLonLat_EPSG4326.lat); 
    $('#alertLongitude').val(that.inputLonLat_EPSG4326.lon); 

    //lonLat2.transform(that.inputMap.getProjectionObject(), new OpenLayers.Projection("EPSG:4326")); 
    that.logMessage("Stored lat " + that.inputLonLat_EPSG4326.lat); 
    that.logMessage("Stored lon " + that.inputLonLat_EPSG4326.lon);  

    that.callFunction(that.inputMapListener, e); 
    } 
}); 

我應該是做不同的事情?有人在使用OpenLayers時看到移動webkit瀏覽器的不準確問題嗎?

回答

1

我終於找到了發生這種情況的原因。似乎在webkit移動瀏覽器上,圖書館似乎正在獲取(或派生)的x,y基於頁面,而不是基於地圖所在的元素。因此計算結果是關閉的。看起來,不準確性不能通過添加地圖元素的xy或一些這樣的基於DOM的圖形來解決(我試過了)。

我解決了這個問題,將Map放在IFrame中,然後讓iFrame安置在地圖元素內。這樣,由地圖點擊處理程序接收的x,y在iFrame內是準確的,因此lat,long也是精確的。由於父級和iFrame都來自同一個域,所以沒有任何問題來回傳遞。

爲了完成上下文,基於iFrame的解決方案與我上面測試的BB 9 &,Android Chrome,Android Default和iPhone Safari完全兼容。

在退房的解決方案 - http://suat1.vocanic.net//saralee/apps/dengue_alert/http://suat1.vocanic.net//saralee/apps/dengue_alert/mobileMap.php(WIP版本可能發生變化或隨着時間的推移打破)

內嵌框架
相關問題