Skip to content

Instantly share code, notes, and snippets.

@haiiro-shimeji
Last active December 16, 2015 07:59
Show Gist options
  • Select an option

  • Save haiiro-shimeji/5402799 to your computer and use it in GitHub Desktop.

Select an option

Save haiiro-shimeji/5402799 to your computer and use it in GitHub Desktop.
clickhold/taphold implementation example on openlayers map
/**
* CSS Reset
* From Blueprint reset.css
* http://blueprintcss.googlecode.com
*/
html, body, div, span, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, code, del, dfn, em, img, q, dl, dt, dd, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td {margin:0;padding:0;border:0;font-weight:inherit;font-style:inherit;font-size:100%;font-family:inherit;vertical-align:baseline;}
body {line-height:1.5;}
table {border-collapse:separate;border-spacing:0;}
caption, th, td {text-align:left;font-weight:normal;}
table, td, th {vertical-align:middle;}
blockquote:before, blockquote:after, q:before, q:after {content:"";}
blockquote, q {quotes:"" "";}
a img {border:none;}
/**
* Basic Typography
*/
body {
font-family: "Lucida Grande", Verdana, Geneva, Lucida, Arial, Helvetica, sans-serif;
font-size: 80%;
color: #222;
background: #fff;
margin: 1em 1.5em;
}
pre, code {
margin: 1.5em 0;
white-space: pre;
}
pre, code {
font: 1em 'andale mono', 'lucida console', monospace;
line-height:1.5;
}
a[href] {
color: #436976;
background-color: transparent;
}
h1, h2, h3, h4, h5, h6 {
color: #003a6b;
background-color: transparent;
font: 100% 'Lucida Grande', Verdana, Geneva, Lucida, Arial, Helvetica, sans-serif;
margin: 0;
padding-top: 0.5em;
}
h1 {
font-size: 130%;
margin-bottom: 0.5em;
border-bottom: 1px solid #fcb100;
}
h2 {
font-size: 120%;
margin-bottom: 0.5em;
border-bottom: 1px solid #aaa;
}
h3 {
font-size: 110%;
margin-bottom: 0.5em;
text-decoration: underline;
}
h4 {
font-size: 100%;
font-weight: bold;
}
h5 {
font-size: 100%;
font-weight: bold;
}
h6 {
font-size: 80%;
font-weight: bold;
}
.olControlAttribution {
bottom: 5px;
}
/**
* Map Examples Specific
*/
.smallmap {
width: 512px;
height: 256px;
border: 1px solid #ccc;
}
#tags {
display: none;
}
#docs p {
margin-bottom: 0.5em;
}
/* mobile specific */
@media only screen and (max-width: 600px) {
body {
height : 100%;
margin : 0;
padding : 0;
width : 100%;
}
#map {
background : #7391ad;
width : 100%;
}
#map {
border : 0;
height : 250px;
}
#title {
font-size : 1.3em;
line-height : 2em;
text-indent : 1em;
margin : 0;
padding : 0;
}
#docs {
bottom : 0;
padding : 1em;
}
#shortdesc {
color : #aaa;
font-size : 0.8em;
padding : 1em;
text-align : right;
}
#tags {
display : none;
}
}
@media only screen and (orientation: landscape) and (max-width: 600px) {
#shortdesc {
float: right;
width: 25%;
}
#map {
width: 70%;
}
#docs {
font-size: 12px;
}
}
body {
-webkit-text-size-adjust: none;
}
<!DOCTYPE html>
<html>
<head>
<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=0">
<meta name="apple-mobile-web-app-capable" content="yes">
<title>Clickhold/Taphold example</title>
<link rel="stylesheet" href="http://openlayers.org/dev/theme/default/style.css" type="text/css">
<link rel="stylesheet" href="style.css" type="text/css">
<script src="http://openlayers.org/dev/OpenLayers.js"></script>
<script src="taphold.js"></script>
</head>
<body onload="init()">
<h1 id="title">Clickhold or Taphold</h1>
<p id="shortdesc">
click or tap on the map and hold 1 sec, then callcack is fired.
</p>
<div id="tags">
</div>
<div id="map" class="smallmap"></div>
<input id="output" size=52></div>
<div id="docs">
See the <a href="taphold.js" target="_blank">
taphold.js</a> to see how this is done.
</div>
</body>
</html>
var HoldHandler = OpenLayers.Class(OpenLayers.Handler, {
holdTime: 500,
initialize: function(control, callbacks, options) {
OpenLayers.Handler.prototype.initialize.apply(this, arguments);
},
touchstart: function(evt) {
this._mousedown(evt);
},
touchmove: function(evt) {
this._mousemove(evt);
},
touchend: function(evt) {
this._mouseup(evt);
},
mousedown: function(evt) {
this._mousedown(evt);
},
mousemove: function(evt) {
this._mousemove(evt);
},
mouseup: function(evt) {
this._mouseup(evt);
},
_mousedown: function(evt) {
var self = this;
this.callback('mousedown', [evt]);
this._cancelhold();
var hold = this._starthold();
setTimeout(function() {
if (hold.flag) {
if (!evt.touches) {
self.callback('clickhold', [evt]);
} else {
self.callback('clickhold', [evt.touches[0]]);
}
}
}, this.holdTime);
return true;
},
_mousemove: function(evt) {
this._cancelhold();
},
_mouseup: function(evt) {
this.callback('mouseup', [evt]);
this._cancelhold();
return true;
},
_starthold: function() {
return (this.hold = { flag: true });
},
_cancelhold: function() {
if (this.hold) {
this.hold.flag = false;
}
}
});
var CustomControl = OpenLayers.Class(OpenLayers.Control, {
initialize: function(options) {
OpenLayers.Control.prototype.initialize.apply(
this, arguments
);
this.handler = new HoldHandler(
this, {
clickhold: this.clickhold
}, { holdTime: 1000 }
);
},
clickhold: function(evt) {
document.getElementById('output').value = "taphold at " + evt.clientX + ", " + evt.clientY;
}
});
function init() {
var map = new OpenLayers.Map({
div: "map",
allOverlays: true
});
var osm = new OpenLayers.Layer.OSM();
map.addLayers([osm]);
map.zoomToMaxExtent();
var control = new CustomControl;
map.addControl(control);
control.activate();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment