如何在google maps v3上使用fromPointToLatLng

zjm*_*126 4 javascript google-maps google-maps-api-3

这是我的代码:

var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
    zoom: 8,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
google.maps.event.addListener(map,'projection_changed', function () {
    var proj = map.getProjection();
    ltBound = proj.fromPointToLatLng(new google.maps.Point(0,100))
    rbBound = proj.fromPointToLatLng(new google.maps.Point(100,200))
    console.log(ltBound,rbBound)
});
Run Code Online (Sandbox Code Playgroud)

我想在谷歌地图上创建一个节点,但我无法使用fromPointToLatLng正确的方式.我能做什么?

Byr*_*ngh 7

以下是我使用fromPointToLatLng()方法的方法.首先需要在地图上找到左上角的Lat/Lng然后从那里计算偏移量.

var pixelToLatlng = function(xcoor, ycoor) {
  var ne = map.getBounds().getNorthEast();
  var sw = map.getBounds().getSouthWest();
  var projection = map.getProjection();
  var topRight = projection.fromLatLngToPoint(ne);
  var bottomLeft = projection.fromLatLngToPoint(sw);
  var scale = 1 << map.getZoom();
  var newLatlng = projection.fromPointToLatLng(new google.maps.Point(xcoor / scale + bottomLeft.x, ycoor / scale + topRight.y));
  return newLatlng;
};
Run Code Online (Sandbox Code Playgroud)

试试这里的小提琴:http://jsfiddle.net/mhaq865o/5/

你可以使用fromPointToLatLng()方法或OverlayView方法,如http://magicalrosebud.com/how-to-use-googlemaps-api-frompointtolatlng/


Mar*_*ler 3

Projection中的 和 方法在256x256 坐标系和现实世界坐标中的点之间进行转换fromPointToLatLngfromLatLngToPoint

当缩放级别为 0 时,整个 Google 地图为 256x256 像素。尝试缩小并输出在地图周围单击时获得的点:

google.maps.event.addListener(map, 'click', function(event) {
    var proj = map.getProjection();
    console.log('latLng: ' + event.latLng);
    console.log('Point:  ' + proj.fromLatLngToPoint(event.latLng));
});
Run Code Online (Sandbox Code Playgroud)

我认为您只需要知道/使用这个,如果您想计算缩放级别或做类似的事情,您需要知道现实世界坐标和屏幕像素之间的关系。

您询问有关向地图添加“节点”的问题。如果您正在考虑文档中所谓的“标记”,请参阅有关标记的部分