1
0

osm.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. var map;
  2. var layer_mapnik;
  3. var layer_tah;
  4. var layer_markers;
  5. var PI = Math.PI;
  6. var latfield = '';
  7. var lonfield = '';
  8. var latfield_id='';
  9. var lonfield_id='';
  10. var centerlon = 10;
  11. var centerlat = 52;
  12. var zoom = 6;
  13. function lon2merc(lon) {
  14. return 20037508.34 * lon / 180;
  15. }
  16. function lat2merc(lat) {
  17. lat = Math.log(Math.tan( (90 + lat) * PI / 360)) / PI;
  18. return 20037508.34 * lat;
  19. }
  20. function merc2lon(lon) {
  21. return lon*180/20037508.34;
  22. };
  23. function merc2lat(lat) {
  24. return Math.atan(Math.exp(lat*PI/20037508.34))*360/PI-90;
  25. };
  26. OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, {
  27. defaultHandlerOptions: {
  28. 'single': true,
  29. 'double': false,
  30. 'pixelTolerance': 0,
  31. 'stopSingle': false,
  32. 'stopDouble': false
  33. },
  34. initialize: function(options) {
  35. this.handlerOptions = OpenLayers.Util.extend(
  36. {}, this.defaultHandlerOptions
  37. );
  38. OpenLayers.Control.prototype.initialize.apply(
  39. this, arguments
  40. );
  41. this.handler = new OpenLayers.Handler.Click(
  42. this, {
  43. 'click': this.trigger
  44. }, this.handlerOptions
  45. );
  46. },
  47. trigger: function(e) {
  48. var lonlat = map.getLonLatFromViewPortPx(e.xy);
  49. lat=merc2lat(lonlat.lat);
  50. lon=merc2lon(lonlat.lon);
  51. if(parent.document.getElementById(latfield_id)==null){
  52. latfield=document.getElementById('osmlat');
  53. }else{
  54. latfield=parent.document.getElementById(latfield_id);
  55. }
  56. if(parent.document.getElementById(lonfield_id)==null){
  57. lonfield=document.getElementById('osmlon');
  58. }else{
  59. lonfield=parent.document.getElementById(lonfield_id);
  60. }
  61. latfield.value = lat;
  62. lonfield.value = lon;
  63. }
  64. });
  65. function init(){
  66. var field = window.name.substring(0, window.name.lastIndexOf("."));
  67. if(parent.document.getElementById(field+".latfield")!=null){
  68. latfield_id = parent.document.getElementById(field+".latfield").value;
  69. document.getElementById('osm').style.display="none";
  70. }
  71. if(parent.document.getElementById(field+".lonfield")!=null){
  72. lonfield_id = parent.document.getElementById(field+".lonfield").value;
  73. }
  74. if(parent.document.getElementById(field+".centerlat")!=null){
  75. centerlat =parseFloat(parent.document.getElementById(field+".centerlat").value);
  76. }
  77. if(parent.document.getElementById(field+".centerlon")!=null){
  78. centerlon = parseFloat(parent.document.getElementById(field+".centerlon").value);
  79. }
  80. if(parent.document.getElementById(field+".zoom")!=null){
  81. zoom = parseFloat(parent.document.getElementById(field+".zoom").value);
  82. }
  83. }
  84. function drawmap() {
  85. OpenLayers.Lang.setCode('de');
  86. mapdiv=document.getElementById('map');
  87. mapdiv.style.height=window.innerHeight+"px";
  88. mapdiv.style.width=window.innerWidth+"px";
  89. map = new OpenLayers.Map('map', {
  90. projection: new OpenLayers.Projection("EPSG:900913"),
  91. displayProjection: new OpenLayers.Projection("EPSG:4326"),
  92. controls: [
  93. new OpenLayers.Control.Navigation(),
  94. new OpenLayers.Control.PanZoomBar()],
  95. maxExtent:
  96. new OpenLayers.Bounds(-20037508.34,-20037508.34, 20037508.34, 20037508.34),
  97. numZoomLevels: 18,
  98. maxResolution: 156543,
  99. units: 'meters'
  100. });
  101. layer_mapnik = new OpenLayers.Layer.OSM.Mapnik("Mapnik");
  102. map.addLayers([layer_mapnik]);
  103. var y =lat2merc(centerlat);
  104. var x =lon2merc(centerlon);
  105. map.setCenter(new OpenLayers.LonLat(x, y), zoom);
  106. // Check for geolocation support
  107. if(navigator.geolocation){
  108. navigator.geolocation.getCurrentPosition(function(position){
  109. var y =lat2merc(position.coords.latitude);
  110. var x =lon2merc(position.coords.longitude);
  111. map.setCenter(new OpenLayers.LonLat(x, y), '17');
  112. });
  113. }
  114. var click = new OpenLayers.Control.Click();
  115. map.addControl(click);
  116. click.activate();
  117. }