Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7772291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:51:19+00:00 2026-06-01T16:51:19+00:00

I have a problem with the click event on an OpenLayers Map on iPhone

  • 0

I have a problem with the click event on an OpenLayers Map on iPhone 4 using PhoneGap. It appears that the Position received by my setMarker(evt) function is dependent from the scroll position. After taking a closer look at the values for x and y I found out that y can even be negative.
The same code is working on Android, with setMarker receiving plausible values for both x and y.

Here is a short example where this problem occurs:

<!DOCTYPE html>
<html>
<head>
<title></title>

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
<meta charset="utf-8">
<style>
    html, body {
        height:99%;
        width:99%;
    }
</style>
<script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></script>
<script type="text/javascript" charset="utf-8" src="OpenLayers.js"></script>    
<script type="text/javascript">
var map, layer, markers;


function setMarker(e){
    markers.clearMarkers();
    var markerpos = map.getLonLatFromViewPortPx(e.xy);

    var size = new window.OpenLayers.Size(21,25);
    var offset = new window.OpenLayers.Pixel(-(size.w/2), -size.h);
    var icon = new window.OpenLayers.Icon('http://www.openlayers.org/dev/img/marker.png',size,offset);

    var marker = new window.OpenLayers.Marker(markerpos,icon);
    markers.addMarker(marker);
}

function onDeviceReady()
{
    map = new window.OpenLayers.Map("map", { controls: [new window.OpenLayers.Control.Navigation()]});
    layer = new window.OpenLayers.Layer.OSM("Simple OSM Map");      
    map.addLayer(layer);
    map.setCenter(
                  new window.OpenLayers.LonLat(10,50).transform(
                    new window.OpenLayers.Projection("EPSG:4326"), // From WGS 84
                    map.getProjectionObject()       // to Spherical Mercator
                    ),
                18 //Zoom
                );

    var touchNav = new window.OpenLayers.Control.TouchNavigation({
                        defaultClickOptions:{
                            pixelTolerance: 10
                            }
                        });

    // A click will set the marker:
    window.OpenLayers.Util.extend(touchNav, {
                                  defaultClick: function(evt) {
                                  alert("you clicked " + evt.xy);
                                  setMarker(evt);
                                  }
                                  });
    map.addControl(touchNav);   
    touchNav.activate();

    markers = new window.OpenLayers.Layer.Markers("Markers");
    map.addLayer(markers);
    console.log("map generated");

}

</script>
</head>
  <body onload="document.addEventListener('deviceready', onDeviceReady, false);">
    <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
  <div id="map" style="width:100%;height:100%"></div>
    <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
  </body>
</html>

A similar problem is here, but as I am already using OpenLayers 2.11 his solution does not help me: Problems with zooming and mouse events in Openlayers

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:51:20+00:00Added an answer on June 1, 2026 at 4:51 pm

    I finally got this code to work by using
    event.lastTouches[0] for the x/y coordinates on iPhone:

    var x = e.xy.x;
    var y = e.xy.y;
    if(window.device.platform === "iPhone"){
      x = e.lastTouches[0].clientX;
      y = e.lastTouches[0].clientY + window.pageYOffset;
    
      x -= document.getElementById("map").offsetLeft;
      y -= document.getElementById("map").offsetTop;
    }
    var markerpos = map.getLonLatFromViewPortPx(new window.OpenLayers.Pixel(x, y));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem that when I click on the image button instead of
I have a problem with an animation in jQuery using ajax. On the click
I have a problem with click event; here's the code: jQuery: $(document).ready(function() { $('.myClass').click(function()
I have table row with click event button with click event, that button is
I have a problem with the jQuery click event when an element is inside
I have an anchor which is using jQuery .click event to start a few
I'm using javascript and jQuery. I have a problem that my web app has
I have a problem with jQuery click event handling. Here's my code: $j(#btn_renommer_groupe).click(function(){ document.forms['creation_edition_groupe'].reset();
I have a problem when assigning functions to the click event of a button
I have problem with change of tab panel layout by click event on buton

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.