如何让OpenLayer地图放大iphone?

时间:2011-08-16 10:41:46

标签: javascript iphone openlayers

我在iphone中创建了OpenLayer地图应用程序。所以我想通过iphone的功能访问它的事件意味着我想通过触摸事件和放大器拖动OpenLayer地图。通过iphone缩放手势事件缩放。为了调用OpenLayer地图我已经在web视图中实现了一个html文件。它正常工作我得到了OpenLayer地图&地图触摸事件现在我也想像其他地图那样缩放地图或者图像将在iphone中放大。请指导我在iphone中的OpenLayer地图中实现缩放功能..

用于调用地图&触摸事件我在.html文件中使用此代码

   <!DOCTYPE html>
    <html>
      <head>
        <title>OpenLayers Tutorial - Basic Map Setup</title>
        <script src="http://openlayers.org/api/OpenLayers.js"></script>
        <script type="text/javascript">
        var map, baseLayer;
            function init(){
                map = new OpenLayers.Map('map');            
                baseLayer = new OpenLayers.Layer.WMS("OpenLayers WMS", "http://labs.metacarta.com/wms/vmap0", {layers:"basic"});
                map.addLayer(baseLayer);
                map.setCenter(new OpenLayers.LonLat(0,0),1);             
            }
        function touchHandler(event)
        {
       var touches = event.changedTouches,
       first = touches[0],
       type = "";
     switch(event.type)
       {
         case "touchstart": type = "mousedown"; break;
         case "touchmove":  type="mousemove"; break;
         case "touchend":   type="mouseup"; break;
         default: return;
     }
     var simulatedEvent = document.createEvent("MouseEvent");
        simulatedEvent.initMouseEvent(type, true, true, window, 1,
        first.screenX, first.screenY,
        first.clientX, first.clientY, false,
        false, false, false, 0/*left*/, null);
        first.target.dispatchEvent(simulatedEvent);
        event.preventDefault();
     }
    document.addEventListener("touchstart", touchHandler, true);
     document.addEventListener("touchmove", touchHandler, true);
     document.addEventListener("touchend", touchHandler, true);
     document.addEventListener("touchcancel", touchHandler, true);
        </script>
        <style>
        @media screen
        {
            #map{width: 300px; height:360px; border: 2px solid black;}
        }
        </style>
      </head>
      <body onload="init()">
        <h3>OpenLayers Tutorial - Basic Map Setup</h3>
        <div id="map"></div>
      </body>
    </html>

此代码正常运行。

2 个答案:

答案 0 :(得分:1)

自己实施这将是痛苦的。您可能希望使用版本2.11 RC2或trunk,因为它变得非常简单 - 您需要做的就是添加TouchNavigation控件,如this example所示:

function init() {
    map = new OpenLayers.Map({
        div: "map",
        theme: null,
        projection: new OpenLayers.Projection("EPSG:900913"),
        units: "m",
        numZoomLevels: 18,
        maxResolution: 156543.0339,
        maxExtent: new OpenLayers.Bounds(
            -20037508.34, -20037508.34, 20037508.34, 20037508.34
        ),
        controls: [
            new OpenLayers.Control.TouchNavigation({
                dragPanOptions: {
                    enableKinetic: true
                }
            }),
            new OpenLayers.Control.ZoomPanel()
        ],
        layers: [
            new OpenLayers.Layer.OSM("OpenStreetMap", null, {
                transitionEffect: 'resize'
            })
        ]
    });
    map.setCenter(new OpenLayers.LonLat(0, 0), 3);
}

答案 1 :(得分:0)

即将推出的2.11版本的OpenLayers将支持用于平移,缩放的触摸事件...... 请参阅:http://dev.openlayers.org/examples/mobile.html

HTH,