Bookmarklet JumpToOsm

From OpenStreetMap Wiki
Jump to navigation Jump to search

This bookmarklet retrieves zoom and coordinates in current url and opens a page on openstreetmap.org at the same place.

Recognized formats

Current url has to contain the following informations :

  • either 0/0.000/0.000
  • or zoom=0&lat=0.000&lon=0.000 (order not important and possible other parameters in between)

If none of these informations are found, no tab is created (an error message is displayed).

Source

Bookmarklet

javascript:(function()%7Bjavascript%3A(function()%20%7Bparams%20%3D%20location.href.match(%2F%5Cd%7B1%2C2%7D%5C%2F-%3F%5Cd%7B1%2C3%7D%5C.%5Cd%2B%5C%2F-%3F%5Cd%7B1%2C3%7D%5C.%5Cd%2B%2F)%20%3Bparams2%20%3D%20location.href.match(%2F%40(%5Cd%7B1%2C3%7D%5C.%5Cd%2B)%2C%3F(%5Cd%7B1%2C3%7D%5C.%5Cd%2B)%2C(%5Cd%7B1%2C2%7D)z%2F)%20%3Bif%20(params!%3Dnull)%7Bwindow.open(%22https%3A%2F%2Fwww.openstreetmap.org%2F%23map%3D%22%2Bparams%5B0%5D)%20%3B%7D%20else%20if%20(params2!%3Dnull)%7Bwindow.open(%22https%3A%2F%2Fwww.openstreetmap.org%2F%23map%3D%22%2Bparams2%5B3%5D%2B%22%2F%22%2Bparams2%5B1%5D%2B%22%2F%22%2Bparams2%5B2%5D)%20%3B%7D%20else%20%7Bzoom%20%3D%20location.href.match(%2Fzoom%3D(%5Cd%7B1%2C2%7D)%2F)%20%3Blat%20%3D%20location.href.match(%2Flat%3D(-%3F%5Cd%7B1%2C3%7D%5C.%5Cd%2B)%2F)%20%3Blon%20%3D%20location.href.match(%2Flon%3D(-%3F%5Cd%7B1%2C3%7D%5C.%5Cd%2B)%2F)%20%3Bif%20(zoom!%3Dnull%20%26%26%20lat!%3Dnull%20%26%26%20lon!%3Dnull)%20%7Bwindow.open(%22https%3A%2F%2Fwww.openstreetmap.org%2F%23map%3D%22%2Bzoom%5B1%5D%2B%22%2F%22%2Blat%5B1%5D%2B%22%2F%22%2Blon%5B1%5D)%20%3B%7D%20else%20%7Balert(%22Jump%2520to%2520OpenStreetMap%2520impossible.%5Cn%5Cn%5CCould%2520not%2520find%2520zoom%2520and%2520coordinates%2520in%2520url.%22)%20%3B%7D%7D%7D)()%7D)()

Readable version

javascript:
(function() {
  params = location.href.match(/\d{1,2}\/-?\d{1,3}\.\d+\/-?\d{1,3}\.\d+/) ;
  google = location.href.match(/@(\d{1,3}\.\d+),?(\d{1,3}\.\d+),(\d{1,2})z/) ; 
  if (params!=null){
    window.open("https://www.openstreetmap.org/#map="+params[0]) ;
  } else if (google!=null){
    window.open("https://www.openstreetmap.org/#map="+google[3]+"/"+google[1]+"/"+google[2]) ;
  } else {
    zoom = location.href.match(/zoom=(\d{1,2})/) ;
    lat = location.href.match(/lat=(-?\d{1,3}\.\d+)/) ;
    lon = location.href.match(/lon=(-?\d{1,3}\.\d+)/) ;
    if (zoom!=null && lat!=null && lon!=null) {
      window.open("https://www.openstreetmap.org/#map="+zoom[1]+"/"+lat[1]+"/"+lon[1]) ;
    } else {
      alert("Jump%20to%20OpenStreetMap%20impossible.\n\n\Could%20not%20find%20zoom%20and%20coordinates%20in%20url.") ;
    }
  }
}
)()

Crédits

Auteur : the_knife
Licence : copyleft

Updated to work with Google Maps by PangoSE (talk) 11:34, 8 January 2020 (UTC)