经纬度坐标转换为像素坐标:
// 坐标转像素点
function LatLng2Pixel(latLng) {
var scale = Math.pow(2, GlobalMap.getZoom())
var proj = map.getProjection()
var bounds = map.getBounds()
var nw = proj.fromLatLngToPoint(new google.maps.LatLng(bounds.getNorthEast().lat(), bounds.getSouthWest().lng()))
var point = proj.fromLatLngToPoint(latLng)
return new google.maps.Point(Math.floor((point.x - nw.x) * scale), Math.floor((point.y - nw.y) * scale))
}
像素坐标转为经纬度坐标函数:
// 像素转坐标
function Pixel2LatLng(pixel) {
var scale = Math.pow(2, GlobalMap.getZoom())
var proj = map.getProjection()
var bounds = map.getBounds()
var nw = proj.fromLatLngToPoint(new google.maps.LatLng(bounds.getNorthEast().lat(), bounds.getSouthWest().lng()))
var point = new google.maps.Point()
point.x = pixel.x / scale + nw.x
point.y = pixel.y / scale + nw.y
return proj.fromPointToLatLng(point)
}