您可以在https://developers.google.com/maps/documentation/javascript/maptypes#PixelCoordinates找到实现它的基本公式
pixelCoordinate = worldCoordinate * 2zoomLevel
获取点击的世界坐标,计算像素坐标,加上偏移量,计算新的世界坐标,你就得到了想要的位置。
您需要两个函数将 LatLng 转换为像素(反之亦然):
fromLatLngToPoint() and fromPointToLatLng()
示例函数:
google.maps.event.addListener(map, 'click', function(e){
//assume a cursor with a size of 22*40
var //define the anchor, the base(top-left) is 0,0
//bottom middle will be 11,40
anchor = new google.maps.Point(11,40),
//the map-projection, needed to calculate the desired LatLng
proj = this.getProjection(),
//clicked latLng
pos = e.latLng;
//the power of the map-zoom
power = Math.pow(2,map.getZoom()),
//get the world-coordinate
//will be equal to the pixel-coordinate at zoom 0
point = proj.fromLatLngToPoint(pos),
//calculate the new world-coordinate based on the anchor
offsetPoint = new google.maps.Point(
(point.x*power+anchor.x)/power,
(point.y*power+anchor.y)/power
),
//convert it back to a LatLng
offsetPosition = proj.fromPointToLatLng(offsetPoint);
//done
new google.maps.Marker({ position:offsetPosition, map:map});
});
演示:http://jsfiddle.net/doktormolle/NYh7g/