views:

110

answers:

1

I was trying to make Bing Maps's VEMap compatible with touch devices, in particular the iphone / webkit mobile.

In the following example, this works: - pan using one finger - zoom in/out using pinch (two fingers)

However if I put both in the same time, then the zoom doesn't work anymore: you can see in the console that VEMap.ZoomIn() or VEMap.ZoomOut() are called, but nothing happens (no error, exception or anything, it just doesn't do anything).

There most likely exists a solution given http://www.bing.com/maps handles both touch and gesture fine, but that code is compressed, so I'm stumped so far, so any idea would be a huge help.

<!DOCTYPE html>
<html>
<head>
    <title>VEMap</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.3&amp;mkt=en-us"&gt;&lt;/script&gt;
</head>
<body onload="init()">


<div id="mymap" style="position: relative; width: 256px; height: 256px;"></div>


<script type="text/javascript">
// Reference to the VEMap
var map = null;

// Whether a gesture is being performed currently or not.
var gesture = false;

// Translates touch event names into similar mouse event names
var touch2mouse = {"touchstart": "mousedown", "touchmove": "mousemove", "touchend": "mouseup"};


// Initializes the map and adds the touch/gesture listeners
function init(){
    map = new VEMap('mymap');
    map.HideDashboard();
    map.LoadMap(new VELatLong(51.64, -0.18), 9, VEMapStyle.Road);

    if (typeof Touch == "object"){
        // Zooms the map using touch-pinch
        document.addEventListener("gesturestart", ongesture, true);
        document.addEventListener("gesturechange", ongesture, true);
        document.addEventListener("gestureend", ongesture, true);

        // Pans the map using touch-drag
        // If you comment out these three lines, pinch-zoom works :(
        document.addEventListener("touchstart", ontouch, true);
        document.addEventListener("touchmove", ontouch, true);
        document.addEventListener("touchend", ontouch, true);
    }
}


// One finger interaction
function ontouch(e){
    var touches = e.changedTouches;
    if ((!gesture) && (touches.length == 1)){
        if (e.type == "touchmove") e.preventDefault();
        var obj = touches[0];
        var e2 = document.createEvent("MouseEvent");
        e2.initMouseEvent(touch2mouse[e.type], true, true, window, 1, obj.screenX, obj.screenY, obj.clientX, obj.clientY, false, false, false, false, 0, null);
        obj.target.dispatchEvent(e2);
    }
}

// Two fingers interaction
function ongesture(e){
    e.preventDefault();
    switch(e.type){
        case "gesturestart":
            gesture = true;
        break;
        case "gestureend":
            gesture = false;
            if (e.scale > 1.2){
                console.log('Attempting to zoom IN.');
                map.ZoomIn();
            } else if (e.scale < 0.8){
                map.ZoomOut();
                console.log('Attempting to zoom OUT.');
            }
        break;
    }
}
</script>



</body>
</html>
+1  A: 

I am having exactly the same problem, and it seems there is a clear lack of documentation on that particular point!

I found some information about the viewport at the following address on the msdn forum, but for me, even removing the viewport meta attributes and fixing the size of the map container doesn't give any positive results.

http://social.msdn.microsoft.com/Forums/en-US/vemapcontroldev/thread/342e5807-7bb5-4c54-94d4-095e800fb736

I hope I'll find something soon.

ekynoxe
I tried to change the thresholds for the scaling events, to 1 (and not respectively 1.2 and 0.8) and it works on iPhone (3.1.2 - webkit) but not on Android (chrome).There is certainly something to do with the events capture on different mobile platforms, but I don't understand why this is not documented better in the API documentation.
ekynoxe
It also works on iPhone 4, but on both devices, it's extremely slow to react.
ekynoxe
Indeed, having 1 as the threshold seems to help, but it's still only working something like once every ten times in average (but at least that's already one more than before :) ). Ah, I wish they had included in the mapcontrol whatever hack they used on bing.com because that map works great.
wildpeaks
Looking into the control, in "VEMap._GetMapFromGUID(THE_MAP_GUID).vemapcontrol.ZoomIn()" more exactly, I can assume that whatever problem is it is triggered by one of these three lines: a = preferredView.makeCopy(); a.SetZoomLevel(currentView.zoomLevel + 1); or SetView(a);My guess would be on SetView, but I don't have time right now to dig further.
wildpeaks
(ah, I wish StackOverflow wouldn't remove breaklines from replies, it makes code hard to read)
wildpeaks
I had exchanges with someone at Microsoft that confirmed that there are no specific mobile APIs for bing Maps other than for windows phone 7. It seems such an API is in preparation but is not available even for pre-testing.Now back to Google maps...
ekynoxe