changeset 172:c62f0cf2bf16 gmap2

It almost works \:D\
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 11 Mar 2014 21:17:52 +0200
parents b874aae3e75f
children fa0a8bb7fcfd
files map.js markers.js
diffstat 2 files changed, 28 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/map.js	Tue Mar 11 20:48:17 2014 +0200
+++ b/map.js	Tue Mar 11 21:17:52 2014 +0200
@@ -2,7 +2,6 @@
 
 var pmap;
 var pmapCanvas;
-var proj;
 
 var pmapTileSize = 256;
 var pmapTileOrig = pmapTileSize / 2.0;
@@ -16,14 +15,33 @@
 }
 
 
-function pmapLatLngToMapCoords(latLng)
+function pmapLatLngToMapCoords(latLng, zoom)
 {
   msiny = pmapMinMax(Math.sin((latLng.lat() * Math.PI) / 180.0), -0.9999, 0.9999);
 
   px = latLng.lng() * pmapTilePxPerDeg;
   py = 0.5 * Math.log((1 + msiny) / (1 - msiny)) * - pmapTilePxPerRad;
 
-  return new google.maps.Point(Math.round((px * pmapTileOrig + 1) / 2), Math.round((py * pmapTileOrig + 1) / 2));
+  pz = 1 << zoom;
+  return new google.maps.Point(px * pz, py * pz);
+}
+
+
+function pmapMapCoordsToLatLng(point, zoom)
+{
+  pz = 1 << zoom;
+  px = point.x / pz;
+  py = point.y / pz;
+
+  lng = (px - pmapTileOrig) / pmapTilePxPerDeg;
+  latRadians = (py - pmapTileOrig) / - pmapTilePxPerRad;
+
+  //lat = radiansToDegrees(2 * Math.atan(Math.exp(latRadians)) - Math.PI / 2);
+  //lat = (2 * Math.atan(Math.exp(latRadians)) - Math.PI / 2) / (Math.PI / 180);
+
+  lat = (360 * Math.atan(Math.exp(latRadians))) / Math.PI - 90;
+
+  return new google.maps.LatLng(lat, lng);
 }
 
 
@@ -86,9 +104,9 @@
 //
 function pmapCoordinateListener(point)
 {
-  var p = pmapLatLngToMapCoords(point.latLng);
-  var tx = p.x;
-  var ty = p.y;
+  var p = pmapLatLngToMapCoords(point.latLng, 7);
+  var tx = Math.round((p.x + 1) / 2.0);
+  var ty = Math.round((p.y + 1) / 2.0);
 
   var cx = tx;
   var cy = ty;
--- a/markers.js	Tue Mar 11 20:48:17 2014 +0200
+++ b/markers.js	Tue Mar 11 21:17:52 2014 +0200
@@ -73,7 +73,7 @@
   if (name.indexOf('FERRY') == 0)
     type = "ferry";
 
-  var ppos = new google.maps.LatLng(0, 0); //proj.fromPixelToLatLng(new google.maps.Point(m["x"] * 2+1, m["y"] * 2+1),7);
+  var ppos = pmapMapCoordsToLatLng(new google.maps.Point(m["x"] * 2 + 1, m["y"] * 2 + 1), 7);
   var marker = pmapCreateMarker(ppos, m["name"], m["html"], pmapIcons[type], type, m["continent"], mclass);
 
   if (pmapAreas[type] == undefined)
@@ -122,7 +122,6 @@
     document.getElementById("sidecontent").innerHTML = pmapSideBuiltinHTML;
   });
 
-/*
   //
   // Parse tradelane markers from another XML
   //
@@ -139,7 +138,6 @@
     pmapMMgr.refresh();
   });
 
-
   //
   // Create GMap polylines from tradelane line data
   //
@@ -157,8 +155,8 @@
       for (var npt = 0; npt < points.length; npt++)
       {
         var pt = points[npt];
-        coords.push(proj.fromPixelToLatLng(new google.maps.Point(pt["x"] * 2 + 1, pt["y"] * 2 + 1), 7));
-//        coords.push(new google.maps.Point(x*2 + 1, y*2 + 1));
+        coords.push(pmapMapCoordsToLatLng(new google.maps.Point(pt["x"] * 2 + 1, pt["y"] * 2 + 1), 7));
+//        coords.push(new google.maps.Point(pt["x"]*2 + 1, pt["y"]*2 + 1));
       }
 
       // Create new polyline and add it to map overlay
@@ -172,7 +170,7 @@
       poly.setMap(pmap);
     }
   });
-*/
+
 
   //
   // Create map control elements