原理:
通过自定义GMapType(ditu)和自定义投影坐标GProjection(dituProjection) 来实现偏差纠正。
原理是修改ditu层的投影换算函数,
在.fromLatLngToPixel()里面 +Offset
在.fromPixelToLatLng()里面 -Offset
这样子就无需修改上层Overlay的坐标参数,直接使用正确的Lat,Lng即可
代码:(以上海为例)
function load_gmap()
{
//shanghai
var xOffset= -0.001889737;
var yOffset= 0.004844069;
var map = null;
if(GBrowserIsCompatible()) {
var Cditu = new GCopyright(1,
new GLatLngBounds(
new GLatLng(-90,-180),
new GLatLng(90,180) ),
0,
"Mapabc.com");
var copyright = new GCopyrightCollection("");
copyright.addCopyright(Cditu);
var dituTileLayer = new GTileLayer(copyright, 1, 17);
dituTileLayer.getTileUrl = function(tile, zoomlevel,x) {
var url = G_NORMAL_MAP.getTileLayers()
[0].getTileUrl(tile,zoomlevel,x);
var bits = url.split("&");
bits[1] = "
http://mapgoogle.mapabc.com/googlechina/maptile?" +
bits[1];
bits.shift();
url = bits.join("&");
return url;
};
function dituProjection(xOffset,yOffset){
this.xOffset = xOffset;
this.yOffset = yOffset;
}
dituProjection.prototype = new GProjection();
dituProjection.prototype.fromLatLngToPixel = function(latlng, zoom){
return (G_NORMAL_MAP.getProjection()).fromLatLngToPixel(new
GLatLng(latlng.lat()+this.xOffset,latlng.lng()+this.yOffset),zoom);
};
dituProjection.prototype.fromPixelToLatLng =
function(pixel,zoom,unbounded)
{
var latlng =
(G_NORMAL_MAP.getProjection()).fromPixelToLatLng(pixel,zoom,unbounded);
return new GLatLng(latlng.lat()-this.xOffset,latlng.lng()-
this.yOffset);
}
dituProjection.prototype.tileCheckRange = function(tile, zoom,
tilesize)
{
return
(G_NORMAL_MAP.getProjection()).tileCheckRange(tile,zoom,tilesize);
}
dituProjection.prototype.getWrapWidth = function(zoom)
{
return (G_NORMAL_MAP.getProjection()).getWrapWidth(zoom);
}
var ditu = new GMapType([dituTileLayer],
new dituProjection(xOffset,yOffset),
"Ditu",
{ shortName: "ditu", alt: "layer from
ditu.google.com" }
);
map = new GMap2(document.getElementById('gmap'),{mapTypes:[ditu,/
*G_NORMAL_MAP,*/G_SATELLITE_MAP]});
map.addControl(new GLargeMapControl());
/**map.addControl(new GMapTypeControl());**/
map.addControl(new GOverviewMapControl());
map.addControl(new GScaleControl());
map.enableDoubleClickZoom();
/**map.enableScrollWheelZoom();**/
map.setCenter(new GLatLng(31.231628368031693, 121.47645235061645 ),
16);
ditu.getName = function(){ return '中国地图';};
ditu._getName = function(){ return 'ditu';};
/*
G_NORMAL_MAP.getName = function(){ return '世界地图';};
G_NORMAL_MAP._getName = function(){ return 'Map';};
*/
G_SATELLITE_MAP.getName = function(){ return '卫星图';};
G_SATELLITE_MAP._getName = function(){ return 'Satellite';};
/** the map type **/
var _mt = ditu;
map.setMapType(_mt);
map.addControl(new GMapTypeControl());
var point = new GLatLng(31.231628368031693, 121.47645235061645);
map.addOverlay(new GMarker(point));
}
}
演示地址:
http://www.mipang.com/tmp/gmap_offset_test.ue
交流:
http://www.mipang.com/groups/31@/t.26540.f4a.htm