Skip to content

Instantly share code, notes, and snippets.

@ka2n
Last active December 13, 2015 22:29
Show Gist options
  • Save ka2n/4985270 to your computer and use it in GitHub Desktop.
Save ka2n/4985270 to your computer and use it in GitHub Desktop.
OpenLayers.LonLat projection transformation(EPSG:4326 <=> EPSG:900913)
var p4, p9;
p4 = new OpenLayers.Projection('EPSG:4326');
p9 = new OpenLayers.Projection('EPSG:900913');
var v, p4_lat,p4_lon, p9_lat, p9_lon;
v = new OpenLayers.LonLat( 135, 35);
p4_lat = v.lat;
p4_lon = v.lon;
// 値を確認
console.assert(p4_lat == 35 && p4_lon == 135, '1');
// EPSG:4326 => EPSG:900913へ変換
v.transform(p4, p9);
p9_lat = v.lat;
p9_lon = v.lon;
// EPSG:900913 => EPSG:4326へ変換
v.transform(p9, p4);
// 2度変換後の値が同じかチェック
console.assert(v.lat == p4_lat && v.lon == p4_lon, '2');
// EPSG:900913のLonLatオブジェクトを作る
v = new OpenLayers.LonLat( p9_lon, p9_lat);
// EPSG:900913 => EPSG:4326へ変換
v.transform(p9, p4);
// 最初の値と同じかチェック
console.assert(p4_lat == v.lat && p4_lon == v.lon, '3');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment