「Openlayers開発」クリックされた位置の緯度経度を取得するサンプルコード
JavaScriptコード:
var click = new OpenLayers.Control.Click();
map.addControl(click);
click.activate();
OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, {
defaultHandlerOptions: {
'single’: true,
'double’: false,
'pixelTolerance’: 0,
'stopSingle’: false,
'stopDouble’: false
},
initialize: function(options) {
this.handlerOptions = OpenLayers.Util.extend(
{}, this.defaultHandlerOptions
);
OpenLayers.Control.prototype.initialize.apply(
this, arguments
);
this.handler = new OpenLayers.Handler.Click(
this, {
'click’: this.trigger
}, this.handlerOptions
);
},
trigger: function(e) {
markers.clearMarkers();
var url = 'gis/imgage/zaho.png’;
//サイズ
var paSze = new OpenLayers.Size(30, 30);
var calculateOffset = function(size) {
return new OpenLayers.Pixel(-(size.w/2), -size.h);
};
var icon = new OpenLayers.Icon(url, paSze, null, calculateOffset);
var lonlat = map.getLonLatFromPixel(e.xy);
marker = new OpenLayers.Marker(lonlat, icon);
markers.addMarker(marker);
//座標変換
lonlat.transform(new OpenLayers.Projection(“EPSG:3857"),
new OpenLayers.Projection(“EPSG:4326"));
//親画面のパラメータを設定
parent.gisx = lonlat.lon.toFixed(5);
parent.gisy = lonlat.lat.toFixed(5);
}
});