Created
July 24, 2014 05:54
-
-
Save hiro-matsuno/e57511d6aa346b9121ed to your computer and use it in GitHub Desktop.
OpenStreetMapでクリックしたMapの中心にマーカーを入れる
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//これはOpenLayerで作成しております。 | |
//htmlファイルに以下のタグを入れて設定して下さい。 | |
//<script src='OpenLayers.js'></script> | |
//<script src='mapping_touch.js'></script> | |
//<style type="text/css"> | |
// html { height: 100% } | |
// body { height: 100%; margin: 0; padding: 0 } | |
// #map_canvas { height: 50%;float: left; } | |
//</style> | |
//クリックアクションの登録 | |
OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, { | |
initialize:function(options){ | |
OpenLayers.Control.prototype.initialize.apply(this, arguments); | |
this.handler = new OpenLayers.Handler.Click( | |
this, | |
{ | |
'click':this.onClick, | |
'dblclick':this.onDblClick | |
}, | |
{ | |
'single':true, | |
'double':true, | |
'pixelTolerance':0, | |
'stopSingle':false, | |
'stopDouble':false | |
} | |
); | |
}, | |
onClick:function(e) { | |
//クリック座標の中心の緯度経度の取得 | |
var lonLat = map.getCenter().transform( | |
new OpenLayers.Projection("EPSG:900913"), | |
new OpenLayers.Projection("EPSG:4326") | |
); | |
lat = lonLat.lat; | |
lon = lonLat.lon; | |
//マーカーの追加 | |
var markers = new OpenLayers.Layer.Markers("Markers"); | |
map.addLayer(markers); | |
//マーカーの設定 | |
var marker = new OpenLayers.Marker( | |
new OpenLayers.LonLat(lon,lat) | |
.transform( | |
new OpenLayers.Projection("EPSG:4326"), | |
new OpenLayers.Projection("EPSG:900913") | |
) | |
); | |
//マーカーの追加 | |
markers.addMarker(marker); | |
}, | |
onDblClick:function(e) { | |
alert('Double Click'); | |
} | |
}); | |
//データの表示 | |
var click = new OpenLayers.Control.Click(); | |
map.addControl(click); | |
click.activate(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment